Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use PHPUnit @depends without calling tearDown and setUp between dependent cases?

Tags:

php

phpunit

For example, the action in test1 stores data externally* which test2 then performs an assertion on, but tearDown deletes that data thus breaking test2. Cache deletion can't be removed from tearDown since other tests depend on it. This question is asking if there is a way to skip setUp/tearDown between dependent cases while maintaining the functionality of @depends (which skips the second test if the first test fails instead of the second test failing).

public function tearDown() {
    // delete cache
}

// verify the expected data was retrieved from an uncached source
public function test1() {
    $sut = new SystemUnderTest();
    $data = $sut->getDataAndCache();
    $this->assertEquals('expected', $data);
    return $sut;
}

// verify the expected data was cached
/** @depends test1 */
public function test2($sut) {
    $this->assertEquals('expected', $sut->getCache());
}

*We'll call these integration tests since they interact with an external system.

like image 974
Mike Henry Avatar asked Aug 25 '13 05:08

Mike Henry


People also ask

What are PHPUnit fixtures?

One of the most time-consuming parts of writing tests is writing the code to set the world up in a known state and then return it to its original state when the test is complete. This known state is called the fixture of the test.

What is PHPUnit testing?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit architecture for unit testing frameworks that originated with SUnit and became popular with JUnit. PHPUnit was created by Sebastian Bergmann and its development is hosted on GitHub.

What is test case PHP?

php is an example test class that includes a basic test case using the application testing helpers – ignore it for now. TestCase. php : The TestCase. php file is a bootstrap file for setting up the Laravel environment within our tests.


2 Answers

There are a couple of options.

The first one would be to isolate these two tests into a separate test class. That way your tearDown in other classes will not interfere.

You still might want to get rid of the cache after the test. Well, testing the cache deletion could be a step of itself. But PHPUnit also offers two static methods that are run before the test class starts testing, and after all tests in the class have been run: setUpBeforeClass() and tearDownAfterClass() (see http://phpunit.de/manual/3.7/en/fixtures.html).

On the other hand, the easiest way out would be to combine the two test methods into ONE function. You are already in some kind of trouble with violating the single responsibility principle by naming a function getData*AND*Cache, so there is little benefit in separating the test into two functions.

like image 51
Sven Avatar answered Nov 15 '22 18:11

Sven


In the setup method you can check if the test has hasDependencies() and then skip the setup procedures:

public function setUp()
{
    if (!$this->hasDependencies()) {
        // do setup tasks 
    }
}
like image 31
Gerard Roche Avatar answered Nov 15 '22 18:11

Gerard Roche