LoB Principle: Where to put tests?

Up to now I use this pattern for my python files containing the tests:


After reading about the LoB Principle (The behaviour of a code unit should be as obvious as possible by looking only at that unit of code) I think
it might make more sense to use this pattern:


If you do it extreme, then you could put the test code into the production code, but this would bloat the memory, so I think a different file makes sense. But keeping it close to the original makes switching between production code and test code easier.

At least the pytest documentation does not consider this layout: Good Integration Practices — pytest documentation

What do you think?

Please keep following your current way of organizing.

Separation of concerns

As a developer you split your concerns. From your naming of folders I assume you separate views and models. These are things you want to separate because they are concerned with different objectives. The same applies with test code. It has a separate objective, keep it separate. When I look at some bug in the target system, at that time I am not interested in test code (yet).

The LoB principle

The LoB principle says you have to keep like code together. When you create fixtures for your view code, you will be creating instances of your models to supply the data your view will use. Keep this code close to the code that tests your models, i.e. in a test code directory. That code, and not the code under test are its closest relatives.

The practice of testing

When I start a coding session and before I go to commit it to source control I want to run all my tests. If these are in one directory I can start these by typing
python -m pytest /test/test*.py

So I think you should keep up the way of working you are using now.

1 Like