【发布时间】:2014-12-13 10:04:18
【问题描述】:
我扩展了 PHPUnit_Framework_TestSuite 以覆盖 setUp 和 tearDown 方法,因为我需要 PHPUnit 在一组测试之前和之后执行一些操作。 (测试在多个TestCase类中。)
class MyTestSuite extends PHPUnit_Framework_TestSuite {
protected function setUp()
{
//do some stuff before all tests are run
}
protected function tearDown()
{
//do some stuff after all tests are run
}
}
如何在 xml 配置文件中告诉 phpunit 使用这个 TestSuite 类,然后将 testCase 类绑定到它?我能找到的只是这样的例子,它似乎没有指定 phpunit 应该使用哪个测试套件类。
<phpunit>
<testsuites>
<testsuite name="money">
<file>tests/IntlFormatterTest.php</file>
<file>tests/MoneyTest.php</file>
<file>tests/CurrencyTest.php</file>
</testsuite>
</testsuites>
</phpunit>
【问题讨论】:
-
为什么不能简单地在特定的测试用例中扩展你的测试套件?例如。
IntlFormatterTest extends MyTestSuite。请注意,setUp()和tearDown()方法应在 每个 测试方法之前和之后运行。也许你需要的是setUpBeforeClass()和tearDownAfterClass()。 -
因为它们是完全不同类型的测试,出于逻辑原因,我不想将它们放在同一个类中。可悲的是,他们都需要相同的“setUpBeforeClass”和“tearDownAfterClass”。所以这就是我使用测试套件的原因。如果您查看 phpunit github.com/sebastianbergmann/phpunit/blob/master/src/Framework/… 的代码本身,则测试套件类没有这两种方法......但是如果您查看 setUp 和 tearDown 上的注释,您会得到:“在此测试的测试之前调用的模板方法套件正在运行。”
标签: php phpunit test-suite