We're sunsetting PodQuest on 2025-07-28. Thank you for your support!
Export Podcast Subscriptions
cover of episode 221: How to get pytest to import your code under test

221: How to get pytest to import your code under test

2024/6/3
logo of podcast Test & Code

Test & Code

AI Deep Dive AI Insights AI Chapters Transcript
People
B
Brian Okken
Topics
Brian Okken: 我将讨论如何让测试代码导入被测试代码。这涉及到两种主要方法:一种是将被测试代码安装为pip可安装的包,另一种是使用pytest的pythonpath设置。 第一种方法适用于被测试代码是一个可安装的包的情况,我们可以使用pip install -e /path/to/local/package来安装它。这使得测试代码能够通过标准的import语句来导入被测试代码。 第二种方法则适用于被测试代码是单个文件、目录包或其他非标准包的情况。在这种情况下,我们需要在pytest的配置文件(例如pytest.ini、tox.ini或pyproject.toml)中设置pythonpath,指定被测试代码的路径。通过这种方式,pytest能够在运行测试时找到并导入被测试代码。 此外,pythonpath设置还可以用来指定测试辅助函数的路径,方便测试代码的编写和维护。 总而言之,通过这两种方法,我们可以有效地解决测试代码导入被测试代码的问题,确保测试能够顺利进行。

Deep Dive

Key Insights

What are the two main options discussed for enabling pytest to import code under test?

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.

Why might creating an installable package be beneficial even if you don't plan to distribute it?

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.

How does the `pythonpath` setting in pytest help with importing code under test?

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.

What is the advantage of using the `pythonpath` setting when running tests 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.

What additional functionality does the `pythonpath` setting provide beyond importing code under test?

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.

What will the next episode focus on regarding imports in Python projects?

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.

Chapters
This chapter introduces the problem of importing code under test in pytest, focusing on scenarios involving single Python files, modules, directory packages, and pip-installable packages. It sets the stage for exploring solutions in the following sections.
  • Code under test can be a single file, module, directory package, or installable package.
  • Tests and source code are typically in different directories.
  • The challenge is enabling tests to import code from a different directory.

Shownotes Transcript

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.
  • Using the pythonpath pytest setting).

** Learn pytest**