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

我们有一些要测试的代码和一些测试。测试需要能够导入被测试的代码,或者至少是它的 API,以便对其运行测试。我们该如何做呢?我们该如何设置才能让我们的测试导入我们的代码?在本集中,我们将讨论两种选择:使用 `pip install -e /path/to/local/package` 将被测试的代码安装为可通过 pip 安装的包。使用 pythonpath pytest 设置。 学习 pytestpytest 是 Python 的首选测试框架。通过 Hello, pytest! 快速学习基础知识。之后,你可以通过《The Complete pytest Course》成为 pytest 专家这两门课程都在 courses.pythontest.com</context> <raw_text>0 在本集和下一集中,我将讨论一下导入。在本集中,我们将讨论导入被测试的代码。在下一集中,我们将讨论从项目内部导入项目的 API。这部分是为了测试目的,部分是为了举例说明。但我们将在下一集中详细介绍。在本集中,我们将正确设置所有内容,以便我们的测试可以导入被测试的代码。现在让我们开始吧。

欢迎收听 Test and Code。本集由 HelloPyTest(学习 PyTest 的最快方法)和 Python Test 社区赞助播出。更多信息请访问 courses.pythontest.com。因此,我们有一些要测试的代码和一些测试。测试需要能够导入被测试的代码,或者至少是其 API,以便对其进行测试。让我们更具体一些。

假设我们的被测试代码是一个单独的 Python 文件,或者是一个包含需要测试的函数的模块,或者是一个包含 `dunderinit.py` 文件的 Python 文件和模块的目录。这有时被称为目录包或只是包,但这有点令人困惑,因为我们还有可通过 pip 安装的包。这是我们可以测试的另一种东西。

如果我们安装一个可安装的包,我将假设它是一个本地机器上的包。它不是 PyPI 上的东西。所以这就是我们要测试的代码。它可以是单个文件模块、目录包或可安装包。

现在是我们的测试代码。让我们假设我们的测试代码和我们的源代码位于不同的目录中。因此,为了讨论起见,我们有一个顶级项目目录,然后我们有一个源子目录(在我的例子中通常拼写为 SRC)和一个测试目录。

我们的一个测试文件是什么样的呢?在我们的测试文件中,它将只是 test_something.py。在其中,它将包含类似 import project 或 import project_name 或 import project.module 或 from project.module import func_name 的内容。你懂的。

不知何故,我已经在我的测试文件中包含了我正在测试的内容或我正在测试的项目的一部分。我必须导入该函数或模块或其他内容,以便我可以从测试代码中访问它。但是测试代码与源代码位于不同的目录中。那么这个导入是如何工作的呢?实际上,我们必须做两件事中的一件。我们必须将被测试的代码安装起来。

使用 pip。或者我们需要告诉 pytest 在哪里可以找到被测试的代码。因此,再次强调,我们的被测试代码可以是单个文件、目录包或可安装包。如果是可安装包,那就是

使用 pip 安装我们的被测试代码。我们只需说 pip install . dirname,它就会安装到我们的虚拟环境中。希望我们正在使用虚拟环境。那么其他情况呢?其他情况是棘手的部分。因此,如果它是一个单个文件模块或目录包,甚至是只是一些东西的目录,那就是困难的部分。这可能是你收听本集的原因。

但是,让我们不要立即否定打包这件事。在过去的几年里,打包变得容易多了,即使你并不打算通过像 pip 这样的包索引、本地存储库或你的工作存储库来分发你的项目,让我们来看一下……

采取额外的几个步骤来从你的目录包创建一个可安装的包可能非常容易,这可能是你想要考虑的事情。但是假设你没有这样做。假设你只想拥有这个目录,并且通常你会访问它。你知道你是如何访问它的,但是测试并没有那样看待它。所以我们需要告诉 PyTest 在哪里可以找到这段代码。

幸运的是,有一个专门为此设置的设置。它被称为 Python Path。我将在节目说明中添加一个指向相关文档的链接。其要点是在你的设置文件中,例如你的 pytest.ini 或 tox.ini 或 pyproject.toml,为了讨论起见,假设你的 pytest.ini 文件,它将位于你的顶级目录中。所以你的顶级项目目录,与你的源代码和测试代码位于同一级别的目录。

在这个文件中,你将有一行内容:PythonPath=SRC 或 source 或 source/subter 或我如何相对于该顶级目录访问我的代码。这就是你想要放在 PythonPath 中的设置。这实际上是我们需要做的所有事情,以告诉 PyTest 在哪里可以找到该导入并使该导入工作。

很酷的一点是,即使我们在测试时,如果你从该目录进行测试,如果你在顶级目录中使用 PyTest,PyTest 将在测试目录中查找,实际上会在所有目录中查找所有测试。在你的顶级目录中,它将找到测试,并且由于我们的 Python path 设置,它将找到被测试的代码。即使我们已经 cd 到测试目录的子目录中,

也许甚至像几层深一样,PyTest 仍然会找到那个 PyTest 任何文件。它将找到该 Python path 设置并将其设置为相对于该顶级内容。它仍然会找到源代码。所以这就是你需要做的所有事情,以使你的测试代码找到你的源代码。你可以使用 Python path 设置做的另一件事是,你可以设置一个指向你的辅助函数的路径。所以假设你的测试有一些

一些你定义的实用测试辅助函数。你可以将它们放在 tests/helper 或 lib 或任何地方,并从你的 Python path 设置中引用它。现在你可以使用测试辅助函数了。那么我们在下一集中将讨论什么呢?在下一集中,我们将专门研究项目的不同部分以及如何导入

将项目的这些不同部分导入到项目的其他部分。具体来说,我将解决一个问题,在这个问题中,我为我的命令行界面访问项目 API 采取了一种可能是非标准的方式。

感谢收听,并感谢所有通过购买课程支持节目的朋友,包括 Hello PyTest(学习 PyTest 的最快方法)和完整的 PyTest 课程,如果你想真正成为 PyTest 专家。两者都可以在 courses.pythontest.com 上找到,你也可以在那里加入 Python 测试社区。就到这里。现在去测试一些东西吧。