3

I want to use setUpBeforeClass() to setup a db connection, and do some logging, but it is not being called before my tests execute (or at all, for that matter). I have the following:

class TestSetup extends PHPUnit_Extensions_SeleniumTestCase {
    public static function setUpBeforeClass() {
        //do some setup stuff here for all my tests
    }
    protected function setUp() {
        $this->setBrowserUrl('http://' . $this->deviceIp);
    }
    protected function testOne() {
        //do a test here
    }
    protected function testTwo() {
        //do a test here
    }
}

I did some digging into PHPUnit/Frameworks/TestSuite.php and have confirmed that on line 660 that $this->testCase is bool(false). But I couldn't figure out if it should be true or where that should happen (other than in __construct()).

I'm slightly over my head here, so any help would be greatly appreciated.

Let me know if I can provide any other helpful information.

Josh

1 Answer 1

4

I couldn't find anything in the docs but the code seems to agree with you.

In PHPUnit/Extensions/SeleniumTestCase.php run method (line 289+) there is no sign of calling setUpBeforeClass (or any other method that might do so).

If you consider that an issue I'd suggest opening a ticket on phpunits issue tracker.

For a workaround you could use a static property in setUp like this:

protected function setUp() {
    static $db = null;
    if($db === null) {
        $db = whatevery_you_do_there();
    }
    $this->db = $db;
}

that should work, sort of, as if you'd run it in setUpBeforeClass()

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, twofold. First, for confirming that I wasn't missing something :). Second, for a very clever solution that does exactly what I need.

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.