Php unit test phpunit getting started instance tutorial, unit test phpunit

Source: Internet
Author: User

Php unit test phpunit getting started instance tutorial, unit test phpunit

This article describes the php unit test phpunit. We will share this with you for your reference. The details are as follows:

This article provides some information not mentioned in the official phpunit tutorials. It helps beginners quickly learn about php unit testing and provides detailed Chinese tutorials on the phpunit official website, which can be downloaded in multiple formats.

PHP unit official website address: https://phpunit.de/

What is unit test:

Testing the basic unit in the software, such as functions and methods, to check whether the return value or behavior meets expectations. In reality, the software is very complex and consists of many components, the execution process is coherent. To test the unit fragment, you need to provide the execution context (or parameters) and environment (such as piling up to simulate some objects) for it to run, it also monitors its behavior and return values. Therefore, we need to write the program code for doing this. Such code is called a test case. Many test cases are combined to form a complete test, it is also called the test suite. The tested program code is called the production code. Phpunit is used to help us write test cases and perform tests.

Php unit test: phpunit

There is a detailed tutorial on its official website. Here we will introduce some additional content.

First of all, you must understand that the phpunit software is implemented by the php language. It is used through the command line and is not accessed through a browser.

It is important to understand that many new people are stuck here. Very basic and very simple, but it does not seem to be mentioned.

Download a phar file from the official website. For details about the phar archive file, refer to the previous article on the concept and usage of phar file in archive format developed by PHP.

Download the phpunit User Guide and install it according to the installation method. Then, run the following command on the command line:

phpunit --help

If the help information is displayed, the installation is successful. Create a test case named yunke. php in the current directory of the command line. The content is as follows:

<?phpuse PHPUnit\Framework\TestCase;class yunkeTest extends TestCase{  public function testPushAndPop()  {    $stack = [];    $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));  }}?>

Then enter the following command in the command line:

phpunit yunke

The following content is displayed:

PHPUnit 5.7.12 by Sebastian Bergmann and contributors..                                  1 / 1 (100%)Time: 159 ms, Memory: 7.00MBOK (1 test, 5 assertions)

Congratulations, you have successfully run a unit test. The first line is the author's information (this guy in Sebastian Bergmann is fond of signing)

The second line starts with a period, indicating that all tests have passed successfully.

You may wonder, what is the above php code?

This is a test case. We test an array operation in a simple way and perform unit tests in the following four steps:

1. Test the production code Class in the Class ClassTest.
2. ClassTest (usually) inherits from PHPUnit \ Framework \ TestCase.
3. test is a public method named test. You can also use it in the docblock of the method.@test Mark it as a test method.
4. the test method is similarassertEquals()This assertion method is used to determine whether the actual value matches the expected value.

Here you may be confused about the following:

1. The annotation used in the annotation block can be obtained through reflection by php, and the program can configure it using the obtained information.
2. Where does the TestCase class in the test case Code listed above come from? Not loaded yet?
As mentioned above, phpunit is written in php language and packaged into phar for use. phar is executable. When executed, execute the stub file stub in the package first.

You can extract and restore the phpunit. phar package based on the method described in another phar post of Alibaba Cloud, or directly open it using an ide such as phpstorm.

In. phar/stub. in php, you will see the stub code. The TestCase class is the "phpunit yunke" command line code in the require code. The command line code will first run the phpunit script and start execution from the stub file, then, phpunit loads the test case code.

It can be seen that the program is not executed from the test case script. Now I understand where the TestCase class comes from.

With the above content and the official documentation, the beginner should be able to get started smoothly.

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.