使用PHPUnit运行多个测试套件

时间:2014-04-25 05:35:31

标签: unit-testing phpunit

在我的phpunit.xml中,我定义了许多<testsuite>,每个{{1}}定义了我要测试的应用程序的不同方面。在开发过程中,我不希望必须运行每个测试套件,只需要处理我正在处理的方面。

但是,当我想测试我的完整应用程序时,我想指定要运行的多个测试套件。有没有办法从命令行执行此操作?

3 个答案:

答案 0 :(得分:4)

您可以使用@group注释来执行此操作。以下是annotations in phpUnit的文档。

您可以通过将@group examplegroup放在您希望加入群组的每个测试类上方的php docblock中来指定测试所属的组。

例如:

<?php
/**
 * @group examplegroup
 *
 */
class StackTest extends PHPUnit_Framework_TestCase
{
    public function testPushAndPop()
    {
        $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));
    }
}
?>

从命令行运行组的工作方式如下:

phpunit --group examplegroup

答案 1 :(得分:2)

如果每个测试套件都有不同的命名空间,则可以使用--filter选项过滤运行的测试。

例如:phpunit --filter '/Controller|Tools/'

将执行具有与正则表达式匹配的命名空间的所有测试。

phpunit options documentation中有更多示例。

答案 2 :(得分:1)

使用最新版本的PHPUnit(从v6.1开始),您只需用逗号(--testsuite <name,...>)分隔套件即可。

考试:phpunit --testdox --testsuite Users,Articles

文档:https://phpunit.readthedocs.io/en/8.0/textui.html?highlight=--testsuite#command-line-options