This article mainly introduced the PHP unit test PHPUnit, combined with the instance form Analysis PHP Unit Test phpunit Basic concept, the use method and the related attention matters, the need friend can refer to the next
Specific as follows:
What is unit testing:
Refers to the basic unit of software testing, such as functions, methods, etc., to check whether the return value or behavior is expected; In practice, software is very complex, composed of many components, the execution process is coherent, to test the unit fragment, It is necessary to provide the execution context (or parameters) and environment (such as piling simulation objects) to run, and monitor its behavior and return values, so we need to write the program code to do this, such code is called test cases, many test cases organically together to form a whole test, Also called the test suite, the program code being tested is called the production Code. PHPUnit This software is designed to help us write test cases and test them.
Unit test for PHP: PHPUnit
There is a detailed tutorial on its website, here are some supplemental content.
The first thing to understand is that the PHPUnit software itself is implemented by the PHP language, and its use is done through the command line, not through browser access
It's important to understand that many new people are stuck here. It's very basic and simple, but it doesn't seem to be mentioned.
After downloading the website to get a Phar file, the introduction of Phar Archive file can be found in the previous article PHP development of the archive format Phar file concept and usage
Please also download phpunit using the documentation, follow the installation method installed, and then enter the following command at the command line:
PHPUnit--help
If the help message is displayed and the installation succeeds, create a test case for yunke.php at the command line current directory, as follows:
<?phpuse Phpunit\framework\testcase;class Yunketest extends testcase{public function Testpushandpop () { $stack = []; $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));} }? >
Then enter the following command at the command line:
PHPUnit Yunke
Displays the following:
PHPUnit 5.7.12 by Sebastian Bergmann and contributors . 1/1 (100%) time:159 MS, Memory:7.00mbok (1 Test, 5 assertions)
Congratulations, you have successfully run a unit test, the first line is the author information (Sebastian Bergmann this guy likes to sign it.)
The second line begins with a period indicating that all tests successfully pass
You may wonder what's going on with the PHP code above?
That is a test case, a simple test of an array operation, unit testing generally through the following four steps:
1. The test for the class of production code is written in the class Classtest.
2. Classtest (usually) inherits from Phpunit\framework\testcase.
3. The test is a common method named test*. You can also use annotations in the document comment block (DocBlock) of a method to @test
mark it as a test method.
4. Within the test method, assertEquals()
an assertion method similar to this is used to assert the match between the actual value and the expected value.
Here you may have a few places to confuse:
1, the annotations used in the comment block, PHP can be reflected, the program can use the information obtained to configure
2. Where is the TestCase class from the test case code listed above? It's not loaded, is it?
As mentioned above, PHPUnit itself is written in PHP language, packaged into Phar for use, Phar is executable, execution first executes the stub file stub inside the package.
You can extract the restore Phpunit.phar package according to another Oberwon Phar Post, or use an IDE like Phpstorm to open it directly
In. phar/stub.php you will see the stub code, the TestCase class is require in the stub code "PHPUnit yunke" This command line code will first run PHPUnit script, starting from the stub file execution, The test case code is then loaded by PHPUnit
You can see that the program is not executed from the test case script, and now you know where the TestCase class comes from.
Related recommendations:
How to install PHPUnit in the CentOS environment
Implementation of PHPUnit interface Automation test function
Case study YAF Framework PHPUnit Integration test method