Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding certain tests from loading in phpunit

Tags:

phpunit

Some of my testcases use a custom testing library. Also these testcases are very slow. So I would like to run them only in the build server and not in my local. I want to run the other tests locally.

Following is the directory structure. The ones inside the slow directory are the slow test cases that should be excluded.

/tests/unit-tests/test-1.php
/tests/unit-tests/test-2.php
/tests/unit-tests/slow/test-1.php
/tests/unit-tests/slow/test-2.php
/tests/unit-tests/foo/test-1.php
/tests/unit-tests/bar/test-2.php

I tried creating groups using @group annotation. This works, but the issue is that these test files are getting loaded (tests not executed though). Since they require the test library which is not installed locally, it is giving an error.

What is the best way to create the phpunit.xml configuration such that these slow tests are excluded (and not even loaded) by default and can be executed if needed?

like image 578
Sudar Avatar asked Jul 26 '16 10:07

Sudar


People also ask

Is PHPUnit a framework?

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. The currently supported versions are PHPUnit 9 and PHPUnit 8.

What is PHPUnit testing?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit architecture for unit testing frameworks that originated with SUnit and became popular with JUnit. PHPUnit was created by Sebastian Bergmann and its development is hosted on GitHub.


1 Answers

There are 2 options:

  1. In your phpunit.xml create 2 test suits - one for CI server and one for local development
<testsuites>
    <testsuite name="all_tests">
        <directory>tests/unit-tests/*</directory>
    </testsuite>
    <testsuite name="only_fast_tests">
        <directory>tests/unit-tests/*</directory>
        <!-- Exclude slow tests -->
        <exclude>tests/unit-tests/slow</exclude>
    </testsuite>
</testsuites>

So on CI server you can run

phpunit --testsuite all_tests

And locally

phpunit --testsuite only_fast_tests

Obviously, you can name test suites as you want.

  1. I think preferable way is:
  • Create phpunit.xml.dist and configure default execution of phpunit (for CI server and all those who just cloned repository)
  • Modify phpunit.xml by configuring your local execution of phpunit (by adding <exclude>tests/unit-tests/slow</exclude> to default testsuite)
  • Exclude phpunit.xml from version control.

From the docs:

If phpunit.xml or phpunit.xml.dist (in that order) exist in the current working directory and --configuration is not used, the configuration will be automatically read from that file.


Some links:

The XML Configuration File. Test Suites

How to run a specific phpunit xml testsuite?

like image 90
Nikita U. Avatar answered Oct 09 '22 00:10

Nikita U.