3
votes

I extended PHPUnit_Framework_TestSuite to overwrite the setUp and tearDown methode because I need PHPUnit to do some operation before and after a suite of test. (the test are in multiple TestCase class.)

class MyTestSuite extends PHPUnit_Framework_TestSuite {

    protected function setUp()
    {
        //do some stuff before all tests are run
    }

    protected function tearDown()
    {
        //do some stuff after all tests are run
    }
}

How in a xml config file do I tell phpunit to use this TestSuite class and then bound the testCase class to it? All I can find are example like this which doesnt seem like specify which test suite class phpunit shall use.

<phpunit>
  <testsuites>
    <testsuite name="money">
      <file>tests/IntlFormatterTest.php</file>
      <file>tests/MoneyTest.php</file>
      <file>tests/CurrencyTest.php</file>
    </testsuite>
  </testsuites>
</phpunit>
1
Why can't you simply extend your test suite in specific test cases? Eg. IntlFormatterTest extends MyTestSuite. Just note that setUp() and tearDown() methods are meant to run before and after every test method. Maybe what you need is setUpBeforeClass() and tearDownAfterClass().Maciej Sz
Because they are completely different types of test and I dont want to put then in the same class for logic reasons. Sadly they both need the same "setUpBeforeClass" and "tearDownAfterClass". So that's why I am using test suites. And if you look at the code itself for phpunit github.com/sebastianbergmann/phpunit/blob/master/src/Framework/… the test suite class doesnt have these two methods... but if you look the comment over setUp and tearDown you get : "Template Method that is called before the tests of this test suite are run."holygrinder

1 Answers

1
votes

In https://phpunit.de/manual/current/en/appendixes.configuration.html#appendixes.configuration.testsuites there is this information:

The <testsuites> element and its one or more <testsuite> children can be used to compose a test suite out of test suites and test cases.

I use Ecomdev_PHPUnit in Magento, which does subclass like you want, look at its xml:

<testsuite name="Magento Test Suite">
     <file>app/code/community/EcomDev/PHPUnit/Test/Suite.php</file>
</testsuite>

So just pass the test suite complete path I guess!