3

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>
Ian Bytchek
  • 8,804
  • 6
  • 46
  • 72
holygrinder
  • 108
  • 7
  • 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 Oct 17 '14 at 21:16
  • 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 https://github.com/sebastianbergmann/phpunit/blob/master/src/Framework/TestSuite.php 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 Oct 19 '14 at 16:59

1 Answers1

1

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!

Niloct
  • 9,491
  • 3
  • 44
  • 57