BrainBlog |
![]() |
![]() |
|
| BrainBlog is the Brains4All weblog. Established 2004 in The Netherlands. Brains have been working in IT since 1983, working on the internet since 1993. They have nothing particular to say, but their thoughts need a place to stay anyway. This is that place. | ![]() |
From concept to working software in 10 minutesMay 12, 2006 |
marko
So if testing is a good thing, why not test all the time? In fact, why not start out with testing? If you have a large stretched-out project with a fair bump of testing on the end, we’ve seen that often the testing phase will be pressured because of the deadline. What is even worse is that the valuable feedback from the testing phase will have no way to be incorporated into the design and code of your project. We’ve seen that to accommodate learning in a process we have to make it iterative and build in practices that facilitate and encourage learning. ![]() Test: Requirements: Design:
$objEmailValidator = new emailValidator;
$validEmailAddress = ‘marko@brains4all.com’;
$invalidEmailAddress = ‘http://www.brains4all.com’;
assertTrue($objEmailValidator->validate($validEmailAddress);
assertFalse($objEmailValidator->validate($invalidEmailAddress);
Apparently we are going to need some object to house our validation routines, it has a method called “validate” that you pass a single string value: the email address to check. In this case it passes in our valid email address. Code:
Class emailValidator {
function validate ($emailToCheck) {
if ($email == “marko@brains4all.com”) {
return true;
} else {
return false;
}
}
}
Run the tests, if and not until all the tests pass you can check it into the versioning system, from where it can be released as working software. That’s it; you’ve written your first test in an iterative manner and released your first working code for your customer to review and to bestow feedback on. But wait! Well, let’s try it and add another test. Remember this is an iterative process.
$objEmailValidator = new emailValidator;
$myValidEmailAddress = ‘marko@brains4all.com’;
$invalidEmailAddress = ‘http://www.brains4all.com’;
$yourValidEmailAddress = ‘blogreader@bloglines.com’;
assertTrue($objEmailValidator->validate($myValidEmailAddress);
assertTrue($objEmailValidator->validate($yourValidEmailAddress);
assertFalse($objEmailValidator->validate($invalidEmailAddress);
If we run the tests, the new test fails miserably. You’re right. You have to change the code to make it pass:
Class emailValidator {
function validate ($emailToCheck) {
if (preg_match("/^[a-z0-9_.-]+@[-.a-z0-9]+\.[a-z]{2,6}$/i", $emailToCheck) == 1) {
return true;
} else {
return false;
}
}
}
If changed the code to have a simple regular expression match. Let’s run the tests again. Hey, the tests pass; quickly check in your code to be delivered to the customer! Can you think of a test to break the code? Repeat until you can’t think of any test cases, or your test cases stop being sensible or relevant. Move on to the next tiny subset of functionality you need to implement. Be sure to talk to the customer about the software you’ve made. Find out how he likes it and to see if any improvements can be made. Does your code address what the user needs? Trackback PingsTrackBack URL for this entry: CommentsPost a comment |
Recent entries
Archives
CategoriesBlogRoll |




