In other words, the use of phpunit or curiosity, why has not been used before? The main is too troublesome, small projects, both front and back are written by themselves, a few people on the completion of the work, no need to get so troublesome. Well, don't say much, let's feel the next phpunit!
Suppose that we have a project Demo,demo is using composer to do the dependency management. Then we only need the following steps to complete the installation and use of PHPUnit:
First, PHPUnit installation:
1. Download PHPUnit:wget https://phar.phpunit.de/phpunit.phar
2, modify the permissions to download the file:chmod +x phpunit.phar
3. Set PHPUnit to global variables:mv phpunit.phar /usr/local/bin/phpunit
4. View PHPUnit version: Phpunit-v
Second, the project demo introduced PHPUnit components:
1. Open the Composer.json file, add "Require-dev": {"phpunit/phpunit": "5.7.*"}, then composer install (equivalent to composer require " phpunit/phpunit:5.7.* ")
2. After completing Step 1, the PHPUnit component will be present in the Vendor/phpunit/phpunit
3. phpunit file directory:
├──phpunit. xml├──src│ ├──autoload. php│ └──phpunitclass. php└──tests └──phpunitclasstest. php
4, Phpunit.xml as the core configuration file, not introduced, the default can be
5, the SRC store the real service class, of course, generally we will put the project's service class to other directories
6, tests store all the test class, all test classes must inherit phpunit\framework\testcase; All test class names are best *test.php; all methods in the test class must be test*
7, a simple example of phpunit:
Service class, Phpunitclass:
namespace Appservice; class phpunitclass{ private$att; Public function __construct ($at) { echo "Phpunitclass init"; $this->att=$at; } Public function say () { return "say"; }}
Unit test class, Phpunitclasstest:
classPhpunitclasstestextends\phpunit\framework\testcase{ Public functionTestphpunitclassinit () {$unit=New\appservice\phpunitclass ("Init"); $this->assertattributeequals ("Init", "ATT",$unit); } Public functionTestphpunitclasssay () {$unit=New\appservice\phpunitclass ("Init"); $this->assertequals ("Say",$unit-say ()); }}
8, CD into the directory tests, the terminal execution: PHPUnit phpunitclasstest can perform unit testing
Above, is a phpunit simple summary of the introduction, convenient for later review.
The Laravel project comes with PHPUnit, and all the test classes are stored under the root directory tests/.
PHPUnit Getting Started