Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Running with --process-isolation breaks semantics of setUpBeforeClass() #8

Closed
jakubkulhan opened this Issue · 2 comments

2 participants

Jakub Kulhan Sebastian Bergmann
Jakub Kulhan

With --process-isolation setUpBeforeClass() is not run before tests, but instead for each test.

<?php
require 'PHPUnit/Framework/TestCase.php';

class FooTest extends PHPUnit_Framework_TestCase
{
    static function setUpBeforeClass()
    {
        echo "setUpBeforeClass, pid=" . getmypid() . "\n";
    }

    function testA()
    {
        echo "testA, pid=" . getmypid() . "\n";
        $this->assertTrue(TRUE);
    }

    function testB()
    {
        echo "testB, pid=" . getmypid() . "\n";
        $this->assertTrue(TRUE);
    }
}

Expected output:

PHPUnit 3.5.0RC1 by Sebastian Bergmann.

setUpBeforeClass, pid=23871
testA, pid=23872
.testB, pid=23873
.

Time: 0 seconds, Memory: 3.50Mb

OK (2 tests, 2 assertions)

Actual output:

PHPUnit 3.5.0RC1 by Sebastian Bergmann.

setUpBeforeClass, pid=23882
setUpBeforeClass, pid=23883
testA, pid=23883
.setUpBeforeClass, pid=23884
testB, pid=23884
.

Time: 1 second, Memory: 3.50Mb

OK (2 tests, 2 assertions)
Sebastian Bergmann

That is the expected behavior for process isolation.

Jakub Kulhan

OK, I see.

How do you suggest to test cache that can make item depedent on presence/content of a constant? If setUpBeforeClass() was called only once per testcase, it would be simple:

<?php
require 'PHPUnit/Framework/TestCase.php';

class CacheTest extends PHPUnit_Framework_TestCase
{
    static $cache;

    static function setUpBeforeClass()
    {
        self::$cache = new Cache(TEMP_DIR);
        self::$cache->clean(); // removes all data from cache (and that is the problem,
                               // because it will be called before
                               // testChangeOfConstantWillRemoveItemFromCache, so 
                               // the test will say everything is OK even if it may 
                               // not be)
    }

    /**
     * @runInSeparateProcess
     */
    function testSaveItemThatDependsOnConstant()
    {
        define('FOO', 'bar');

        self::$cache->save(
            'key', 'value',
            self::$cache->constantDependency('FOO') // makes item saved under
                                                    // key 'key' dependent
                                                    // on constant FOO
        );

        $this->assertTrue(self::$cache->isCached('key'));
        $this->assertEquals(self::$cache->read('key'), 'value');
    }

    /**
     * @depends testSaveItemThatDependsOnConstant
     * @runInSeparateProcess
     */
    function testChangeOfConstantWillRemoveItemFromCache()
    {
        define('FOO', 'not bar');

        $this->assertFalse(
            self::$cache->isCached('key') // because content of FOO has changed,
                                          // item should not be in cache anymore
        );
    }
}
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.