The most time to write a test case is to write code that sets the program back to its original state after it has been set up and tested. PHPUnit provided us with the setup and Teardown methods to solve this problem.
Setup is called before each test case of the class is run, and you can do some of the related program initialization work in it.
The Teardown method is called after each test case has finished running, and you can do some cleanup work in it.
It is important to note that the setup is not necessarily written in the Teardown,teardown to be used to reclaim those objects that occupy a large amount of resources, such as the connection of the database, open files and so on. is a simple example of a demo Setup method (click to enlarge)
In addition to setup and teardown, PHPUnit provides some ways to get you ready for more work, see (click to enlarge)
The result of the above test execution
PHPUnit Templatemethodstest
PHPUnit 3.5.13 by Sebastian Bergmann.
Templatemethodstest::setupbeforeclass
Templatemethodstest::setup
Templatemethodstest::assertpreconditions
Templatemethodstest::testone
Templatemethodstest::assertpostconditions
Templatemethodstest::teardown
. Templatemethodstest::setup
Templatemethodstest::assertpreconditions
Templatemethodstest::testtwo
Templatemethodstest::teardown
Templatemethodstest::onnotsuccessfultest
Ftemplatemethodstest::teardownafterclass
time:0 seconds
There was 1 failure:
1) templatemethodstest::testtwo
Failed asserting <boolean:false> is true.
/home/sb/templatemethodstest.php:30
failures!
Tests:2, Assertions:2, failures:1.
PHP Tutorial: PHPUnit Study Notes (vi) Fixture