Can I use @depends to depend on a test using an @dataProvider?
php, phpunit, unit-testing
Solution
Normally `@dataProvider` is used when you want to run a test multiple times with different data sets for each. It exists to save you from writing looping code in the test and to allow different data sets to pass or fail individually.
As I said in my comments, I believe that PHPUnit will use either `@depends` or `@dataProvider`, and from your example my guess is that the second wins out. Another possibility is that tests with data providers cannot be used as dependencies because PHPUnit doesn't know which test-plus-dataset to pick.
Since `registerDataProvider` returns a single data set, you could just as easily call it from the test itself. This would allow `@depends` to work in the second test without the `@dataProvider`. Assuming that `testRegister` needs to modify `$device` and/or `$supposedResult`, this should work:
class DevicesTest extends PHPUnit_Framework_TestCase {
public function testRegister() {
list($device, $supposedResult) = $this->registerDataProvider();
//do a bunch of tests
//register a device in the DB
return array($device, $supposedResult);
}
public function registerDataProvider() {
return array("foo", "foo");
}
/**
* @depends testRegister
*/
public function testSaveDevicePreferences($data) {
list($device, $supposedResult) = $data;
// do stuff dependent on testRegister()
$this->assertEquals($device, $supposedResult);
}
}
If those variables don't need to be modified by the first test, you can simply call `registerDataProvider` from both tests. Note that PHPUnit will not separate a returned array from a dependend upon test into arguments to the dependent test as the data provider mechanism does. This is because it doesn't know that the array being returned is multiple arguments versus a single argument.
Problem
I have a testclass in which one test runs multiple times via a `@dataProvider` and another test that `@depends` on the first method. However, when I called `var_dump` on what should be passed to the second test, it gives me a solid `NULL`, which I didn't expect. In other words: what should this do: ``` <?php class DevicesTest extends PHPUnit_Framework_TestCase { /** * @dataProvider registerDataProvider */ public function testRegister($device, $supposedResult) { //do a bunch of tests return array($device, $supposedResult); } public function registerDataProvider() { return array(array("foo", "foo")); } /** * @depends testRegister */ public function testSaveDevicePreferences($deviceArr) { $this->assertNotEmpty($deviceArr); } } ?> ```