PHP unit testing tool phpunit initial experience
Today, I received a task to calculate numbers. Because many conditions such as integers, decimals, and scientific notation are involved, manual testing is very troublesome, so I thought of PHP's unit test tool phpunit, so I wrote a document for future reference.
After reading the phpunit documentation, I have some knowledge about it,
Http://pear.php.net/manual/en/packages.php.phpunit.intro.php
The workflow is as follows:
1. design your class/API
2. Create a test assembly
3. Implement class/API
4. Run the test
5. Correct test failures or errors and return to step 1.
Here is an example:
The following is the class you want to test. The formatn function is a function that takes 5 Valid digits of any number.
----------format_number.php-----------
class fo {
function fo() {
}
function formatn($num) {
$num = rtrim($num,"0");
$pos = strpos($num,".");
$num = str_replace(".","",$num);
$count1 = strlen($num);
$num = ltrim($num,"0");
$count2 = strlen($num);
$zeroc = $count1 - $count2;
$num = substr($num,0,6);
$num = round($num/10);
//$num = str_pad($num, 5, "0");
if ($pos !== false) {
$num = str_pad($num, (strlen($num)+$zeroc), "0", STR_PAD_LEFT);
$dotl = substr($num,0,$pos);
$dotr = substr($num,$pos);
$num = $dotl.".".$dotr;
}
return $num;
}
}
Create testcase and inherit from phpunit_testcase.
---------- Testcase. php -----------
<? PHP
Require_once 'format _ number. php ';
Require_once 'phpunit. php ';
Class fotest extends phpunit_testcase {
// This member variable is used to store the class reference to be tested.
VaR $ ABC;
// Constructor
Function fotest ($ name ){
$ This->; phpunit_testcase ($ name );
}
// A new class to be tested assigns a value to the member variable ABC
Function setup (){
$ This->; abc = new fo;
}
// Unset the class to be tested
Function teardown (){
Unset ($ this->; ABC );
}
// Custom testcase
Function testformatn1 (){
// Call the method of the class to be tested and place the result to the $ result variable.
$ Result = $ this->; ABC->; formatn ("100.234 ");
// Expected results
$ Expected= "100.23 ";
// Determine whether the attribute is equal. Here, the asserttrue method is used to determine whether the attribute value is true.
$ This->; asserttrue ($ result = $ expected );
}
Function testformatn2 (){
$ Result = $ this->; ABC->; formatn ("0.100234 ");
$ Expected= "0.10023 ";
$ This->; asserttrue ($ result = $ expected );
}
Function testformatn3 (){
$ Result = $ this->; ABC->; formatn ("0.100235 ");
$ Expected= "0.10024 ";
$ This->; asserttrue ($ result = $ expected );
}
Function testformatn4 (){
$ Result = $ this->; ABC->; formatn ("0.000100235 ");
$ Expected= "0.00010024 ";
$ This->; asserttrue ($ result = $ expected );
}
Function testformatn5 (){
$ Result = $ this->; ABC->; formatn ("0.000100232 ");
$ Expected= "0.00010023 ";
$ This->; asserttrue ($ result = $ expected );
}
Function testformatn6 (){
$ Result = $ this->; ABC->; formatn ("1343 ");
$ Expected= "1343 ";
$ This->; asserttrue ($ result = $ expected );
}
Function testformatn7 (){
$ Result = $ this->; ABC->; formatn ("1343.01 ");
$ Expected= "1343 ";
$ This->; asserttrue ($ result = $ expected );
}
Function testformatn8 (){
$ Result = $ this->; ABC->; formatn ("1343.05 ");
$ Expected= "1343.1 ";
$ This->; asserttrue ($ result = $ expected );
}
Function testformattings (){
$ Result = $ this->; ABC->; formatn ("0 ");
$ Expected = "0 ";
$ This->; asserttrue ($ result = $ expected );
}
Function testformatn10 (){
$ Result = $ this->; ABC->; formatn ("105.2342 ");
$ Expected= "105.23 ";
$ This->; asserttrue ($ result = $ expected );
}
Function testformatn11 (){
$ Result = $ this->; ABC->; formatn ("105.2375 ");
$ Expected= "105.24 ";
$ This->; asserttrue ($ result = $ expected );
}
Function testformatn12 (){
$ Result = $ this->; ABC->; formatn ("0.000523751 ");
$ Expected= "0.00052375 ";
$ This->; asserttrue ($ result = $ expected );
}
Function testformatn13 (){
$ Result = $ this->; ABC->; formatn ("0.000523755 ");
$ Expected= "0.00052376 ";
$ This->; asserttrue ($ result = $ expected );
}
}
Finally, you need a program to run the test.
----------runtest.php-----------
<?php
require_once 'testcase.php';
require_once 'PHPUnit.php';
$suite = new PHPUnit_TestSuite("foTest");
$result = PHPUnit::run($suite);
echo $result->;toString();
?>;
Now you can run the testcase command line.
PHP runtest. php
The result is as follows:
TestCase foTest->;testFormatn1() passed
TestCase foTest->;testFormatn2() passed
TestCase foTest->;testFormatn3() passed
TestCase foTest->;testFormatn4() passed
TestCase foTest->;testFormatn5() passed
TestCase foTest->;testFormatn7() passed
TestCase foTest->;testFormatn8() passed
TestCase foTest->;testFormatn9() passed
TestCase foTest->;testFormatn10() passed
TestCase foTest->;testFormatn11() passed
TestCase foTest->;testFormatn12() passed
TestCase foTest->;testFormatn13() passed
TestCase foTest->;testFormatn6() failed: expected TRUE, actual FALSE
Testformatn6 test failed,
We can check where our code goes wrong.
Add one point
You can also change the asserttrue method to assertequals as follows:
function testFormatn6() {
$result = $this->;abc->;formatn("1343");
$expected = "1343";
$this->;assertEquals($expected, $result);
}
If a failure occurs, the corresponding results are displayed intuitively (the error results can be displayed ):
TestCase foTest->;testFormatn8() failed: expected 1343 , actual 134.