Run Tests Sequentially?


#1

Wrote some tests for my web app, they’ll only be useful if they can be run sequentially, I’m using

cargo test -- --test-threads 1

yet it still doesn’t run them in the order they were defined in the file.


#2

Tests are generally meant to be independent of each other? Could you be more specific about what you’re trying to do?


#3

Like i said, I’m writing tests for my web app, which interacts with a database.

tests for the create_account REST endpoint should happen before the test for login REST endpoint etc.


#4

The obvious way would be to remove the #[test] annotation from your tests, and instead create a single test that calls your methods in the desired order.

I would still have many tests, maybe calling the same create_account from several of the before testing different aspects of using the account in different tests.


#5

Actually no.

Both tests should be independent. As in you should create dummy data so that you have sufficient data to test it on, run the test on the function, and then clear up the database after every test (if possible).

That way you know you won’t have false positives or hoaxes due to:

  • Tests failing due to improper data
  • Tests failing because another test deleted a row
  • Tests passing due to a certain cases that arise from the other tests. ( e.g. if one test deletes a row and the other test is asserting if that row is missing)
  • Tests failing due to the data being modified in another test.

It all comes down to one thing - SIDE EFFECTS. So you write your tests in isolation.


#6

This. If you want an example, I think Diesel’s test setup is pretty close to what you’re looking for, and it’s pretty readable, too.


#7

This is a little macro I use: https://github.com/Thomasdezeeuw/std-logger/blob/17fd34131a1b71063139e393de949a54990b2ef8/src/tests.rs#L14-L33.


#8

And while i’ll be following @dylan.dpc’s suggestion in subsequent tests for other endpoints. for now i just needed tests that could describe the auth flow.

i ended up adding numbers to the test names, because i realized the tests were being run in alphabetical order, not sure why though.

cargo test -- --test-threads 1 --nocapture + the numbers in the test names gives me exactly what i want for now, thanks everyone.