php - 按特定顺序运行 PHPUnit 测试

标签 php unit-testing phpunit

有没有办法让 TestCase 中的测试按特定顺序运行?例如,我想将对象的生命周期从创建到使用到销毁分开,但我需要确保在运行其他测试之前先设置对象。

最佳答案

PHPUnit 通过 @depends 支持测试依赖。注释。

这是文档中的一个示例,其中测试将以满足依赖关系的顺序运行,每个依赖测试将参数传递给下一个:

class StackTest extends PHPUnit_Framework_TestCase
{
    public function testEmpty()
    {
        $stack = array();
        $this->assertEmpty($stack);

        return $stack;
    }

    /**
     * @depends testEmpty
     */
    public function testPush(array $stack)
    {
        array_push($stack, 'foo');
        $this->assertEquals('foo', $stack[count($stack)-1]);
        $this->assertNotEmpty($stack);

        return $stack;
    }

    /**
     * @depends testPush
     */
    public function testPop(array $stack)
    {
        $this->assertEquals('foo', array_pop($stack));
        $this->assertEmpty($stack);
    }
}

但是,重要的是要注意,具有未解决依赖关系的测试将执行(这是理想的,因为这会迅速引起对失败测试的注意)。因此,在使用依赖项时要密切注意。

关于php - 按特定顺序运行 PHPUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10228/

相关文章:

php - 如何测试验证错误在 laravel 单元测试中抛出确切的错误和消息

php - 获取用户最近的消息

javascript - JS 中的 Cookie 与 PHP 中的 Cookie

asp.net-mvc - 单元测试自定义模型绑定(bind)器 - 假 HttpContext 问题

php - 我应该抛出不同的异常类型吗?

php - 期望使用 PHPUnit 模拟对象的部分数组

php - 如何使用 ODBC+FreeTDS 从 UNIX 中的 PHP 连接到 sybase?

php - 通过php连接数据库

java - 如何模拟一个类,其对象在另一个类的类级别创建

python - 如何将 python 中的日志记录模块与 unittest 模块一起使用?