1 minute read

I’m more and more convinced that Python developers should steal this great convention from Rust developers.

I talked briefly about colocating source code and test code in this previous blog post and in that blog post I mentioned someone on Reddit who mentioned Rust. Coincidentally, it just so happens that I’ve been reading up a bit about Rust and I found the official documentation talking about this and this is what they write.

Under the header Unit Tests the book writes:

The purpose of unit tests is to test each unit of code in isolation from the rest of the code to quickly pinpoint where code is and isn’t working as expected. You’ll put unit tests in the src directory in each file with the code that they’re testing. The convention is to create a module named tests in each file to contain the test functions and to annotate the module with cfg(test).

Under the header Integration Tests the book writes:

In Rust, integration tests are entirely external to your library. They use your library in the same way any other code would, which means they can only call functions that are part of your library’s public API. Their purpose is to test whether many parts of your library work together correctly. Units of code that work correctly on their own could have problems when integrated, so test coverage of the integrated code is important as well. To create integration tests, you first need a tests directory.

In summary, colocate the unit tests and source code and act like integration tests are tests external to your library and hence they should stay in a separate tests directory.