PHPUnit – Using fixtures for tests

In a unit test, we usually want to do some common codes like object initialization, database connectivity etc. Suppose, if there exists multiple tests in a testing class, then it will be really cumbersome to write same set of codes in all the tests. In order to share common set of codes in multiple tests, we can use fixtures. In short, fixture is a state that can be shared across multiple tests.

In order to setup a fixture, PHPUnit provides a set of template methods as given below :

1. setup : This function will be executed just before a test is started.

2. teardown : This function will be executed immediately after the execution of a test.

3. assertPreCoditions : This function will be executed just before a test is started but after executing setup

4. assertPostConditions : This function will be executed immediately after the execution of a test but before teardown

5. setupBeforeClass : This will be executed before any test is started and even before setup of the first test. This should be a static function

6. teardownAfterClass : This will be executed after all the tests are executed including teardown of the last test.

Note : The template functions setup, teardown, assertPreConditions and assertPostConditions are executed against all the tests of a test class. Whereas the template functions setupBeforeClass and teardownAfterClass are executed only once for a test class.

Given below is a test class using fixtures. In this test case class we are testing the divide operation of our Calculator class used in the previous examples.

1. PHPUnit test class using fixtures

<?php
    use PHPUnit\Framework\TestCase;

    class CalculatorWithFixtureTest extends TestCase {
        protected $calc;

        // Template Function
        public  static function setupBeforeClass(){
        }

        // Template Function
        public function setup(){
            $this->calc = new Calculator();
        }

        // Template Function
        public function assertPreConditions(){

        }

        // Template Function
        public function assertPostConditions() {

        }

        // Template Function
        public function teardown(){
        }

        // Template Function
        public  static function teardownAfterClass(){

        }

        public function testIsDivisionCorrect(){            
            $result = $this->calc->divide(0,1);
            $expected = 0;
            $this->assertSame($expected,$result);       
        }        

    }

2. Running the test

We can run the test by executing the given below command at the root of the project

$./vendor/bin/phpunit --testdox tests/CalculatorWithFixtureTest.php

On executing the test, PHPUnit will display the test result as given below :

PHPUnit test with fixtures

Comments on this post

No comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

Trackbacks and Pinbacks on this post

No trackbacks.

TrackBack URL