How to Determine if PHPUnit Tests are Running?

前端 未结 5 1059
别跟我提以往
别跟我提以往 2020-12-30 20:03

I currently have a problem that I have to work around in legacy code to get our interaction with a PHP Extension to work properly (Singleton Testing Question).

As su

相关标签:
5条回答
  • 2020-12-30 20:45

    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!';
    }
    
    0 讨论(0)
  • 2020-12-30 20:52

    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.

    0 讨论(0)
  • 2020-12-30 21:01

    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') { ... }
    
    }
    
    0 讨论(0)
  • 2020-12-30 21:07

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

    0 讨论(0)
  • 2020-12-30 21:10

    Use PHPUnit Constants

    You can either define constant, but that requires your work, no typos and it's not generic. How to do it better?

    PHPUnit defines 2 constants by itself:

    if (! defined('PHPUNIT_COMPOSER_INSTALL') && ! defined('__PHPUNIT_PHAR__')) {
        // is not PHPUnit run
        return;
    }
    
    // is PHPUnit
    
    0 讨论(0)
提交回复
热议问题