PHPUnit Multiple Test Dependencies

The PHPUnit documentation for working with dependencies has a simple line that teases us where it says:

A test may have more than one @depends annotation.

Sadly, there are no examples of this in action and it leaves the door open for questions like, can more than one return value from a dependency be used? The answer is yes.

Let's look at a quick and dirty example with three tests:

function testFoo() {
  // Run tests.

  return 'foo';
}

function testBar() {
  // Run tests.

  return 'bar';
}

/**
 * @depends testFoo
 * @depends testBar
 */
function testBaz($foo, $bar) {
  $this->assertEquals('foobar', $foo . $bar);
}

If either testFoo or testBar fail testBaz will be skipped. Multiple return values are passed in to testBaz.

Using dependencies can be a great way to handle testing and multiple dependencies have many use cases where they can speed up testing and remove duplicate work.