Test-driven development on the internet also talked a lot, PHP article also some, in Baidu and Google search, as if not seen a few talk with mock (camouflage object) of the technology, here write an article to talk about.
First of all, the basic concept of test-driven development: to write test cases first (generally this test case is automated unit test cases, easy to roll back execution), and then through the step-by-phase repair of the test case to complete the product code, the final test case after the repair, the product has been finished.
From my own practice, I think the use of test-driven development techniques in class library development is a good solution for the following reasons:
Being able to write test cases, which means that there is a clear understanding of the problem domain,
Save time writing documents, test cases are sample code for class library invocation.
The quality of the code is guaranteed because the process of writing the class library is the process of repairing the test case, so the class library is finished after the test case is repaired.
When it is easy to estimate, the problem of estimating library development time is reduced to estimating the time it takes to fix the test case, which is easier to estimate.
We explain the concept of test-driven development by writing a function of string-to-number, and then introduce mock technology. Before you begin, you need to install the PHPUnit and mockery libraries (this article does not use PHPUnit's own mock libraries):
# install Phpunitpear config-set auto_discover 1pear install pear.phpunit.de/phpunit# installation mock library sudo pear channel-discover Pear.survivethedeepend.comsudo pear channel-discover hamcrest.googlecode.com/svn/pearsudo Pear Install--alldeps Deepend/mockery
That from the concept of test-driven development, we first write the test case –parseinttest.php:
<? class ParseIntTest extends PHPUnit_Framework_TestCase { public function testparseintbasic () { $v = parse_int ("12345"); $this->assertequals (12345, $v); $v = parse_int (" -12345"); $this->assertequals ( -12345, $v); /* $v = parse_int ("ABCD"); $this->assertequals (0, $v); $v = parse_int ("0xab12 "); $this->assertequals (0xab12 , $v); $v = parse_int (" 01b "); $this->assertequals (1, $ V); */ } }?>
In the above code, we specify the requirements of the function Parse_int to be implemented through the unit test case, that is, we can parse integers, signed integers, etc., and because of time and resource constraints, we remove the support for hexadecimal and binary.
Run the following command to execute the test case:
PHPUnit--verbose Parseinttest
Because there is no code at this time, getting the expected error –phpunit tells us that we cannot find the Parse_int function:
PHPUnit 3.6.12 by Sebastian Bergmann.
PHP Fatal error:call to undefined function parse_int () in/var/www/pmdemo/parseinttest2.php on line 6
Then we'll build an empty Parse_int function –parseint.php:
<? function Parse_int ($str) {return 0; }?>
In the above code, we do not implement any logic of the function Parse_int, and then run the test case to get:
Shiyimin@ubuntu:/var/www/pmdemo$ phpunit--verbose parseinttest
PHPUnit 3.6.12 by Sebastian Bergmann.
F
time:0 seconds, MEMORY:2.75MB
There was 1 failure:
1) Parseinttest::testparseintbasic
Failed asserting that 0 matches expected 12345.
/var/www/pmdemo/parseinttest.php:7
failures!
Tests:1, Assertions:1, failures:1.
The error message highlighted in red from above, we know that the first judgment in the test case did not pass, that is, the following judgment statement did not execute successfully:
$this->assertequals (12345, $v);
This is because our Parse_int function always returns the value of 0, and we find this error, and we'll take this logic to fix the test case:
parseint.php
<? function Parse_int ($str) {$result = 0; $i = 0; for ($i = 0; $i < strlen ($STR); + + $i) {$result *= 10; $result + = $str [$i]-' 0 '; } return $result; }?>
Run the test case again:
Shiyimin@ubuntu:/var/www/pmdemo$ phpunit--verbose parseinttest
PHPUnit 3.6.12 by Sebastian Bergmann.
F
time:0 seconds, MEMORY:2.75MB
There was 1 failure:
1) Parseinttest::testparseintbasic
Failed asserting that 12345 matches expected-12345.
/var/www/pmdemo/parseinttest.php:10
failures!
Tests:1, Assertions:2, failures:1.
As you can see from the above results, the first test case has been fixed, and now it's working on a signed string, there is a problem, and the code continues to be repaired:
parseint.php
<? function parse_int ($STR) { $result = 0; $i = 0; $neg = 1; if ( $str [0] == '-' ) { $neg = -1; } for ( $i = 0; $i < strlen ($str), ++ $i ) { $result *= 10; $result += $str [$i] - ' 0 '; } return $result * $neg; }?>
To run the test case again:
Shiyimin@ubuntu:/var/www/pmdemo$ phpunit--verbose parseinttest
PHPUnit 3.6.12 by Sebastian Bergmann.
.
time:0 seconds, MEMORY:2.75MB
OK (1 Test, 2 assertions)
OK, this time all the test cases are passed, then our product code implementation is over, of course, the article for convenience, the above code is not a complete implementation, such as it can not handle the string "+12345" case.
The next article explains how to apply mock techniques in phpunit.
Using mockery in PHP for test-driven Development (TDD)-up