如何运行特定的 phpunit xml 测试套件?

发布于 2024-09-18 11:10:38 字数 461 浏览 5 评论 0原文

我如何选择要执行的特定测试套件?

$ phpunit --配置config.xml

config.xml:

<testsuites>
    <testsuite name="Library">
        <directory>library</directory>
    </testsuite>
    <testsuite name="XXX_Form">
        <file>library/XXX/FormTest.php</file>
        <directory>library/XXX/Form</directory>
    </testsuite>
</testsuites>

how can i choose a specific testsuite to be executed?

$ phpunit --configuration config.xml

config.xml:

<testsuites>
    <testsuite name="Library">
        <directory>library</directory>
    </testsuite>
    <testsuite name="XXX_Form">
        <file>library/XXX/FormTest.php</file>
        <directory>library/XXX/Form</directory>
    </testsuite>
</testsuites>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你的背包 2024-09-25 11:10:38

这里的代码就像 PHPUnit 3.7.13

$ phpunit --configuration config.xml --testsuite Library
$ phpunit --configuration config.xml --testsuite XXX_Form

如果你想运行一组测试套件那么你可以这样做

<testsuites>
  <testsuite name="Library">
    <directory>library</directory>
  </testsuite>
  <testsuite name="XXX_Form">
    <file>library/XXX/FormTest.php</file>
    <directory>library/XXX/Form</directory>
  </testsuite>
  <testsuite name="Both">
    <directory>library</directory>
    <file>library/XXX/FormTest.php</file>
    <directory>library/XXX/Form</directory>
  </testsuite>
</testsuites>

然后

$ phpunit --configuration config.xml --testsuite Both

不幸的是 PHPUnit 目前不支持这样的嵌套测试套件

<testsuites>
    <testsuite name="Both">
      <testsuite name="Library">
        <directory>library</directory>
      </testsuite>
      <testsuite name="XXX_Form">
        <file>library/XXX/FormTest.php</file>
        <directory>library/XXX/Form</directory>
      </testsuite>
  </testsuite>
</testsuites>

所以如果你想以这种方式运行一组测试套件你有有xml配置重复!

Here's the code as if PHPUnit 3.7.13

$ phpunit --configuration config.xml --testsuite Library
$ phpunit --configuration config.xml --testsuite XXX_Form

If you want to run a group of the test suites then you can do this

<testsuites>
  <testsuite name="Library">
    <directory>library</directory>
  </testsuite>
  <testsuite name="XXX_Form">
    <file>library/XXX/FormTest.php</file>
    <directory>library/XXX/Form</directory>
  </testsuite>
  <testsuite name="Both">
    <directory>library</directory>
    <file>library/XXX/FormTest.php</file>
    <directory>library/XXX/Form</directory>
  </testsuite>
</testsuites>

Then

$ phpunit --configuration config.xml --testsuite Both

Unfortunately PHPUnit currently does not support nested testsuites like this

<testsuites>
    <testsuite name="Both">
      <testsuite name="Library">
        <directory>library</directory>
      </testsuite>
      <testsuite name="XXX_Form">
        <file>library/XXX/FormTest.php</file>
        <directory>library/XXX/Form</directory>
      </testsuite>
  </testsuite>
</testsuites>

So if you wanted to run groups of test suites this way you have to have xml configuration duplication!

飘然心甜 2024-09-25 11:10:38

这在当前版本的 PHPUnit 中是不可能的,正如 phpunit-user 邮件列表中的这些消息所证明的那样: http://thread.gmane.org/gmane.comp.php.phpunit.user/1302

但是还有一种替代方法,您可以简单地将路径传递给 phpunit。

phpunit library/XXX

这将运行library/XXX目录中的所有测试

如果这对您来说还不够,另一个选择是使用@group 注释 将测试分为不同的类别,然后可以有选择地运行。

This is not possible in current versions of PHPUnit as evidenced by these messages in phpunit-user mailing list: http://thread.gmane.org/gmane.comp.php.phpunit.user/1302

But there is an alternative, you can simply pass a path to phpunit.

phpunit library/XXX

This would run all the tests in library/XXX directory

If this is not sufficient for you, another option is to use the @group annotation to divide tests into different categories that could then be run selectively.

阪姬 2024-09-25 11:10:38

从 phpunit 6.1 开始,您可以在 xml 配置文件中使用属性 defaultTestSuite,这就像使用默认选项 phpunit --testsuite xxx 一样,并且会被覆盖。

As of phpunit 6.1 you can use in the xml config file the attribute defaultTestSuite, this is like using a default option phpunit --testsuite xxx and is overriden.

