Installation on Windows can refer to its manual
Download Phpunit.phar file First
1. Create a directory for the PHP binary executable file, such as C:\bin
2. Add the C:\bin to the system environment variable,
3. Open command-line cmd
4. New batch script, C:\bin\phpunit.cmd
CD C:\bin Echo " %~dp0phpunit.phar " %* > phpunit.cmdexit
After executing the above command at the command prompt, the Phpunit.cmd file is generated in the C:\bin directory
5. After opening a new window, confirm that the phpunit can be executed under any path
C:\users\username> PHPUnit--versionphpunit x.y.z by Sebastian Bergmann.
This prompt indicates that the PHPUnit installation was successful.
Ii. Writing PHPUnit Test Cases
To test an array operation with PHPUnit
1. Tests for class classes are written in class Classtest.
2.ClassTest (usually) inherits from Phpunit_framework_testcase.
3.
4. Within the test method, an assertion method similar to Assertequals () (see Appendix A, Assertion) is used to assert the match between the value and the expected value.
Example 2.1. To test an array operation with PHPUnit
<?PHPclassStacktestextendsphpunit_framework_testcase{ Public functionTestpushandpop () {$stack=Array(); $this->assertequals (0,Count($stack)); Array_push($stack, ' foo '); $this->assertequals (' foo '),$stack[Count($stack)-1]); $this->assertequals (1,Count($stack)); $this->assertequals (' foo '),Array_pop($stack)); $this->assertequals (0,Count($stack)); }}?>
In the example I use the assertion method assertequals to assert that I am expecting $stack related values
Note that the method highlighted in the code is indexequals, it does not start with test, but I added a @test tag in the comment, then PHPUnit will still run it as a test method, let's run the method and run the result
Go to the command line, use PHPUnit stacktest to perform the stacktest.php test, and run the results as follows:
。。。。
For detailed use, refer to the official manual.
PHPUNIT Unit Test