Running Tests Sequentially in Rust

Ferdinand de Antoni
2 min readDec 26, 2019


You got your Cargo workspace all set up, with an api module and a database module. You created a nice bunch of tests to exercise the api you created, and when running cargo test things work great.

Next you work on the database module, and you create some tests that you run individually, and it works great too. Everything is looking great!

You now run cargo test again from the root of your project, expecting your api and database tests to pass with flying colours. Alas, they do not! You see your database tests fail with things like the following:

DatabaseError(__Unknown, "database is locked")

You quickly realise that Rust runs your tests in parallel, and some of your database tests are stepping on each other. So how to solve?

The first thing you can do is run every test serially by using only one thread for your tests:

cargo test -- --test-threads 1

This way, however, all your tests will run serially, and this is not really necessary. Your api tests can still run in parallel just fine. So how to run only specific tests serially?

The serial_test crate

With the serial_test crate you can decorate individual tests to run sequentially. So in our case, we can just decorate our tests in the database module as follows:

use serial_test::serial;#[test]
fn test_insert_record() {
// test insert
fn test_update_record() {
// test update

Now your api tests can still run in parallel, while your database tests that must run sequentially will run one at the time.