你丑哭了我 2024-09-25 11:10:38

另一种选择是为您想要单独测试的每个测试套件创建一个单独的配置文件。您可能需要复制/粘贴重复的设置,因此会产生一些开销,但您可以根据需要指定每个配置文件。

Another option is to create a separate config file for each testsuite you would like to test separately. There is some overhead in that you may have to copy/paste duplicate settings, but you could then specify each config file as needed.

醉南桥 2024-09-25 11:10:38

这里的其他答案是正确的。你不能使用 xml 配置来做到这一点,但你可以做的是在 php 中进行相同类型的配置。

它当然不是最漂亮的东西,但它应该为您提供所需的功能。

您提供了 xml 配置

<testsuites>
  <testsuite name="Library">
    <directory>library</directory>
  </testsuite>
  <testsuite name="XXX_Form">
    <file>library/XXX/FormTest.php</file>
    <directory>library/XXX/Form</directory>
  </testsuite>
</testsuites>

假设您的目录“library”包含 3 个文件:

library
   XXX    
     FormTest.php
   Unit
     unittest1.php
     unittest2.php

每个文件都包含 1 个按完美命名约定的测试,例如: FormTest 包含 testForm()

对于配置,我们将创建一个包含everything:

<?php
include_once "library/XXX/FormTest.php";
include_once "library/Unit/unittest1.php";
include_once "library/Unit/unittest2.php";

然后我们将按照 phpunit 命名约定创建一个类。您可以将其命名为您想要的任何名称,因为我们永远不会实际使用它...

class LibraryConfigTest extends PHPUnit_Framework_TestCase {

每个“测试套件”只是运行您想要的测试的方法。将方法命名为您想要的任何名称,因为我们永远不会真正使用它。 Phunit 将负责运行。不过,请务必将它们分组注释,以便您知道如何执行。

/**
 * All Tests in Library
 * @group Library
**/
   public function testLibrary() {
      UnitTest1::testUnit1();
      UnitTest2::testUnit2();
      FormTest::testForm();
   }

/**
 * All Form tests in library/XXX
 * @group XXX_Form
**/
   public function testForm() {
      FormTest::testForm();
   }
 }
 ?>

现在,为了获得您想要的功能,只需针对您想要的组运行“配置”即可。

phpunit --group XXX_Form library_config.php
phpunit --group Library library_config.php

正如我所说,这是丑陋的,而且肯定不是好的代码,因为它需要不断的维护,
但它会给你你正在寻找的功能。

希望 Bergmann 在下一轮中添加此功能,尽管这似乎不太可能,因为他看起来几乎是 忽略它

The other answers here are correct. You can't do this using an xml config, what you can do though is make the same type of config in php.

It's certainly not the prettiest thing, but it should give you the functionality you would need.

You provided the xml config

<testsuites>
  <testsuite name="Library">
    <directory>library</directory>
  </testsuite>
  <testsuite name="XXX_Form">
    <file>library/XXX/FormTest.php</file>
    <directory>library/XXX/Form</directory>
  </testsuite>
</testsuites>

Hypothetically, let's say your directory "library" contains 3 files:

library
   XXX    
     FormTest.php
   Unit
     unittest1.php
     unittest2.php

And that each of the files contains 1 test by perfect naming convention, eg: FormTest contains testForm()

For the config we'll create a config that contains everything:

<?php
include_once "library/XXX/FormTest.php";
include_once "library/Unit/unittest1.php";
include_once "library/Unit/unittest2.php";

Then we'll create a class by the phpunit naming conventions. You can name it whatever you want as we'll never actually use it...

class LibraryConfigTest extends PHPUnit_Framework_TestCase {

Each "test suite" will simply be a method that runs the tests you want. Name the methods whatever you want as, once again, we'll never actually use it. Phpunit will take care of the running. Be sure to comment them into groups though so you know how to execute.

/**
 * All Tests in Library
 * @group Library
**/
   public function testLibrary() {
      UnitTest1::testUnit1();
      UnitTest2::testUnit2();
      FormTest::testForm();
   }

/**
 * All Form tests in library/XXX
 * @group XXX_Form
**/
   public function testForm() {
      FormTest::testForm();
   }
 }
 ?>

Now in order to get the functionality you want just run the "config" against the group you want.

phpunit --group XXX_Form library_config.php
phpunit --group Library library_config.php

As I said, this is ugly and certainly not good code as it will require constant maintenance,
but it will give you the functionality you're looking for.

Hopefully Bergmann will add this functionality in his next round although it doesn't seem likely as he appears to pretty much be ignoring it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文