- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 src/utils/helper.py
假设我在 utils/helper.py 中有一个名为 func_a
的函数,它在我的项目中的多个位置使用。
而且每次使用时都是这样导入的
from src.utils.helper import func_a
现在我想在我的测试中模拟这个 func_a
。
我想在 conftest.py 中创建一个 fixture ,这样我就不需要为每个测试文件一次又一次地编写模拟函数。
问题是,在我的模拟函数中,我不能这样写。
https://pypi.org/project/pytest-mock/
mocker.patch('src.utils.helper.func_a', return_value="some_value", autospec=True)
我必须为每个测试文件都这样写
mocker.patch('src.pipeline.node_1.func_a', return_value="some_value", autospec=True)
根据文档 https://docs.python.org/3/library/unittest.mock.html#where-to-patch
由于我要导入 func_a
之类的 from src.utils.helper import func_a
我必须模拟它的使用位置而不是定义位置。
但这种方法的问题是我无法在 conftest.py 的 fixture 中定义它
目录结构
├── src
│ ├── pipeline
│ │ ├── __init__.py
│ │ ├── node_1.py
│ │ ├── node_2.py
│ │ └── node_3.py
│ └── utils
│ ├── __init__.py
│ └── helper.py
└── tests
├── __init__.py
├── conftest.py
└── pipeline
├── __init__.py
├── test_node_1.py
├── test_node_2.py
└── test_node_3.py
最佳答案
好吧,正如您所写,如果您使用 from xxx import
,则必须使用该补丁。您的第一个选择当然是在生产代码中使用完整模块导入:
node_1.py
import src.utils.helper
def node_1():
src.utils.helper.func_a()
我相信你知道这一点,但我还是想提一下。
如果您不想更改生产代码,则必须根据已打补丁的模块进行打补丁,如您所写。这基本上意味着您必须动态构建补丁位置。如果您对测试函数和测试函数进行了对称命名,您可以这样做:
conftest.py
@pytest.fixture
def mock_func_a(mocker, request):
node_name = request.node.name[5:] # we assume that the test function name is "test_node_1" for testing "node_1"
module_path = f'src.pipeline.{node_name}.func_a'
mocked = mocker.patch(module_path,
return_value="some_value",
autospec=True)
yield mocked
如果无法从测试本身推导出补丁路径,则必须向测试函数添加更多信息。这可能只有在你想做的不仅仅是 fixture 中的补丁时才有意义——否则你也可以直接添加一个 patch
装饰器。
您可以添加具有模块路径或模块路径的一部分作为参数的自定义标记:
test_node_1.py
@pytest.mark.node("node_1")
def test_node(mock_func_a):
node_1()
mock_func_a.assert_called_once()
conftest.py
@pytest.fixture
def mock_func_a(mocker, request):
mark = next((m for m in request.node.iter_markers()
if m.name == 'node'), None) # find your custom mark
if mark is not None:
node_name = mark.args[0]
module_path = f'src.pipeline.{node_name}.func_a'
mocked = mocker.patch(module_path,
return_value="some_value",
autospec=True)
yield mocked
或者,如果您需要提供完整路径:
test_node_1.py
@pytest.mark.path("src.pipeline.node_1")
def test_node(mock_func_a):
...
conftest.py
@pytest.fixture
def mock_func_a(mocker, request):
mark = next((m for m in request.node.iter_markers()
if m.name == 'path'), None) # find your custom mark
if mark is not None:
node_name = mark.args[0]
module_path = f'{node_name}.func_a'
...
关于python - pytest mocker fixture mock 模块来自定义的位置而不是使用的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61848315/
我需要在整体超时的情况下停止测试用例,而不是在测试用例级别。 所以如果让我说我有 300 个测试用例,我想超时,总时间为 300 秒。 有没有办法做到这一点? 用于运行 pytest 的示例命令 py
我会默认使用一些参数( -n 2 )运行 pytest 但如果我只输入 pytest ... ,我不希望默认使用该参数直接运行pytest。这可能吗? 如果我包括这个: [pytest] addopt
给定以下模型: import pytest class DummyFile(pytest.File): def collect(self): yield DummyItem(s
对于 pytest,我正在使用库 pytest-dependency 设置依赖项.我还为这些测试添加了标记。这是一个 ECM: # test_test.py import pytest @pytest
我想使用逻辑来控制我的测试的顺序,这将在它们已经运行时动态重新排序它们。 我的用例是这样的:我正在使用 xdist 并行化我的测试,并且每个测试都使用来自公共(public)和有限池的外部资源。一些测
我需要标记要跳过的某些测试。但是,有些测试是参数化的,我只需要能够跳过某些场景。 我使用 py.test -m "hermes_only" 调用测试或 py.test -m "not hermes_o
问题是我给定的 fixture 函数具有外部依赖性,这会导致“错误”(例如无法访问的网络/资源不足等)。 我想跳过 fixture ,然后跳过任何依赖于该 fixture 的测试。 做这样的事情是行不
我正在试用 pytest首次。我如何抑制发出的关于我的代码所依赖的其他人的代码的警告而不抑制关于我自己的代码的警告? 现在我的 pytest.ini 中有这个所以我不必看到 pytest 警告我关于
我试图跳过依赖于命令行参数值的特定测试。我尝试使用 pytest.config.getoption("--some-custom-argument") 获取参数值就像这里描述的一样 related q
我目前使用的是 python 3.5.1 和 3.6 以及最新版本的 pytest。当使用参数化测试运行 pytest 时,我希望任何失败的测试仅显示失败的测试,而不是参数化测试的所有设置。 解释一下
在我的测试套件中,我有一些数据生成装置,用于许多参数化测试。其中一些测试希望这些装置在每个 session 中只运行一次,而另一些则需要它们运行每个功能。例如,我可能有一个类似于: @pytest.f
我想在运行时获取测试名称和测试结果。 我有 setup和 tearDown我的脚本中的方法。在 setup ,我需要获取测试名称,并在 tearDown我需要得到测试结果和测试执行时间。 有没有办法我
有没有办法在 PyTest fixture 中定义标记? 当我指定 -m "not slow" 时,我试图禁用慢速测试在 pytest 中。 我已经能够禁用单个测试,但不能禁用用于多个测试的 fixt
我最低限度地使用 pytest 作为针对工作中各种 API 产品的大型自动化集成测试的通用测试运行器,并且我一直在尝试寻找一个同样通用的拆卸函数示例,该函数在任何测试完成时运行,无论成功或失败。 我的
即使在写入管道时,如何强制 pytest 以颜色显示结果?似乎没有任何命令行选项可以这样做。 最佳答案 从 2.5.0 开始,py.test 有选项 --color=yes 从 2.7.0 开始,还应
作为一组更大的测试的一小部分,我有一套测试函数,我想在每个对象列表上运行。基本上,我有一组插件和一组“插件测试”。 天真地,我可以只列出一个带有插件参数的测试函数列表和一个插件列表,然后进行测试,我在
我想为 pytest-xdist 产生的每个子进程/网关创建一个单独的日志文件。是否有一种优雅的方法可以找出 pytest 当前所在的子进程/网关?我正在使用位于 conftest.py 的 sess
我的测试脚本如下 @pytest.fixture(scope="Module", Autouse="True") def setup_test(): ....................
我正在尝试像这样参数化我的类测试: @pytest.mark.parametrize('current_user', ["test_profile_premium", "test_profile_fr
我不明白如何正确运行一个简单的测试(功能文件和 python 文件) 与图书馆 pytest-bdd . 来自官方documentation ,我无法理解要发出什么命令来运行测试。 我尝试使用 pyt
我是一名优秀的程序员,十分优秀!