多次运行PHPUnit测试用例

时间:2013-08-15 13:15:51

标签: php phpunit testcase

我正在寻找一种如何使用不同的设置多次运行测试用例的方法。

我正在测试数据库访问类(几十种测试方法),并希望在“正常模式”下测试它,然后在“调试模式”下测试它。两种模式都必须产生相同的测试结果。

在测试用例设置中是否有可能这样做?或者重写run()方法? 我当然不想写两次测试:)

谢谢

编辑:GOT IT!

public function run(PHPUnit_Framework_TestResult $result = NULL)
{
    if ($result === NULL) {
        $result = $this->createResult();
    }

    /**
     * Run the testsuite multiple times with different debug level
     */
    $this->debugLevel = 0;
    print "Setting debug level to: " . $this->debugLevel . PHP_EOL;
    $result->run($this);

    $this->debugLevel = 8;
    print "Setting debug level to: " . $this->debugLevel . PHP_EOL;
    $result->run($this);

    $this->debugLevel = 16;
    print "Setting debug level to: " . $this->debugLevel . PHP_EOL;
    $result->run($this);

    return $result;
}

public function setUp()
{
    parent::setUp();
    $this->myclass->setOptions('debug', $this->debugLevel);
}

3 个答案:

答案 0 :(得分:1)

我认为斯文的答案是正确的。写下您要设置的内容,并提供所需的测试用例参数,例如:

/**
 * @dataProvider forMyTest
 */
public function testWithDifferentDbType($type, $param1, $param ....)
{
    $this->assertExists(....);
}

public function forMyTest() {
    return [
        [
            true
            [$prodDb, $param1, $param, .....],
        ],
        [   
            false,
            [$debugDb, $param1 $param, ....,

        ],
    ];
}

答案 1 :(得分:0)

您可以在单元测试中创建多种方法。所有这些都将运行,并在每个测试方法运行之后调用setUp方法。

答案 2 :(得分:-1)

PHPUnit提供测试装饰器。 documentation实际上有重复的装饰器作为如何处理装饰器的例子。装饰器将是以可重用的方式实现行为的完美方式,而不依赖于子类PHPUnit_Framework_TestCase

相关问题