PHPUnit Getting Started

Source: Internet
Author: User
Tags composer install

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.