dra*_*ank 57 php phpunit unit-testing
有没有办法让a内部的测试TestCase
按特定顺序运行?例如,我想将对象的生命周期从创建分为使用分离,但我需要确保在运行其他测试之前首先设置对象.
mjs*_*mjs 137
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);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,重要的是要注意,将不会执行具有未解析的依赖关系的测试(这是可取的,因为这会引起对失败测试的快速关注).因此,在使用依赖项时要特别注意.
Fab*_*mes 48
也许你的测试中存在设计问题.
通常每个测试都不能依赖于任何其他测试,因此它们可以按任何顺序运行.
每个测试都需要实例化并销毁它需要运行的所有东西,这将是完美的方法,你永远不应该在测试之间共享对象和状态.
您能否更具体地说明为什么N测试需要相同的对象?
Gin*_*ane 17
对此的正确答案是用于测试的正确配置文件.我遇到了同样的问题并通过创建具有必要测试文件顺序的testsuite来修复它:
phpunit.xml:
<phpunit
colors="true"
bootstrap="./tests/bootstrap.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
strict="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
stopOnRisky="false"
>
<testsuites>
<testsuite name="Your tests">
<file>file1</file> //this will be run before file2
<file>file2</file> //this depends on file1
</testsuite>
</testsuites>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
25632 次 |
最近记录: |