2014-10-17 1366 views
3

我扩展了PHPUnit_Framework_TestSuite来覆盖setUp和tearDown方法,因为我需要PHPUnit在一组测试之前和之后执行一些操作。 (测试是在多个TestCase类。)phpunit扩展测试套件

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> 
+0

为什么你不能简单地在特定的*测试用例*中扩展你的测试套件?例如。 'IntlFormatterTest扩展MyTestSuite'。请注意,'setUp()'和'tearDown()'方法是为了在每个* test方法之前和之后运行。也许你需要的是'setUpBeforeClass()'和'tearDownAfterClass()'。 – 2014-10-17 21:16:03

+0

因为它们是完全不同类型的测试,我不想为了逻辑原因而把它们放在同一个类中。可悲的是,他们都需要相同的“setUpBeforeClass”和“tearDownAfterClass”。所以这就是我使用测试套件的原因。如果你看看phpunit的代码本身,https://github.com/sebastianbergmann/phpunit/blob/master/src/Framework/TestSuite.php测试套件类没有这两种方法...但是如果你看您对setUp和tearDown发表了评论:“在测试套件的测试运行之前调用的模板方法。” – holygrinder 2014-10-19 16:59:43

回答

0

https://phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.testsuites有这样的信息:

的<测试包>元素及其一个或多个<测试包>孩子可以 被用于组成一个测试套件出测试套件和测试用例。

我用Ecomdev_PHPUnit在Magento,这确实子类像你想,看看它的xml:

<testsuite name="Magento Test Suite"> 
    <file>app/code/community/EcomDev/PHPUnit/Test/Suite.php</file> 
</testsuite> 

所以只是通过我想测试套件的完整路径!