How to Determine if PHPUnit Tests are Running?

An alternative approach is to set a constant in the PHP section of your phpunit.xml.*:

<php>
   <const name="PHPUNIT_YOURAPPLICATION_TESTSUITE" value="true"/>
</php>

In your PHP application, you might then use the following check:

if (defined('PHPUNIT_YOURAPPLICATION_TESTSUITE') && PHPUNIT_YOURAPPLICATION_TESTSUITE)
{ 
    echo 'TestSuite running!';
}

Define a constant in your PHPUnit bootstrap.php file. This is executed before loading or running any tests. This shouldn't impact developers running the application normally--just the unit tests.


If you're using Laravel than use App::runningUnitTests()


You could check the $argv different ways.

if(PHP_SAPI == 'cli') {

    if(strpos($_SERVER['argv'][0], 'phpunit') !== FALSE) { ... }
    // or
    if($_SERVER['argv'][0] == '/usr/bin/phpunit') { ... }

}

Tags:

Phpunit