dbrumann
dbrumann

Reputation: 17166

How can I make a test rely on the outcome of another test

I have an AuthenticationService providing methods like performLogin, performSignup, and so on. When testing the performSignup-method I use 2 tests:

The first test creates a new user account and the second test attempts to create a user with the same identity (e.g. the same email-address). This leads to testSignupAmbigiousIdentity relying on a prior successful outcome from testSuccessfulSignup which I think is a bit clumsy, especially if a simple reordering of the tests (e.g. putting them in alphabetical order) might screw up the test procedure.

Are there any best practices as to how to handle tests relying on previous tests?

TL;DR How can I make a test-method rely on a successful output of another test-method using PHPUnit (3.6).

Upvotes: 0

Views: 87

Answers (3)

Tony Hopkinson
Tony Hopkinson

Reputation: 20330

Look up (or if you are using a wizard) just find setup and teardown. Idea is each test class has a method it runs before any test nmethods are executed and another after all selected tests have run.

Upvotes: 0

deceze
deceze

Reputation: 522523

You can use the @depends annotation.

Having said that, it's probably better to restructure the test to use fixtures to set up a known good state to test against instead of dynamically setting that state up in other, unrelated tests.

Upvotes: 2

Wesley van Opdorp
Wesley van Opdorp

Reputation: 14951

Any test you write should be able to run on its own, and not be dependant on other tests. Not only will your failed tests result point to the wrong test, there can also be scenario's where you only want to run a part of your tests (failed only comes into mind).

You should create a setUp that creates a static entry in your test table to test against for the testSignupAmbigiousIdentity method.

Upvotes: 1

Related Questions