The two main options are: 1) Installing the code under test as a pip installable package using `pip install -e /path/to/local/package`, and 2) Using the `pythonpath` pytest setting to specify the location of the code under test.
Creating an installable package simplifies the process of importing and testing code, as it allows the code to be installed into a virtual environment using pip. This approach is increasingly easier to implement and can streamline testing workflows, even for local projects not intended for distribution.
The `pythonpath` setting in pytest allows you to specify the directory where the code under test is located. By adding a line like `pythonpath = src` in the `pytest.ini` file, pytest can locate and import the code from the specified directory, even when tests are run from subdirectories.
The `pythonpath` setting ensures that pytest can still locate the source code and test files, even if you run tests from deep within subdirectories. It maintains the relative path to the top-level directory, making it easier to organize and run tests across complex project structures.
The `pythonpath` setting can also be used to import test helper functions or utility modules. By specifying the path to a directory containing these helpers, you can reuse them across multiple test files, improving code organization and reducing redundancy.
The next episode will focus on importing different parts of a project into other parts of the project, particularly addressing how the command-line interface can access the API of a project. This will include discussing non-standard import approaches and their implications.
We've got some code we want to test, and some tests.The tests need to be able to import the code under test, or at least the API to it, in order to run tests against it.How do we do that? How do we set things up so that our tests can import our code?
In this episode, we discuss two options:
pip install -e /path/to/local/package
.** Learn pytest**