PHP unit tests use

Unit testing refers to the inspection and verification of the smallest testable unit of software. For the meaning of a unit in unit testing, generally speaking, it is necessary to determine its specific meaning according to the actual situation. For example, a unit in C language refers to a function, a unit in Java refers to a class, and graphical software can refer to a window or a menu. Wait. In general, a unit is the smallest human-defined functional module under test. Unit testing is the lowest level of testing activity to be performed during software development, where individual units of software are tested in isolation from the rest of the program.

Unit testing is done by the programmers themselves, and the programmers themselves benefit in the end. It can be said that programmers are responsible for writing functional code, but also for writing unit tests for their own code. Unit tests are performed to prove that this code behaves as we expect it to.

Here's how to configure PHP unit tests

 

Configuration instructions

1. Install the phpunit command script globally

$ wget https://phar.phpunit.de/phpunit-7.0.phar
$ chmod +x phpunit-7.0.phar
$ sudo mv phpunit-7.0.phar /usr/local/bin/phpunit
$ phpunit --version
PHPUnit x.y.z by Sebastian Bergmann and contributors.

2. Install phpunit code globally

composer global  require phpunit/phpunit

3. Create phpunit.xml and put it in the root directory of your project. This file is a configuration file that phpunit will read by default:

<phpunit bootstrap="vendor/autoload.php">
    <testsuites>
        <testsuite name="service">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

4. Configure the phpstorm unit phpunit.phar path, Languages ​​& Frameworks > PHP > PHPUinit

5. Configure the unit test class prompt, Languages ​​& Frameworks > PHP > include path

6. Writing unit tests

1. The test class whose Class is Demo is DemoTest

2. The test class inherits from PHPUnit\Framework\TestCase

3. Test method

  • Must be public permission,
  • It usually starts with test, or you can annotate it with @test to identify it
  • Within a test method, assertion methods like assertEquals() are used to assert that the actual value matches the expected value.
<?php
use Eoffcn\Utils\Arrays;
use PHPUnit\Framework\TestCase;
/**
 * Array test case
 * Class ArraysTest
 */
class ArraysTest extends TestCase
{
    public function testGet()
    {
        $array = [
            1 => [
                'b' => [
                    'c' => 'cqh'
                ]
            ],
            2 => [
                'b' => [
                    'c' => 'cqh'
                ] ]
        ];
        $this->assertEquals('cqh', Arrays::get($array, '1.b.c'));
    }
}

 

Execute unit tests   

1. Execute single file unit tests

Phpstorm method, the current test class can be run by right-clicking

command line like

phpunit tests/ArraysTest.php

  

 

2. Execute global unit tests

phpstorm way

 

Command line mode, enter the current project execution under the command line

phpunit

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325187400&siteId=291194637