February 2022
Intermediate to advanced
274 pages
6h 28m
English
The __init__.py file affects pytest in one way and one way only: it allows you to have duplicate test file names.
If you have __init__.py files in every test subdirectory, you can have the same test file name show up in multiple directories. That’s it—the only reason to have a __init__.py file.
Here’s an example:
| | $ cd /path/to/code/ch8/dup |
| | $ tree tests_with_init |
| | tests_with_init |
| | ├── api |
| | │ ├── __init__.py |
| | │ └── test_add.py |
| | ├── cli |
| | │ ├── __init__.py |
| | │ └── test_add.py |
| | └── pytest.ini |
We might want to test some add functionality both through the API and through the CLI, so having a test_add.py in both seems reasonable.
As long as we also have a __init__.py file in both the api and cli directories, ...
Read now
Unlock full access