PHPUnit setup and tearDown for test cases

Viewed 5637

PHPUnit has setup and tearDown events that run, respectively, before and after each test within a test case. In my specific scenario, I also want to run something like a testCaseSetup and testCaseTearDown. Is that possible?

Current solution looks like this:

<?php

class MyTestCase extends \PHPUnit_Framework_TestCase
{

    public function __construct($name = NULL, array $data = array(), $dataName = '')
    {
        // My test case setup logic
        parent::__construct($name, $data, $dataName);
    }

    public function __destruct()
    {
        // My test case tear down logic
    }
}

But it seems far from optimal for the following reasons:

  • I have to redeclare PHPUnit_Framework_TestCase construct and redirect any arguments. IF PHPUnit constructor is changed on a version update, my test case will stop.
  • Probably PHPUnit_Framework_TestCase was not declared to be used like this.

I would like to know if there are better solutions. Any ideas?

1 Answers
Related