ChewToy ChewToy - 1 year ago 36
PHP Question

Can't get PHPUnit dependencies to work

I'm developing an API and I'm having trouble writing some of the testcases.
I have already written a group of general testcases to test all entities. These include the general operations such as create, modify and delete.

* @dataProvider provideCreateData
* @depends LoginTest::testLogin
public function testCreate

* @dataProvider provideModifyData
* @depends testCreate
function testModify

The dataproviders provide a list of API entities along with their respective attribute, the tests dynamically test all entities without us having to main the tests when there are future changes.

The above currently works fine but I'm having trouble integrating the other needed tests that are hardcoded, for things such as search-functions that don't follow a general pattern.
Obviously I can't test a find function without existing entities and as such the find test needs to run after the create test, which I'm having trouble doing. I tried using:

* @depends GeneralTest::testCreate
public function testFind() {}

Which doesn't work, the test doesn't run and is instead skipped for no obvious reason. However the test is run if i remove the dependency, but then I can no longer guarantee that there will exist entities in the database.

I feel like I'm missing something obvious, or maybe I misunderstood some important concept.

Answer Source

There are a couple of reasons a consumer test would be skipped.

  1. 'testFind' is defined before 'GeneralTest::testCreate'. PHPUnit does not change the order of execution to help a dependency; that is on you.
  2. An assertion in 'GeneralTest::testCreate' failed. If an assertion fails, the dependency has not been met so the method is skipped.

You should only use @depends if you really need to. You should strive to have each test method test a specific path of execution. If you think you need that dependency, you'll need to configure a test suite so that you can control the order of the tests.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download