0
votes

What is use of setUp() and tearDown() both methods in the PHPUnit. Its invoked automatically in the test class? when it is work and how to call this two methods?

1

1 Answers

2
votes

The method setUp() will be called before each one of the test method and tearDown() method will be called after every test method has been executed. Suppose you want to test two table's data. So for that you have to always need a connection and after execute your query you have to close the connection. So instead of every time we create and close connection for each method, we can do something like this

protected void setUp() {
//code for getting connection
}
protected void tearDown() {
//code for close your connection
}

void testAccoountTable() {
//test code for account table
}
void testEmployeeTable() {
//test code for Employee table
}

Now when you run the tests testAccountTable and testEmployeeTable the setUp (before execution of every test method) and tearDown(after execution of every test method) method will be automatically called for every test method.