PHPUnit: How do I create a function to be called once for all the tests in a class?

snakile picture snakile · Aug 23, 2011 · Viewed 45.1k times · Source

I have a PHPUnit test case class (consisting of some test functions). I would like to write a oneTimeSetUp() function to be called once for all my tests in the class (unlike the standard setUp() function which is called once for each test in the class). In other words, I'm looking for a PHPUnit equivalent to the JUnit @BeforeClass annotation.

Same question with a oneTimeTearDown() function.

Is it possible to do so in PHPUnit?

Answer

borrible picture borrible · Aug 23, 2011

Take a look at setUpBeforeClass() from section 6 of the PHPUnit documentation.

For the one time tearDown you should use tearDownAfterClass();.

Both this methods should be defined in your class as static methods.