Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

50%
+0 −0
Q&A How to use Python/pytest to organize unit and system tests?

It works fine to have tests in a parallel directory, as long as the Python environment is properly configured. When Pytest is properly installed in an environment, it will also provide a wrapper sc...

posted 11mo ago by Karl Knechtel‭

Answer
#1: Initial revision by user avatar Karl Knechtel‭ · 2025-10-29T18:30:21Z (11 months ago)
It works fine to have tests in a parallel directory, as long as the Python environment is properly configured. When Pytest is properly installed in an environment, it will also provide a wrapper script; so as long as that environment is active, a `pytest` command is active and it's not necessary to use `python -m pytest`.

Suppose we have a directory structure like
```text
.
├── LICENSE.txt
├── pyproject.toml
├── README.md
├── src
│   └── package
│       ├── __init__.py
│       └── code.py
└── test
    ├── speed_tests
    |   ├── test_1.py
    |   └── test_2.py
    └── quality_tests
        └── test_1.py
```
To set up testing, we can:

1. Create a virtual environment.
1. Install pytest in the virtual environment.
1. Do an *editable install* of our own project into the virtual environment.
1. Activate the virtual environment.
1. Run `pytest` from the project root.

When writing the test code, make sure to use *absolute* imports to refer to the code being tested. (Modules that are part of the project can and should continue to refer to each other with relative imports.)

Pytest will be available because the virtual environment is active. It will discover the tests automatically because they are found within the current working directory. The test code will be able to use *absolute* imports of the main code because the main project is also installed in the same environment. (Relative imports will not work, because the project root will not become a package directory; the entire point of this setup is to *not have to care about* how the `test` and `package` folders are positioned relative to each other on disk.)

This could look like (on Linux, using pip — of course, other options are possible as well):

```text
$ python -m venv .venv
$ source .venv/bin/activate
(.venv) $ pip install -e .
(.venv) $ pip install pytest
(.venv) $ pytest
```

This bootstraps a separate copy of pip into the virtual environment rather than using the system pip (if any); working around this is possible (and saves installation time and disk space) but requires additional effort.

Writing the actual Python code for testing (and understanding the results, getting results other than pass/fail etc.) is out of scope here; please consult the guides and examples offered in the [documentation](https://docs.pytest.org/en/stable/) and then ask separate, more specific questions if necessary.