Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependent tests between two TestCase classes in PHPUnit

In PHPUnit you can make one test to be depend on other test by using @depends annotation. Is it possible to make whole TestCase dependent on test in other TestCase? Or at least make single test in one TestCase dependent on test in other TestCase?

I tried:

/**
 * @depends A::testMethodName
 */

But as I expected it doesn't work.

Update:

The exact situation looks like this: There is class B which uses class A. So I want to test B only if the tests for A (or one of it's tests) run without a failure. How can I do that?

like image 254
Dawid Ohia Avatar asked Jan 12 '12 13:01

Dawid Ohia


People also ask

What is stub in PHPUnit?

Stubs are used with query like methods - methods that return things, but it's not important if they're actually called. $stub = $this->createMock(SomeClass::class); $stub->method('getSomething') ->willReturn('foo'); $sut->action($stub);

How do I run all PHPUnit tests?

How to Run Tests in PHPUnit. You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status.

What is PHPUnit used for?

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. PHPUnit 9 is the current stable version. PHPUnit 10 is currently in development.


2 Answers

Exploiting dependencies are extremely important! The loose coupling as referred to is for actual application architecture not for unit test cases. If there is logical dependencies built in to functional execution is is always a good idea to exploit those dependencies.

Using test doubling is appropriate for SOA where dependencies cannot be mapped to a particular failure within a black box AND the service is not reliable. This is not appropriate for inter application classes.

You definitely would want to use this type of functionality if there is logical dependencies between test classes. The concept to be grasped from unit testing is it's ability to isolate defects to particular components immediately.

This functionality IS available on PHPUnit v 3.7.13. However, the only way this will work is if you run PHPUnit on a directory which contains both TestCase classes.

For example with this folder structure

- application\dep
   |- BTest.php
   |- CTest.php

The classes...

class BTest extends PHPUnit_Framework_TestCase
{
    /**
     * @depends CTest::testADomino
     */
    public function testDominoDependent()
    {
        $this->assertTrue(true);
    }
}

and...

class CTest extends PHPUnit_Framework_TestCase
{
    public function testADomino()
    {
        $this->assertTrue(false);
    }
}

This is the result

C:\Users\Josh>C:\xampp\php\phpunit.bat "C:\xampp\htdocs\BeAgile\applications\sto
cklogger\tests\dep"
PHPUnit 3.7.13 by Sebastian Bergmann.

SF

Time: 0 seconds, Memory: 2.00Mb

There was 1 failure:

1) CTest::testADomino
Failed asserting that false is true.

C:\xampp\htdocs\BeAgile\applications\stocklogger\tests\dep\CTest.php:7

FAILURES!
Tests: 1, Assertions: 1, Failures: 1, Skipped: 1.

You could have both test case classes in the same file but that would be a poor structure. It is not necessary to "make sure" one test runs before the other.

As an agile coach I see test doubling far too often in large organizations where specialized segments want to avoid having build failures when another components makes changes that causes test failure. This of course defeats the entire purpose of the unit tests which is to identify component failures before the end user does.

like image 59
Josh Woodcock Avatar answered Oct 05 '22 11:10

Josh Woodcock


There's no built-in way to do this, but it wouldn't be hard to have any number of tests depend on some other test passing. You must ensure that ATest is executed before BTest.

class ATest extends PHPUnit_Framework_TestCase {
    public static $passed = false;

    function testThatMustPass() {
        // ... the actual test ...
        // ok, test passed
        self::$passed = true;
    }
}

class BTest extends PHPUnit_Framework_TestCase {
    function testThatDependsOnA() {
        if (!ATest::$passed) {
            self::markTestSkipped('A failed');
        }
    }
}

Having tests depend on an entire test case is also possible.

class ATest extends PHPUnit_Framework_TestCase {
    public static $passed = true;

    protected function onNotSuccessfulTest(Exception $e)
        self::$passed = false;
        parent::onNotSuccessfulTest($e);
    }
}

You could improve these by tracking the names of the tests that failed so you could depend on a subset of tests for each case.

like image 36
David Harkness Avatar answered Oct 05 '22 10:10

David Harkness