0

How can I force PHPUnit to stop running completely and exit when a specific condition (an error of my own choosing) is met? Effectively, what I need is something like the below, except that in reality PHPUnit traps the exit() and continues running instead of exiting.

// PHPUnit does not alter existing but empty env vars, so test for it.
if (strlen(getenv('APP_HOME')) < 1) {
    $this->fail('APP_HOME set but empty.');
    exit(1);  // <-- Does not work.
}

Note: I want to continue running normally for other errors and failures, hence setting stopOnError="true" or stopOnFailure="true" in my XML file is not what I need.

1 Answer 1

1

I think you can achieve this by doing a few overrides and adding some custom behaviour to a base test case class.

EDIT:

As found by the OP after running the below code, calling exit(1); rather than $result->stop() will cause correct termination of the test at that point.

Try the following:

class MyBaseTestCase extends \PHPUnit_Framework_TestCase
{
    // Test this flag at every test run, and stop if this has been set true.
    protected $stopFlag = false;

    // Override parent to gain access to the $result so we can call stop()
    public function run(\PHPUnit_Framework_TestResult $result = null)
    {
        $result = parent::run($result);
        if ($this->stopFlag === true)
        {
            //$result->stop(); // Stop the test for this special case
            exit(1); // UPDATED: This works to terminate the process at this point
        }
        return $result; // return as normal
    }
}

Then in a test case class:

class MyTestCase extends MyBaseTestCase
{
    public function testThisStopsPhpunit()
    {
        if (strlen(getenv('APP_HOME')) < 1) {
            $this->fail('APP_HOME set but empty.');
            $this->stopFlag = true; // Stop further processing if this occurs
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

This means all of the tests will actually be initiated, but stopped in our run() if $stopFlag is true, correct? I have multiple files (classes), each with multiple test cases, all of which depend on the environment variable being set correctly.
I was able to make this do what I wanted by using exit(1) instead of the $result->stop() line in the run() method above. Interestingly, exit() works normally in this context, exiting phpunit completely, versus how it does not work when called inside a test.
Glad you could get it working. Out of interest what result did you get from calling stop(), did it not terminate further tests from being run after the first failure?
stop() only stopped the current test. Since I have a bunch of tests in multiple files, the others still got called (and then stopped). If you'd care to edit your answer to include this bit of information we discovered, I'll mark it as the accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.