Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a single testsuite by default in PHPUnit

My PHPUnit configuration file has two test suites, unit and system. When I run the test runner vendor/bin/phpunit, it runs all tests in both suites. I can target a single suite with the testsuite flag: vendor/bin/phpunit --testsuite unit, but I need to configure the test runner to run only the unit suite by default, and to run integration only when specifically called with the testsuite flag.

My configuration:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
  <testsuites>
    <testsuite name="unit">
      <directory>tests/Unit</directory>
    </testsuite>
    <testsuite name="integration">
      <directory>tests/Integration</directory>
    </testsuite>
  </testsuites>
  <filter>
    <whitelist>
      <directory suffix=".php">src</directory>
    </whitelist>
  </filter>
  <logging>
    <log type="coverage-clover" target="build/clover.xml"/>
  </logging>
</phpunit>
like image 590
jdp Avatar asked Jun 14 '16 19:06

jdp


People also ask

What is the PHPUnit XML configuration file used to organize tests?

The beStrictAboutChangesToGlobalState Attribute This attribute configures whether PHPUnit should mark a test as risky when global state is manipulated by the code under test (or the test code).

What is PHPUnit XML file?

PHPUnit uses XML file for configuration. This configuration file can be added to your version control system so all developers get the same output. These settings will help you make sure your tests work the way you want.


2 Answers

Since PHPUnit 6.1.0, there is now support for a defaultTestSuite attribute.

Take a look at https://github.com/sebastianbergmann/phpunit/pull/2533

This can be used amongst the other phpunit attributes like so:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.2/phpunit.xsd"
         backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="tests/bootstrap.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         defaultTestSuite="unit"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="unit">
            <directory suffix="Test.php">tests/Unit</directory>
        </testsuite>
        <testsuite name="integration">
            <directory suffix="Test.php">tests/Integration</directory>
        </testsuite>
    </testsuites>
</phpunit>

You can now run phpunit instead of phpunit --testsuite unit.

The name of the testsuite may well be case-sensitive, so watch out for that.

like image 121
GaryJ Avatar answered Oct 16 '22 12:10

GaryJ


There doesn't appear to be a way to list multiple testsuites from a phpunit.xml file, but then only run one. However, if you do have some control over a fuller integration and testing environment where you can configure things more exactly, you can have more than one phpunit configuration files, and set one (or more) with more involved environments to set the command-line parameter --configuration <file> option with a configuration that will do more. This at least makes sure that the simplest config will run in the easiest fashion.

The two files can be called whatever you like if you run them specifically, but it may be worth thinking about having the quick-to-run file called the default phpunit.xml, and the specifically named and extended filem as phpunit.xml.dist. The .dist file will be automatically run by default if the original plain .xml does not exist. Another option is to have the phpunit.xml.dist file in a code repository, but then copy it to a phpunit.xml file, with less testsuite's, which is not in itself checked into version control, and is only kept locally (it would probably also be marked as ignored in a .gitignore file, or similar).

like image 40
Alister Bulman Avatar answered Oct 16 '22 10:10

Alister Bulman