php Unit Testing phpunit Getting Started Example Tutorial

  • 2021-08-16 23:29:40
  • OfStack

This article illustrates the php unit test phpunit. Share it for your reference, as follows:

This article provides some phpunit official tutorial does not mention the information, help beginners quickly understand php unit testing, in phpunit official website provides a detailed Chinese tutorial, can choose a variety of formats to download

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

What is a unit test

Refers to testing the basic units in software, such as functions, methods, etc., to check whether their return values or behaviors meet expectations; In practice, software is very complicated, Composed of many components, The execution process is coherent in one case, To test the unit fragment, You need to provide it with execution context (or parameters) and environment (such as piling simulation objects) to run, And monitor its behavior and return value. For this reason, we need to write the program code to do this. Such code is called test cases. Many test cases are organically combined to form a whole test, which is also called test suite. The tested program code is called production code. phpunit is designed to help us write test cases and test them.

Unit Test for php: phpunit

There is a detailed tutorial in its official website, and here are some supplementary contents.

First of all, understand that phpunit software itself is implemented by php language, and its use is carried out through command line, not through browser access

It is important to understand this point. Many newcomers are stuck here. It's basic and simple, but it doesn't seem to be mentioned much

After downloading in official website, I got an phar file. For the introduction of phar archive file, please refer to the archive format developed by PHP in the previous article. Concept and usage of phar file

Please also download the phpunit usage documentation, install it according to the installation method inside, and then enter the following command on the command line:


phpunit --help

If the help information is displayed, that is, the installation is successful, establish a test case in the current directory of the command line as yunke. php, which reads as follows:


<?php
use 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 on the command line:


phpunit yunke

Shows the following:


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

Congratulations, you have successfully run a unit test, and the first line is the author information (Sebastian Bergmann, this guy likes to sign it very much)

Line 2 begins with a period to indicate that all tests have passed successfully

You may wonder, what happened to the php code above?

That is a test case, which simply tests an array operation and performs unit test 1 through the following four steps:

1. Tests for the production code class Class are written in the class ClassTest.
2. ClassTest (typically) inherits from PHPUnit\ Framework\ TestCase.
3. Tests are common methods named test*. It can also be used in the document comment block (docblock) of the method @test Annotation marks it as a test method.
4. Within the test method, similar to assertEquals() This assertion method is used to make assertion judgment on the match between actual value and expected value.

Here you may be confused in several places:

1. The annotation used in the annotation block, php can be obtained by reflection, and the program can use the obtained information to configure
2. Where does the TestCase class in the test case code above come from? It's not loaded?
The above mentioned phpunit itself is written in php language, packaged into phar for use, phar is executable, the first implementation of the package inside the stub file stub

You can extract and restore the phpunit. phar package according to the method described in another phar post of Yunke above, or open it directly by using ide such as phpstorm

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

You can see that the program didn't start with the test case script. Now you can see where the TestCase class came from

Through the above content and official website documents, novices should be able to get started smoothly

For more readers interested in PHP related content, please check the topics on this site: "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Introduction to php Object-Oriented Programming", "Summary of PHP Network Programming Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: