【问题标题】:Order of test cases execution in PHPUnitPHPUnit中测试用例的执行顺序
【发布时间】:2014-05-15 03:40:08
【问题描述】:

我已经在phpunit.xml中定义了需要按顺序执行的Test文件列表。

根据http://phpunit.de/manual/3.7/en/organizing-tests.html#organizing-tests.xml-configuration 提到,测试用例的顺序可以定义为:

<phpunit>
  <testsuites>
    <testsuite name="Object_Freezer">
      <file>Tests/Freezer/HashGenerator/NonRecursiveSHA1Test.php</file>
      <file>Tests/Freezer/IdGenerator/UUIDTest.php</file>
      <file>Tests/Freezer/UtilTest.php</file>
      <file>Tests/FreezerTest.php</file>
      <file>Tests/Freezer/StorageTest.php</file>
      <file>Tests/Freezer/Storage/CouchDB/WithLazyLoadTest.php</file>
      <file>Tests/Freezer/Storage/CouchDB/WithoutLazyLoadTest.php</file>
    </testsuite>
  </testsuites>
</phpunit>

但似乎 PHPUnit 忽略了 phpunit.xml 中的配置并按字母顺序执行测试用例。

我想定义顺序只是因为我想在最后执行我的一个测试用例。 以下是我的代码:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit 
    colors="true" 
    stopOnFailure="false" 
    forceCoversAnnotation="true"
    bootstrap="../application/third_party/CIUnit/bootstrap_phpunit.php">
    <testsuites>
        <testsuite name="ModelTests">
            <directory suffix=".php">models</directory>
            <file>./models/aa.php</file>
            <file>./models/bb.php</file>
            <file>./models/cc.php</file>
            <file>./models/dd.php</file>
            <file>./models/ab.php</file>
            <file>./models/ac.php</file>
        </testsuite>
    </testsuites>
    <filter>
    <whitelist addUncoveredFilesFromWhitelist="true">
        <directory suffix=".php">models</directory>
    </whitelist>
    </filter>
</phpunit>

当我执行 phpunit 时,它会打印: 从 /var/www/builds/latest/tests/phpunit.xml 读取配置

有什么想法吗?

【问题讨论】:

  • 你确定配置文件已经加载了吗? (尝试打破它,看看你是否得到错误)
  • 是的,当我执行 phpunit 时,它会打印:从 /var/www/builds/latest/tests/phpunit.xml 读取的配置

标签: php phpunit


【解决方案1】:

您需要告诉 phpunit 使用测试套件,以便它以正确的顺序运行测试。我猜你只是在使用phpunit . 或类似的东西来运行测试。在这种情况下,PHPUnit 会加载测试并以非常随机的方式运行它们。为了使用您指定的顺序,您需要执行 phpunit --testsuite ModelTest 这告诉 PHPUnit 使用套件中指定的顺序。

但是,它也只会运行那里的 XML 中列出的测试,因此请确保您的列表是全面的。

此外,正如 cmets 中的链接答案中所述,要求您的测试按特定顺序运行并不是理想的情况。您应该尝试更改您的设计或测试,以便您不依赖于它们以特定顺序运行。

【讨论】:

  • @DShah 你运行的是什么版本的 PHPUnit?该命令适用于 3.7
  • 版本是 3.6.10,我会再次检查 3.7。实际上我已经使用 sudo apt-get install phpunit 在 ubuntu 中安装了 PHPUnit。并且仅安装 3.6.10 版本。
猜你喜欢
  • 2015-06-15
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-23
  • 2020-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多