Installing PHPUnit on a 1.linux server
wget Https://phar.phpunit.de/phpunit.phar
chmod +x Phpunit.phar
sudo mv Phpunit.phar/usr/local/bin/phpunit
Establish phpunit Short command
PHPUnit--version
[Email protected] phpunit_test]# phpunit--versionphpunit 5.6.1 by Sebastian Bergmann and contributors.
2. Create a unit test file
File name is called unittest.php
We can call the function module inside the method of the unit test file, use the data simulation to see if it is running normally, if the general rule will error, break off
<?php class UnitTest extends phpunit_framework_testcase{public function Testpushandpop () { $ stack = array (); $this->assertequals (0,count ($stack)); Array_push ($stack, ' foo '); Asserts whether the value after inserting data into the $stack array is equal to 1 $this->assertequals (1,count ($stack)); } /** * Defines the test label declaration that the method is a testing method * @test ***/public function Indexequals () { $stack = array (1,2,3,4 ); Assert $stack[0] equals 2 $this->assertequals (2, $stack [0]); } }? >
3.phpunit Running Files
[Email protected] phpunit_test]# phpunit unittest.php phpunit 5.6.1 by Sebastian Bergmann and contributors. F 2/2 (100%) time:82 MS, Memory:6.75mbthere is 1 failure:1) unittest::indexequalsfailed asserting that 1 matches E Xpected 2./wwwroot/phpunit_test/unittest.php:18failures! Tests:2, Assertions:3, failures:1.
The results show that the test PHP file runs two modules with a module error
The error test method named Indexequals 18 lines.
Because the stack equals 0 not equal to 1 of the assertion, the error is correct, and the location errors succeed.
PHPUnit Unit Test