Active Topics Memberlist Calendar Search Help | |
Register Login |
One Stop Testing Forum : Types Of Software Testing @ OneStopTesting : Unit Testing @ OneStopTesting |
Topic: phpPatterns |
|
Author | Message |
Sandeep
Newbie Joined: 23Feb2007 Online Status: Offline Posts: 1 |
Topic: phpPatterns Posted: 23Feb2007 at 4:33pm |
Unit testing is one of the practical facets of eXtreme programming. As a
In this article I’ll look at a script which makes unit testing a pleasure rather than a burden. eXtreme programming, even by it’s name, is a controversial subject. Is it some kind of methodology for programming while sky diving? Alot of what eXtreme programming advocates is “how to work” like coding in pairs. One aspect though which applies directly to code itself is unit testing. In general the “testing lifecycle” of software is summarized as;
Now for your typical But if the expiry date of the code we’re working on is longer than 24 hours, chances are we’ll end up writing something far bigger than we originally planned and start having to spend more and more time on debugging and testing code that wasn’t that well designed in the first place. This is where unit testing can be a real help. The basic concept of unit testing is write more code which will test the main code we’ve written, by “throwing” sample data at it and examining what it gets back. On reading “more code” you may be starting to lose interest but in general, writing test scripts is very little work and the good news is once they’re written, you can retest your code ad infinitum with nothing more than the click of a mouse. A change “here” may effect code we couldn’t have imagined - re-running a black box test tells us everything is still working properly. There are two approaches to unit testing: black box testing (which is what we’ll be looking at here) and while box testing.
Black box testing takes the approach of working outside
the code being tested (hence the code is a black box), passing data to
it and examining what it gets back. This approach is particularily
useful for classes where we’ve got a well designed White box testing takes the view that everything can ( and perhaps should ) be tested (no hiding behind APIs) and generally means adding the tests directly to the code being tested. In some cases it’s essential - a class method may return the correct value but some operation it performed “behind the scenes” has gone badly wrong. To some degree we’ve all used white box testing when using print statements inside a class to find out what’s going on. In a perfect world, given infinite time and interest, we should probably use both to the fullest extent. Just using black box testing though is a good start and meets the practical constraint effecting most developers: lack of time. One final piece of introduction: eXtreme programming suggests writing the tests before the code they’ll be testing. From a developers perspectice, it takes some getting used to but,
daring to intepret the intent of this practice, it’s meant to encourage
developers to focus on designing a solid Enough theory. Now to make good the claim that unit testing (black box at least) is both easy and fast to implement.
There are quite a few In this article we’ll be using PHPUnit from Vincent Oostindië, author of the Eclipse Library (which is well worth checking out by the way). It’s provided with the code for this article. This particular PHPUnit is a single script which is lightweight and extremely easy to use. Some would argue this is exactly what a unit testing tool should be like. Now a while back we looked at the Strategy Pattern. Using Vincent’s PHPUnit (which is well documented by the way - just point your browser at it and all will be revealed), the first thing is to create a sub directory “test” which will contain all the test scripts and PHPUnit itself (in the same code it’s renamed to index.php for convienience). Taking one class in particular to illustrate how the test scripts are written, lets test the ValidateUser class (from the strategy pattern article). The basic “rules” of Vincents test environment are; <list> Create one test class per test script, the class name being the same as the file name. All methods within the test class beginning with the letters “test” will be executed by PHPUnit All test classes must extend a class Test The test scripts need to include the script they will be testing (obviously).</list> Post Resume: Click here to Upload your Resume & Apply for Jobs |
|
IP Logged | |
Forum Jump |
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |
© Vyom Technosoft Pvt. Ltd. All Rights Reserved.