3

Maybe it's just me but @depends doesn't seem to be working as I'd expect it to. My code:

<?php
use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
    /*
     * @depends testFunc1
     */
    public function testFunc2()
    {
        exit('TEST FUNC 2 called');
    }

    public function testFunc1()
    {
        exit('TEST FUNC 1 called');
    }
}

When I do phpunit MyTest.php I'd expect to see TEST FUNC 1 called but instead I see TEST FUNC 2 called. As is it seems to just be running the tests in the order they appear in the script, regardless of the @depends attribute, which really begs the question: what does @depends actually do?

I'm running PHPUnit 5.7.20.

neubert
  • 15,947
  • 24
  • 120
  • 212
  • 1
    https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.test-dependencies --> "PHPUnit supports the declaration of explicit dependencies between test methods. Such dependencies do not define the order in which the test methods are to be executed but they allow the returning of an instance of the test fixture by a producer and passing it to the dependent consumers." – marcelog Jun 22 '17 at 01:03
  • Have you read the [documentation](https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.test-dependencies)? `@depends` works fine, but only when it is [used properly](https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.examples.StackTest2.php). There is no place for [`exit()`](http://php.net/manual/en/function.exit.php) in a test (not to be used in a function or method, in general). – axiac Jun 22 '17 at 01:05
  • 1
    @axiac - the point of the exit's was just to show which method was being called first. I could have done `echo "TEST FUNC 1/2 called\n";`, as well, but I figured `exit` would be slightly more clear. To get caught up in that is rather akin to being caught up in semantics. What's next? Saying it's a useless unit test because there aren't any assert's? – neubert Jun 22 '17 at 01:11
  • @neubert you can achieve the desired result by using [assertions](https://phpunit.de/manual/current/en/appendixes.assertions.html) that fail, by throwing un[expected exceptions](https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.exceptions) or by [marking the test as incomplete or skipped](https://phpunit.de/manual/current/en/incomplete-and-skipped-tests.html). In all these cases, PHPUnit reports the affected tests at the end of its report. – axiac Jun 22 '17 at 06:48

1 Answers1

11

You need to use /** instead of /* to start a docblock.

Sebastian Bergmann
  • 7,837
  • 1
  • 27
  • 35