如何在mark.parametrize之前运行夹具

问题描述 投票:0回答:1
Sample_test.py

@pytest.mark.parametrize(argnames="key",argvalues=ExcelUtils.getinputrows(__name__),scope="session")
def test_execute():
    #Do something

conftest.py

@pytest.fixture(name='setup',autouse=True,scope="session")
def setup_test(pytestconfig):
    dict['environment']="QA"

如上面的代码所示,我需要在test_execute方法之前运行安装程序夹具,因为getinputrows方法需要环境来读取工作表。不幸的是,参数化夹具在setup_test之前执行。有什么可能吗?

python pytest pytest-django
1个回答
0
投票

您需要在测试函数中而不是在装饰器中执行参数:

@pytest.mark.parametrize("key", [ExcelUtils.getinputrows], scope="session")
def test_execute(key):
    key(__name__)
    #Do something

或预先将__name__绑定到函数调用,但是再次在测试内部调用函数:

@pytest.mark.parametrize("key", [lambda: ExcelUtils.getinputrows(__name__)], scope="session")
def test_execute(key):
    key()
    #Do something

请介意,我还不完全了解您的工作,因此这些示例可能有意义,也可能没有意义。

© www.soinside.com 2019 - 2024. All rights reserved.