PHPUnit扩展测试套件

3
我扩展了PHPUnit_Framework_TestSuite以覆盖setUp和tearDown方法,因为我需要在测试套件之前和之后进行一些操作。 (测试在多个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() - Maciej Sz
因为它们是完全不同类型的测试,出于逻辑原因,我不想将它们放在同一个类中。遗憾的是,它们都需要相同的 "setUpBeforeClass" 和 "tearDownAfterClass"。这就是为什么我正在使用测试套件。如果您查看 phpunit 的代码本身 https://github.com/sebastianbergmann/phpunit/blob/master/src/Framework/TestSuite.php,测试套件类没有这两个方法... 但是,如果您查看 setUp 和 tearDown 上的注释,您会得到:"在运行此测试套件的测试之前调用的模板方法。" - holygrinder
1个回答

1

https://phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.testsuites中有以下信息:

<testsuites>元素及其一个或多个<testsuite>子元素可以用于组合由测试套件和测试用例组成的测试套件。

我在Magento中使用Ecomdev_PHPUnit,它会像您想要的那样进行子类化,请查看其xml:

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

所以,我猜只需要传递测试套件的完整路径!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接