Peon
Peon

Reputation: 8020

Laravel 5 Unittest call Method Order

In what order, does Laravel 5 call test cases inside a single class?

Are they called from top to bottom? Alphabetically? Can I specify the order?

Meaning, I want to test API calls starting from: POST ( creating an order ), GET ( reading that newly created order ), DELETE ( deleting that order ). It would be nice, If I could do that as 3 separate automatic tests, but I don't know, if they will be always executed int that specific order.

Upvotes: 2

Views: 659

Answers (2)

Maxim Krizhanovsky
Maxim Krizhanovsky

Reputation: 26699

You can use @depends annotation to ensure one test runs after another, but as @scrubmx pointed out, your tests should not relay on the order of the execution. Otherwise if your createOrder tests fails, the rest of the test will not be executed. It's also harder to maintain a growing test suite if the data is created implicitly by another test case, and not explicitly in the one that makes operations on it, as you'll have more and more variations of the data you want to test against.

Upvotes: 1

scrubmx
scrubmx

Reputation: 2556

You should not relay on the order of the tests, instead do something like:

  1. Make a POST request and assert that the post was created.
  2. Create an order manually and then make a GET request and assert the order was found
  3. Create an order manually and then make a DELETE request and assert the order was deleted.

The data should not persist between tests, use the Illuminate\Foundation\Testing\DatabaseTransactionsor the Illuminate\Foundation\Testing\DatabaseMigrations.

See more info: https://laravel.com/docs/5.2/testing#resetting-the-database-after-each-test

Upvotes: 3

Related Questions