传递其他装饰器(例如灯具,模拟物)的参数时,如何正确使用pytest mark.parametrize装饰器?

时间:2019-04-26 09:11:39

标签: python mocking pytest

当我尝试将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 argumentsid的{​​{1}}尽管提供了,如图所示?

1 个答案:

答案 0 :(得分:1)

当一起使用 patch、parametrize 和 fixtures 时,顺序很重要: 拳头应该以相反的顺序提及修补值,然后以任何顺序在最后提及参数化值和装置。

> out
  name job_type subgraph_origin_id job_rate subgraph_size
1    4        0                  6     0.60             6
2    4        0                  7     1.00             3
3    3        0                  8     0.75             5
  no_(job_type==0)_in_subgrapgh no_(job_type==1)_in_subgrapgh
1                             1                             3
2                             1                             2
3                             1                             3
  no_(job_type==2)_in_subgrapgh no_(job_type==3)_in_subgrapgh
1                             1                             1
2                             0                             0
3                             0                             1