php71非侵入使用phpunit

  • 文档 https://phpunit.readthedocs.io/zh_CN/latest/installation.html

  • 安装全局phpunit,7.5.20版本兼容php7.1,变量环境不要忘记 /home/jcleng/.composer/vendor/bin

  • 或者安装到项目里面

composer global require phpunit/phpunit:7.5.20
composer require phpunit/phpunit:7.5.20
  • 项目目录配置phpunit.xml

<!-- composer -->
<!-- <phpunit bootstrap="vendor/autoload.php"> -->
<phpunit>
    <testsuites>
        <testsuite name="service">
            <!-- 测试文件指定文件夹 -->
            <directory>tests</directory>
        </testsuite>
    </testsuites>
</phpunit>
  • 创建phpunit.xml对应的目录,在目录里面创建XxxTest.php文件

<?php

use PHPUnit\Framework\TestCase;

/// 类名使用XxxTest
final class DemoTest extends TestCase
{
    ///方法名使用testXxx
    public function testDemo1(): void
    {
        echo PHP_EOL;
        echo "DemoTest-testDemo1" . PHP_EOL;
        $this->assertSame('testDemo1', 'testDemo1');
    }
    public function testDemo2(): void
    {
        echo PHP_EOL;
        echo "DemoTest-testDemo2" . PHP_EOL;
        $this->assertSame('testDemo2', 'testDemo2');
    }
}
  • 执行phpunit或者/home/jcleng/test/vendor/bin/phpunit即可测试目录里面所有的测试

phpunit

## 输出
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.

.
DemoTest-testDemo1
.                                                                  2 / 2 (100%)
DemoTest-testDemo2

Time: 70 ms, Memory: 4.00 MB

OK (2 tests, 2 assertions)
  • 常见使用

# 查看帮助
phpunit -h
# 执行单个文件
phpunit ./tests/DemoTest.php
# 测试单个方法
phpunit --filter DemoTest::testDemo1
# 使用@author注解,进行分组
phpunit --list-groups
# 执行分组lxx所有的测试
phpunit --group lxx
  • 框架接入

# 使用对应的包

# 获取ide的智能提示,把/home/jcleng/.config/composer/vendor/phpunit 加入到ide目录即可
cp -r /home/jcleng/.config/composer/vendor/phpunit ./.ide/
  • 框架手动接入,配置phpunit.xml,新增命令行,通过命令行调用phpunit

<phpunit>
    <testsuites>
        <testsuite name="service">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
</phpunit>
# 在当前框架新增一个命令行应用
# 如果是tp51,参考文档https://www.kancloud.cn/manual/thinkphp5_1/354146
# 例如创建一个application\common\command\Util.php类

# 完成Util.php类,就可执行命令了
php think unit --filter ConfigTest::testDb
<?php

namespace app\common\command;
# 这里引入一下安装的phpunit包位置,如果不安装到项目的话请单独安装到某一个位置,在这里include_once一下即可
if (file_exists('/mnt/f/test/phpunit/autoload.php')) {
    include_once "/mnt/f/test/phpunit/autoload.php";
}
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Util extends Command
{
    public function configure()
    {
        $this->setName('util')->setDescription('phpunit')->ignoreValidationErrors();
    }

    public function execute(Input $input, Output $output)
    {
        $argv = $_SERVER['argv'];
        array_shift($argv);
        array_shift($argv);
        array_unshift($argv, 'phpunit');
        return (new \PHPUnit\TextUI\Command())->run($argv, false);
    }
}
  • phpunit.xml原文

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>
            <directory suffix=".php">application/</directory>
        </whitelist>
    </filter>
</phpunit>