简体   繁体   English

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

[英]How to run fixture before mark.parametrize

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"

As shown in the code above, I need to run the setup fixture before the test_execute method because the getinputrows method requires the environment to read the sheet.如上面的代码所示,我需要在 test_execute 方法之前运行 setup 夹具,因为 getinputrows 方法需要环境来读取工作表。 Unfortunately, the parametrize fixture gets executed before the setup_test.不幸的是,参数化夹具在 setup_test 之前执行。 Is there any way this is possible?有什么办法可以吗?

You need to execute the parameter inside the test function, not in the decorator:需要在测试function里面执行参数,而不是在装饰器里面:

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

or bind __name__ to the function call beforehand, but again, call the function inside your test:或事先将__name__绑定到 function 调用,但同样,在测试中调用 function :

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

Mind you I am not fully understanding what you're doing, so these examples might or might not make sense.请注意,我并不完全理解您在做什么,因此这些示例可能有意义,也可能没有意义。

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

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