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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3671685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:16:26  来源:igfitidea点击:

How to run a specific phpunit xml testsuite?

xmlphpunit

提问by Andreas Linden

how can i choose a specific testsuite to be executed?

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

$ phpunit --configuration config.xml

$ 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>

回答by Josh Woodcock

Here's the code as if PHPUnit 3.7.13

这里的代码就像 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

不幸的是 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>

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

因此,如果您想以这种方式运行测试套件组,您必须有 xml 配置重复!

回答by Anti Veeranna

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

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

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

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

phpunit library/XXX

This would run all the tests in library/XXX directory

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

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

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

回答by Carlos C Soto

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 xxxand is overriden.

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

回答by Mike Purcell

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.

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

回答by SuperFamousGuy

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.

这里的其他答案是正确的。您不能使用 xml 配置执行此操作,但您可以做的是在 php 中创建相同类型的配置。

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

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

You provided the 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>

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

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

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()

并且通过完美的命名约定,每个文件都包含 1 个测试,例如:FormTest 包含 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...

然后我们将按照 phpunit 命名约定创建一个类。您可以随意命名它,因为我们永远不会实际使用它...

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.

每个“测试套件”只是一个运行你想要的测试的方法。再一次为这些方法命名,因为我们永远不会实际使用它。Phpunit 将负责运行。请务必将它们分组评论,以便您知道如何执行。

/**
 * 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.

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