First of all, we need to understand the concept of unit testing, unit testing refers to the software in the basic unit test, such as functions, methods, to check whether their return value or behavior is expected, but is actually very responsible, because of a number of components, the execution process is coherent, to test the unit fragment, It is necessary to provide the execution context (or arguments) and the environment (such as piling simulation objects) to run.
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 @test annotations in the document comment block (DocBlock) of a method to mark it as a test method.
4. Within the test method, an assertion method similar to Assertequals () 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.
Through the above content to cooperate with official website document, Novice should be able to get started smoothly
Believe that you have seen these cases you have mastered the method, more wonderful please pay attention to the PHP Chinese network other related articles!
Related reading:
PHP deployment Environment using GIT
PHP custom functions Generate Cartesian product
PHP Commodity second Kill timing implementation (to resolve large traffic scenarios)