【问题标题】:phpunit extend test suitephpunit 扩展测试套件
【发布时间】: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


【解决方案1】:

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

元素及其一个或多个 子元素可以 用于从测试套件和测试用例组成测试套件。

我在 Magento 中使用Ecomdev_PHPUnit,它会像你想要的那样子类化,看看它的 xml:

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

所以我猜只要通过测试套件完整路径!

【讨论】:

    猜你喜欢
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 2012-10-19
    • 2016-05-24
    • 1970-01-01
    相关资源
    最近更新 更多