PHPUNIT Unit Test

Source: Internet
Author: User

Installation on Windows can refer to its manual

Download Phpunit.phar file First

1. Create a directory for the PHP binary executable file, such as C:\bin

2. Add the C:\bin to the system environment variable,

3. Open command-line cmd

4. New batch script, C:\bin\phpunit.cmd

CD C:\bin Echo " %~dp0phpunit.phar " %* > phpunit.cmdexit

After executing the above command at the command prompt, the Phpunit.cmd file is generated in the C:\bin directory

5. After opening a new window, confirm that the phpunit can be executed under any path

  

C:\users\username> PHPUnit--versionphpunit x.y.z by Sebastian Bergmann.

This prompt indicates that the PHPUnit installation was successful.

Ii. Writing PHPUnit Test Cases

To test an array operation with PHPUnit

1. Tests for class classes are written in class Classtest.
2.ClassTest (usually) inherits from Phpunit_framework_testcase.
3.
4. Within the test method, an assertion method similar to Assertequals () (see Appendix A, Assertion) is used to assert the match between the value and the expected value.

Example 2.1. To test an array operation with PHPUnit

<?PHPclassStacktestextendsphpunit_framework_testcase{ Public functionTestpushandpop () {$stack=Array(); $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)); }}?>

In the example I use the assertion method assertequals to assert that I am expecting $stack related values

Note that the method highlighted in the code is indexequals, it does not start with test, but I added a @test tag in the comment, then PHPUnit will still run it as a test method, let's run the method and run the result

Go to the command line, use PHPUnit stacktest to perform the stacktest.php test, and run the results as follows:

。。。。

For detailed use, refer to the official manual.

PHPUNIT Unit Test

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.