python - 如何在不导入的情况下使用 py.test fixtures

标签 python pytest

假设我有一个文件 fixtures.py,它定义了一个名为 foobar 的简单 py.test fixture。

通常我必须导入该 fixture 才能使用它(包括所有子 fixture ),如下所示:

from fixtures import foobar

def test_bazinga(foobar):

请注意,我也不想使用星号导入。

我如何导入这个 fixture ,这样我就可以写:

import fixtures

def test_bazinga(foobar):

这可能吗?看起来是这样,因为 py.test 本身就定义了这样的固定装置(例如 monkeypatch)。

最佳答案

固定装置及其可见性在 pytest 中有点奇怪。它们不需要导入,但如果您在 test_*.py 文件中定义它们,它们将仅在该文件中可用。

但是您可以将它们放在(项目或子文件夹范围内)conftest.py在多个文件中使用它们。

pytest-internal fixtures 简单地定义在 core plugin 中,因此随处可用。事实上,conftest.py 基本上就是一个目录插件。

您还可以运行 py.test --fixtures 来查看 fixture 来自哪里。

关于python - 如何在不导入的情况下使用 py.test fixtures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36452363/

相关文章:

python - 在 pandas json 中添加一个更高一级的对象

python - 自动检测测试耦合

python - Docker 构建 pytest :Failed to establish a new connection: [Errno 111] Connection refused

python - 为什么修补析构函数 (__del__) 对失败的测试不起作用?

python - Flask 单击命令单元测试 - 如何使用带有 "with_appcontext"装饰器的测试应用程序?

python - cpu.c :253: x264_cpu_detect: Assertion

python - 重命名 python 临时文件

python - 如何在 pytest 中的 mark.parametrize 中使用固定装置

python - ttk 标签小部件中的多个图像

Python subprocess.Popen 不加入