PHPUnit - Fixtures - setUp and tearDown

PHPUnit

← Prev

In this tutorial we will learn about fixtures in PHPUnit.

In the Hello World Test tutorial we have seen how to write basic test. And we have also seen the use of assertEquals() method.

Now, lets talk about fixtures in PHPUnit.

What is fixture?

Fixture is an initial state of the application when a test is executed.

setUp and tearDown

In PHPUnit testing we have the setUp() method that is called before every test. And we have the tearDown() method that is called after every test.

We use the setUp() method to initialise variables, open file connection etc. that will prepare the environment for the test. And after completing the test we use the tearDown() method to unset variables, close file connection etc.

In most scenario we will be using the setUp() method more than the tearDown() method.

Example

In the following example we are testing odd/even number. Our test methods are testOdd() and testEven(). We also have the setUp() method that is executes before every test and tearDown() method that is executed after every test.

<?php

class OddEvenTest extends PHPUnit_Framework_TestCase
{
    private $number;

    protected function setUp()
    {
        $this->number = 2;
    }

    public function testOdd()
    {
        $this->number++;
        $this->assertNotEquals(0, $this->number % 2);
    }

    public function testEven()
    {
        $this->assertEquals(0, $this->number % 2);
    }

    protected function tearDown()
    {
        $this->number = null;
    }
}

In the above code we have a private variable $number. And in the setUp() method we are assigning value 2 to the variable $number.

For odd test:

Inside the testOdd() method we are incrementing the value of $number by 1 using the increment operator ++ so, the new value is 3. Next, we are checking the remainder on dividing $number by 2 using the assertNotEquals() method.

The assertNotEquals() method takes two arguments. The first argument is an expected value and the second argument is the value that we want to check. On dividing 3 by 2 we get 1 as remainder which is not equal to 0 so, the assertNotEquals() method gives us a successful test result.

For even test:

Inside the testEven() method we are checking the remainder on dividing $number by 2 using the assertEquals() method.

The assertEquals() method takes two arguments. The first argument is an expected value and the second argument is the value that we want to check. On dividing 2 by 2 we get 0 as remainder which is equal to 0 so, the assertEquals() method gives us a successful test result.

In the tearDown() method we are just setting the value of $number to null.

Output

Testing started at 1:42 PM ...
PHPUnit 5.7.23 by Sebastian Bergmann and contributors.

Method: OddEvenTest::setUp
Method: OddEvenTest::testOdd
Method: OddEvenTest::tearDown
Method: OddEvenTest::setUp
Method: OddEvenTest::testEven
Method: OddEvenTest::tearDown

Time: 64 ms, Memory: 4.00MB

OK (2 tests, 2 assertions)

Process finished with exit code 0

From the above output we can see that there are 2 tests and both are successfully.

setUpBeforeClass and tearDownAfterClass

The setUpBeforeClass() method is called before the first test is executed and the tearDownAfterClass() method is called after last test is executed.

We use these two methods to share settings with all the tests.

For example, it is wise to get the database connection once in the setUpBeforeClass() method then get connection multiple times in the setUp() method.

And likewise, it is wise to close the database connection only once in the tearDownAfterClass() method then close the database connection after every test in the tearDown() method.

So, basically we create a database connection only once and then reuse the connection in every test. This helps to run the test faster.

← Prev