2

Looking at the Pest documentation seems that there is no way to run test in a specific order, except renaming file with something like "01_Auth...", "02_Organization...". This doens't seem a standard or correct way to run tests that depend on others.

At the moment I have the following test files:

Feature
├── AuthTest.php
├── OrganizationTest.php
└── UserTest.php

So Pest will run tests in the following order:

1. AuthTest.php
2. OrganizationTest.php
3. UserTest.php

But I need to run tests in that order, since some tests depends on others:

1. UserTest.php
2. AuthTest.php
3. OrganizationTest.php

This is the flow:

UserTest.php register users on the database and AuthTest.php tries to authenticate with those Users. In order to create an Organization, it's necessary that there is at least 1 user associated, so it must be run after UserTest.php

How can I achieve this result in a standard way?

Veenz
  • 362
  • 2
  • 7
  • 2
    It doesn't seem there's support for this in PEST, consider switching to PHPUnit and using the `@depends` annotation. However you should not really aim to structure your tests like this. You should make an effort to have each test independent of other tests, that way you can also run tests in parallel or run individual tests quicker. In your case you can make a seeder to seed the users table for the tests that need a user to be in there (i,.e. the AuthTest etc) – apokryfos Dec 03 '22 at 12:38
  • This sounds right, but for integration test how can I test part of software that are strictly connected each other? Should I instead touch the Database directly with queries for testing that? – Veenz Dec 03 '22 at 14:25
  • 1
    After working with tests for at least 3 or 4 years, I can assure you, you must have your tests not relying on the others ones. Of course you can use `@depends` as @apokryfos mentioned (and I used them a lot, just for not running tests I know they would fail if the depended test did not pass, I am not sharing data between them with the `return` and `@depends`). Still, you asked if you should instead toch the Database directly and my answer is 100%, either with a seeder for each test, or inside the test. Remember you must ONLY have what it is expected to have related to the test, just that – matiaslauriti Dec 03 '22 at 18:03
  • @matiaslauriti Thank you so much for the clarification. At this point I can confirm I would try to avoid that, maybe I missunderstood how them work. Thank you so much apokrysof too. – Veenz Dec 03 '22 at 18:09

0 Answers0