Address of this article
Share an outline:
1. Overview
2. Installation
3. Writing the first test case
4.PHPUnit Advanced
5. Reference
1. Overview
1) "Testing Framework"
It is a lightweight PHP test framework and is a Xunit architecture unit testing Framework. Complex projects, through unit testing can quickly troubleshoot bugs, effectively reduce the production of bugs. Simple project, using PHP's own var_dump() , print_r() can also be very convenient debugging bugs. PHPUnit checks that the code conforms to expectations by running the assertions in the test case, such as determining that the returned result is not empty.
2. Installation
There are two ways of installing it. One is to use the Phar package, and one is to use composer.
For installation under Mac, refer to http://blog.csdn.net/u013166311/article/details/52622560
(You can download the lower version of PHPUnit wget Https://phar.phpunit.de/phpunit-4.0.14.phar)
Then open the terminal, first pear version to verify whether the pear is installed. If you do not install $sudo php/usr/lib/php/install-pear-nozlib.phar-d/usr/local/lib/php-b/usr/local/bin with $ pear version to detect the installation success sudo pear upgrade-all update. Finally, install success with the following command: $ wget https://phar.phpunit.de/phpunit-4.0.14.phar$ chmod +x phpunit.phar$ sudo mv phpunit.phar/usr/local/ Bin/phpunit
Install under Mac
2.1) using the Phar Package
The latest version of the stable version of the Phar package is 5.7, Https://phar.phpunit.de/phpunit.phar
phar5.7 was released on December 2, 2016 and ended on February 2, 2018.
PHPUnit 5.7 supports PHP 5.6, PHP 7.0, and PHP 7.1.
Note: PHP version is relatively low, you can download the lower version of the phpunit, such as wget Https://phar.phpunit.de/phpunit-4.0.14.phar
How to run:
--version# linuxchmod +x phpunit.pharsudomv phpunit.phar/usr/local/ bin/--version
You can view the version number.
2.2) Use composer
If you use Composer to manage a project's dependencies, simply add the dependency to the project's Composer.json file phpunit/phpunit . The following is an example of a minimized composer.json file that defines only one pair PHPUnit 5.7 of development-time dependencies:
{" Require-dev": { "phpunit/phpunit": "5.5.*" }}
To Composer complete a system-level installation, you can run:
Composer global require "phpunit/phpunit=5.5.*"
Make sure that the path variable contains a ~/.composer/vendor/bin/ .
3. Writing the first test case
3.1) Create a new folder Testcase and write sayhello.php:
<? class sayhello{ publicfunction Printhello () { echo ' Hello '; return ' Hello '; }? >
sayhello.php
3.2) New test case sayhellotest.php
<?PHPrequire_once' Sayhello.php '; classSayhellotestextendsPhpunit_framework_testcase { Public functionsetUp () {} Public functionTearDown () {} Public functionTestconnectionisvalid () {$hi=NewSayHello (); $this->asserttrue ($hi->printhello () = = ' Hello '); } }sayhellotest.php
When you are finished writing, switch to the directory where Phpunit.phar command line executes:
$ php Phpunit.phar testcase/sayhellotest.php
Output Result:
PHPUnit 5.7.4 by Sebastian Bergmann and contributors . 1/1 (100%) hellotime:130 MS, Memory:10.00mbok (1 test, 1 assertion)
The results show that:
Test passes, 1 test methods, 1 assertions, no failures.
Here are the notes:
1. All classes ending with test are test cases;
2, all the methods beginning with test are test methods, will automatically run;
3, Setup is the first method to run each test case, teardown is the last method to run each test case;
4, assertTrue used to determine whether the result is true.
4.PHPUnit Advanced
4.1) Mock object Mock
What is the deep-seated principle of mock-up? The method in class Phpunit_framework_mockobject_generator Getmock () detects a mock object in PHPUnit, and when the object to be replaced is instantiated by a class, it is replaced with a mock object, and then Use the mock method to put back the value of the object. --"Baidu encyclopedia--Define mock" mock in each language implementation--reference: PHPUnit Learning---Using mock objects to resolve test dependencies
5. Reference
5.0) "Good" PHPUnit Handbook
5.1) Start using the phpunit–php test framework
5.2) web3d/tpunit:thinkphp PHPUnit Framework Integration
5.3) [Php]phpunit installation configuration and sample | Coinidea's Technology Sharing blog
5.4) "XUnit Test Patterns" study notes Series-Coderzh-Blog Park
5.5) "Good" PHPUnit notes
"Tamping PHP Basics" PHPUnit--PHP test framework