221: How to get pytest to import your code under test
Jun 3, 2024
auto_awesome
Exploring how to make pytest import your code under test including options like installing code with pip install -e /path/to/local/package and using the pythonpath pytest setting. Discussing the challenges of importing code for pytest and the benefits of packaging for distribution. Learning how to set the Python path in PyTest configuration files for code import.
Setting up Python path in pytest settings is crucial for successful code import in testing.
Install the code under test as a pip installable package to allow test code import.
Deep dives
Importing Code Under Test
In this podcast episode, the focus is on importing the code under test for testing purposes. The host explains the importance of setting up the testing environment correctly so that tests can access the code under test. Whether it's a single Python file, a module with functions, or a directory package, the challenge lies in allowing the test code to import the necessary components.
Setting Up Python Path for Imports
The episode delves into the intricacies of establishing the Python path for imports to facilitate testing. Whether dealing with a single file module, a directory package, or an installable package, the key is to inform PyTest where to locate the code under test. By configuring the Python path in the settings file, such as PyTest, TOX, or PyProject.toml, users can dictate the path to the code relative to the top project directory, enabling seamless import functionality for testing.
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:
Installing the code under test as a pip installable package with `pip install -e /path/to/local/package`.