Re: Help getting started with unit testing
Posted: Fri Apr 18, 2008 12:00 pm
Ok, I never intended this discussion to head down the TDD path, but since it has taken that turn maybe we can look at it a little closer. My understanding of TDD is rather limited so I am throwing this out there as a series of questions...
et's say we are building a string object (for the sake of this question). To build the unit tests in a TDD fashion you would create a minimal class structure with one method that essentially returns true and test that, correct? So we would start off like this:
So now there is a skeleton of a class and we can set up a unit test like this:
So now the unit test passes. The goal now is to make the unit test fail then refactor the code so the unit test passes again. Or is that not correct?
So now the test fails so we need to rework the code so that it doesn't fail anymore. Is this correct?
And this would be a modified unit test:
Is this a correct approach to TDD (or something similar)?
et's say we are building a string object (for the sake of this question). To build the unit tests in a TDD fashion you would create a minimal class structure with one method that essentially returns true and test that, correct? So we would start off like this:
Code: Select all
<?php
class String {
public function __construct() {
}
public function hasString() {
return true;
}
}
?>Code: Select all
<?php
class StringTests extends UnitTestCase {
function TestString() {
$string = new String();
$this->assertTrue($string->hasString());
}
}
?>Code: Select all
<?php
class String {
public $string = null;
public function __construct() {
}
public function hasString() {
return null !== $this->string;
}
}
?>Code: Select all
<?php
class String {
public $string = null;
public function __construct($string = null) {
$this->string = $string;
}
public function hasString() {
return null !== $this->string;
}
}
?>Code: Select all
<?php
class StringTests extends UnitTestCase {
function TestString() {
$string = new String('Hello World');
$this->assertTrue($string->hasString());
}
}
?>