python - 在为其他装饰器(例如夹具、模拟)传递参数时如何正确使用 pytest mark.parametrize 装饰器?
问题描述
当我尝试将 mark.parametrize 与其他装饰器一起使用时,如下所示:
@pytest.fixture(autouse=True)
def some_fixture()
db = create_db()
yield db
db.close()
@pytest.mark.parametrize('id, expected',[(1,1),(2,2)])
@mock.patch('some_module')
def some_test(mock_module, id, expected, db):
mock_module.function.return_value = 1
connection = db.connection()
assert expected my_function(id, connection)
我有两个问题:
一般一:放置参数和对装饰器排序的自然顺序是什么?
特定于上面的代码:为什么我会得到错误:
missing 2 required positional arguments
对于两者id
,expected
即使它们是如图所示提供的?
解决方案
当一起使用 patch、parametrize 和 fixtures 时,顺序很重要:拳头应该以相反的顺序提及 patched 值,然后以任意顺序在最后提及参数化值和 fixtures。
@pytest.fixture
def some_fixture1()
pass
@pytest.fixture
def some_fixture2()
pass
@pytest.mark.parametrize('id, expected',[(1,1),(2,2)])
@mock.patch('some_module1')
@mock.patch('some_module2')
@mock.patch('some_module3')
def some_test(
some_module3,
some_module2,
some_module1,
id,
expected,
some_fixture1,
some_fixture2,
):
pass
推荐阅读
- php - 解析错误:语法错误,第 9 行 C:\wamp64\www\bluepeatshop\db_connect.php 中的意外 ':'
- javascript - 为什么 require() 在交互式 shell 中返回 `{}`?
- java - 用 JAVA 看不到元素 - Selenium
- html - 纸牌游戏不能在纸牌之间放置空白空间
- javascript - 如何使用不同的路线进行反应?
- c# - 如何测试是否没有调用事件
- javascript - 在Javascript中组合字符串
- ffmpeg - 用 x264 编译 ffmpeg
- java - BlueJ 字符串问题
- kendo-ui - 将 Kendo UI 下拉样式应用于选择元素