繁体   English   中英

在为其他装饰器(例如固定装置、模拟)传递参数时如何正确使用 pytest mark.parametrize 装饰器?

[英]how to properly use pytest mark.parametrize decorator when passing arguments for other decorators (e.g fixtures, mocks)?

当我尝试将 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)

我有两个问题:

  • 一般一:在有多个参数的情况下,放置参数和排序装饰器的自然顺序是什么?

  • 特定于上述代码:为什么我会收到错误: idexpectedmissing 2 required positional arguments ,即使它们是按所示提供的?

当同时使用 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM