如何在mark.parametrize

时间:2019-11-22 08:56:23

标签: python pytest pytest-django

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之前执行。有什么可能吗?

1 个答案:

答案 0 :(得分: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

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