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
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...
#1: Initial revision
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.
