How to run a specific phpunit xml testsuite?

XmlPhpunit

Xml Problem Overview


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>

Xml Solutions


Solution 1 - 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!

Solution 2 - Xml

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.

Solution 3 - Xml

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.

Solution 4 - Xml

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.

Solution 5 - Xml

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndreas LindenView Question on Stackoverflow
Solution 1 - XmlJosh WoodcockView Answer on Stackoverflow
Solution 2 - XmlAnti VeerannaView Answer on Stackoverflow
Solution 3 - XmlCarlos C SotoView Answer on Stackoverflow
Solution 4 - XmlMike PurcellView Answer on Stackoverflow
Solution 5 - XmlSuperFamousGuyView Answer on Stackoverflow