Codeception: @runTestsInSeparateProcesses revisited

Created on 27 Feb 2019  路  2Comments  路  Source: Codeception/Codeception

I need to do various automated tests related to a platform's bootstrap process. This means running a number of times a startup procedure that messes with global state (particularly, loading classes and class static initialization), and possibly with different configurations. The PHPUnit @runTestsInSeparateProcesses is an ugly hack ( I'd have implemented it using http://php.net/manual/ro/function.pcntl-fork.php ), but it kinda works.

However, with Codeception I get, for instance:

[InjectionException] Service di is not defined and can't be accessed from a test

https://github.com/Codeception/Codeception/issues/1556

Would there be any way to achieve some kind of process isolation with Codeception?

Most helpful comment

If you find it useful, I managed to make a functional process isolation wrapper for the unit tests:
https://github.com/exteon/WithIsolation

To make it even more perfect, it would need:

  • a wrapper around $testResult = $this->{$this->name}(...\array_values($testArguments)); in \PHPUnit\Framework\TestCase::runTest()
  • a way to disable shutdown nagging and handlers at runtime (see also https://github.com/Codeception/Codeception/issues/4215#issuecomment-467872269)

All 2 comments

I have been able to get this functionality by writing a unit test function like this, works like a charm:

$pipe=tempnam('/tmp','runTest');
file_put_contents($pipe,serialize(new \Codeception\Exception\Fail('Separate process did not run cleanly')));
if(!pcntl_fork()){
    try {
        /* testing code goes here; it will run in the forked process */
    } catch (\Exception $e) {
        file_put_contents($pipe,serialize($e));
        die();
    }
    file_put_contents($pipe,'');
    die();
} else {
    pcntl_wait($status);
    $se=file_get_contents($pipe);
    unlink($pipe);
    if($se){
        $exception=unserialize($se);
        throw $exception;
    }
}

Since it works inside Codeception, I think adding such behavior via an annotation comment PHPUnit-style should not be too difficult.

If you find it useful, I managed to make a functional process isolation wrapper for the unit tests:
https://github.com/exteon/WithIsolation

To make it even more perfect, it would need:

  • a wrapper around $testResult = $this->{$this->name}(...\array_values($testArguments)); in \PHPUnit\Framework\TestCase::runTest()
  • a way to disable shutdown nagging and handlers at runtime (see also https://github.com/Codeception/Codeception/issues/4215#issuecomment-467872269)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

sasha-x picture sasha-x  路  3Comments

sebastianneubert picture sebastianneubert  路  3Comments

gimler picture gimler  路  3Comments

centerax picture centerax  路  4Comments

simara-svatopluk picture simara-svatopluk  路  3Comments