1
votes

I have a couple of questions regarding how phpunit runs the tests. Say that I have a class with 3 test methods, along with a setUp() and tearDown() methods.

When I run phpunit, how is it running the tests?

  1. Does it initialize the class once and runs setUp(), then the test method, then tearDown() and then repeats the above steps for the second method and so on, or

  2. It instantiates the class each time it will run each test method

Is this procedure documented somewhere?

Thanks

1
There is a very simple way to find out: let the class' constructor display a message on each call then run the test case and count the number of displayed messages.axiac
Lol. You are right. I checked it and it actually instantiates the class each time it runs a test method, which is the opposite than what @Tal Avissar suggestThomas

1 Answers

0
votes

I think it would be useful to mention difference between:

  1. setUp() this method is called before every test case, which mean that this method can be called few times per one test class.

  2. setUpBeforeClass() method is executed only once per class, and even before object is constructed, and that is reason why it is marked as static public function.

The above 2 methods can be found in most unit tests we are using.

Similar is difference between tearDownAfterClass and tearDown(), tearDown() method is called after each test case, and tearDownAfterClass() method is called after all tests in class finish, and after last tearDown() method is called.

The class is instantiated once and then all test methods are run on this instance.