1

For a integration test I want to reuse test results. A dependency is defined via annotations. For the depending tests to be executed the result from previous tests needs to be available. Therefore the tests need to be executed in a fixed order. Otherwise tests which depend on other tests are skipped. To make sure the tests are executed in a fixed order, a test suite has been defined. Still the test with the dependency is skipped. Why is that?

ATest.php:

<?php use PHPUnit\Framework\TestCase; class ATest extends TestCase { public function testA() { self::assertTrue(true); return $this; } } 

BTest.php:

<?php use PHPUnit\Framework\TestCase; class BTest extends TestCase { /** * @depends ATest::testA() */ public function testB($a) { self::assertTrue(true); } } 

phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?> <phpunit verbose="true" > <testsuites> <testsuite name="dependency"> <file>ATest.php</file> <file>BTest.php</file> </testsuite> </testsuites> </phpunit> 

phpunit --testsuite dependency

PHPUnit 5.5.7 by Sebastian Bergmann and contributors.

Runtime: PHP 7.1.5 with Xdebug 2.5.4 Configuration: /phpunit.xml

.S 2 / 2 (100%)

Time: 49 ms, Memory: 4.00MB

There was 1 skipped test:

1) BTest::testB This test depends on "ATest::testA()" to pass.

OK, but incomplete, skipped, or risky tests! Tests: 1, Assertions: 1, Skipped: 1.

1 Answer 1

4

You can't have a test depend on a test in a different TestCase. The tests need to be contained in the same test case. Since the test is not in the test case, it gets treated like a failed test and the test is skipped when you run the tests.

Your tests need to be combined into one test in order for the dependency to work.

https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.test-dependencies

Part of the reason for this is that each of your tests should be isolated and be able to run in any order. Having a test depend on a test in a separate testcase means that the test files will need to run in a specific order. This can get complicated very easily with having circular test dependencies.

In addition, you now have things affecting tests that are not contained within your testcase. This can lead to a nightmare in maintaining the tests.

Sign up to request clarification or add additional context in comments.

1 Comment

It's worth mentioning that, if both TestCase classes are in the same TestSuite, it will work, provided that you're running the full TestSuite and not just a single test that depends on another.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.