How to run a specific phpunit xml testsuite?

前端 未结 5 1404
小鲜肉
小鲜肉 2020-11-30 23:15

how can i choose a specific testsuite to be executed?

$ phpunit --configuration config.xml

config.xml:



        
相关标签:
5条回答
  • 2020-11-30 23:31

    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.

    0 讨论(0)
  • 2020-11-30 23:34

    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.

    0 讨论(0)
  • 2020-11-30 23:34

    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.

    0 讨论(0)
  • 2020-11-30 23:37

    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!

    0 讨论(0)
  • 2020-11-30 23:37

    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.

    0 讨论(0)
提交回复
热议问题