0

I'm adding test coverage to an API built in Symfony. I have an ApiControllerTest that is extending Symfony's WebTestCase, and performs tests on the different controller actions of the API controller. Please note these are functional tests so they are calling controller routes and and saving to the database.

I want the tests to run in a specific order. For example, there are API methods to save a project, match that project to another, add a requirement to that project, delete that project, etc. I understand that unit tests should not depend on each other, but it seems functional tests can. Instead of having set up and tear down methods that manually add / delete a project before and after every test, it would be much more efficient to save a project in the first test, then have subsequent tests update, delete, etc. for that project.

I have looked at Run PHPUnit Tests in Certain Order and tried using the @depends annotation, but this does not work (I'm not trying to pass the result of one test to another). Also, I've seen that you can specify the order for separate classes to run, but it seems like these tests are all related and belong in a single class, as they are testing a single controller (I typically have one test class per controller class, service class, etc. that I'm testing)

Is there a way using PHPUnit or Symfony's WebTestCase to specify the order that tests within a specific class should run? Or I'm a way off base on the design pattern here and need to use a different approach?

patrick3853
  • 1,100
  • 9
  • 17
  • 1
    I've tended to rebuild everything I need in a particular test. This means I can build up different settings to test failures as well as correct operations. You may find you need different project settings to test different features and how they react with each other. You can create helper methods to do some of the work so it saves rewriting code. I also find I sometimes want to re-run failed test, which wouldn't work if they rely on things already setup. – Nigel Ren Jul 27 '18 at 06:34
  • Thank you, after more googling I'm thinking that's the better approach. – patrick3853 Jul 27 '18 at 06:40

1 Answers1

-1

Even if you considered it but did not go that way, @depends is what you need. It's not meant only for passing values, but also for your exact need.

Also, even if the tests are functional so you don't pass objects or data between tests, the return value can be useful for passing the ID of the entity created in the previous test, in order for loading it in the next one.

gontrollez
  • 6,372
  • 2
  • 28
  • 36