What is PHPUnit?
1, it is a lightweight PHP test framework, address: http://www.phpunit.cn
2. Manual: Http://www.phpunit.cn/manual/5.7/zh_cn/installation.html
second, why use PHPUnit?
1, you can control the test script by command
2, can test the performance
3. Can test code coverage
4. You can automate the updating of parameter data for test cases
5, Various formats of the log
III. rules for the use of PHPUnit
1, usually after the test class with "test", such as to test the class is an array, the test case is named Arraytest.
2. test class arraytest inherit from Phpunit_framework_testcase
3, the test method must be public permissions, is the beginning of test, or you can also choose to annotate it @test to indicate that the function is a test function
4, the assertion method to assert the actual value and expected value, the assertion method can refer to the manual: http://www.phpunit.cn/manual/5.7/zh_cn/appendixes.assertions.html
Quick Start:
Class Arraytest extends Phpunit_framework_testcase { Initialization of test case before run Public Function SetUp () {} Execute after test case runs Public Function TearDown () {} /** * @test */ Public Function Testarrayisempty () { $fixture = Array (); Asserts that the number of elements in the array $fixture is 0. $this->assertequals (0, sizeof ($fixture)); } /** * @test */ Public Function Testarrayhaskey () { $arr = Array ( ' ID ' =>666, ' Name ' = ' Zhangsan ', ); Assertion $arr is an array $this->asserttrue (Is_array ($arr)); Assertion Array $arr contains the index ID $this->assertarrayhaskey (' id ', $arr); Asserts that the array $arr contains an index name $this->assertarrayhaskey (' name ', $arr); } } |
The command line starts the test case:
PHPUnit test file name, here is the arraytest.php file to test
PHPUnit arraytest.php PHPUnit 5.7.5 by Sebastian Bergmann and contributors. .. 2/2 (100%) time:138 MS, MEMORY:3.25MB OK (2 Tests, 4 assertions) |
Record Test log:
Log multiple formats : Http://www.phpunit.cn/manual/5.7/zh_cn/textui.html#textui.clioptions
PHPUnit arraytest.php--log-tap Log.txt TAP version 13 OK 1-arraytest::testarrayisempty OK 2-arraytest::testarrayhaskey 1..2 |
iv. phpunit test Code Coverage
Refer to: http://www.phpunit.cn/manual/current/zh_cn/code-coverage-analysis.html
You can set the code path or file that you want to test by configuring Phpunit.xml
Example:phpunit.xml configuration (placed in the project root directory)
1. View code coverage by generating HTML pages
By executing the command: PHPUnit--coverage-html./coverage./src/test
Command explanation:
- --coverage-html: Generate HTML for coverage results
- coverage:html build directory, you can redefine
- ./src/test: Test Case Catalog (can also be a single test case file)
Coverage results:
You can see the coverage of each file in the directory:
You can also clearly see each of the file code coverage:
2. View Code coverage through the generated text file
By executing the command: PHPUnit--coverage-text./src/test > Test.log
Command explanation:
- --coverage-html: Generate text for coverage results
- ./src/test: Test Case Catalog (can also be a single test case file)
- > Test.log: file that stores coverage results (the file name is defined by itself)
Coverage results:
You can see the total coverage and the coverage of each file clearly .
Coverage calculation issues:
1, class: Only all the code in the class is executed, the coverage is 100%;
2, Method: The method in the class each row is executed, the coverage is counted 100%. For example, there are 5 methods in the class, and two methods are executed for each line , with coverage of: 40%;
3, line: Each line of code is executed is 100%;
PHPUnit Introduction and use