Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to include/exclude certain groups from test-suite via phpunit.xml

I would like to exclude or include certain test from test-suites. I would like to have some control of this via annotations/groups rather than naming specific files or folders in phpunit.xml

I have attempted something like this, but its appears to be ignoring the <groups> and/or <include>

<testsuites>
    <testsuite name="Unit">
        <directory>Unit</directory>
    </testsuite>
    <testsuite name="IntegrationFirstRound">
        <directory>Integration/</directory>
        <include><!-- I want to ONLY include this group -->
            <group>first-round</group>          
        </include>
    </testsuite>
    <testsuite name="IntegrationOther">
        <directory>Integration/</directory>
        <exclude><!-- I want to EXCLUDE this group, run all others -->
            <group>first-round</group>
        </exclude>
    </testsuite>
</testsuites>

I don't want to move tests to different folders just to accommodate this, and I do not want to invoke phpunit multiple times from the CLI, I am hoping I can achieve the desired results via the xml config.

like image 543
Sam Anthony Avatar asked Mar 12 '18 23:03

Sam Anthony


People also ask

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

The <testsuites> Element This element is the root for one or more <testsuite> elements that are used to configure the tests that are to be executed.

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.

How do I run a single PHPUnit test?

You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status. The output shows that we ran 1 test, and made 3 assertions in it.

What is PHPUnit used for?

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. PHPUnit 9 is the current stable version. PHPUnit 10 is currently in development.


2 Answers

Ok, looking at the DOCs which should be the first place you look

https://phpunit.de/manual/current/en/appendixes.configuration.html

You need a groups element with the group inside of it. So where you have

<exclude><!-- I want to EXCLUDE this group, run all others -->
     <group>first-round</group>
</exclude>

You should have

<groups>
    <exclude><!-- I want to EXCLUDE this group, run all others -->
        <group>first-round</group>
   </exclude>
</groups>

It doesn't really say if it should go inside the <testsuite>, and I never used it but I am sure if you look in the documentation you should find some examples.

like image 124
ArtisticPhoenix Avatar answered Sep 19 '22 00:09

ArtisticPhoenix


In my case I grouped as data


<?php

namespace Tests\Unit\Artefact;

use Tests\TestCase;

/**
 * @group data
 */
class DataMovieTest extends TestCase
{

}

and then run phpunit from terminal like

phpunit --exclude data

like image 27
Bedram Tamang Avatar answered Sep 18 '22 00:09

Bedram Tamang