使用PHPUnit运行多个测试套件

问题描述 投票:0回答:3

在我的phpunit.xml中,我有一些<testsuite>的定义,每个定义了我的应用程序的不同方面进行测试。在开发过程中,我不希望必须运行每个测试套件,只有我正在研究的方面。

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

unit-testing phpunit
3个回答
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


2
投票

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

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

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

你在phpunit options documentation中有更多的例子。


1
投票

使用最新版本的PHPUnit(从v6.1开始),您可以使用逗号(qazxsw poi)简单地分隔套件。

示例:qazxsw poi

Doc:--testsuite <name,...>

© www.soinside.com 2019 - 2024. All rights reserved.