【问题标题】:How to run fixture before mark.parametrize如何在 mark.parametrize 之前运行夹具
【发布时间】:2019-11-22 08:56:23
【问题描述】:
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 方法之前运行 setup 夹具,因为 getinputrows 方法需要环境来读取工作表。不幸的是,参数化夹具在 setup_test 之前执行。有什么办法可以做到吗?

【问题讨论】:

  • parametrize 不是固定装置,而是装饰器。装饰器在模块导入时进行评估,其中没有功能(包括固定装置)有机会执行。

标签: python pytest pytest-django


【解决方案1】:

你需要在测试函数内部执行参数,而不是在装饰器中:

@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

请注意,我并没有完全理解你在做什么,所以这些例子可能有意义,也可能没有意义。

【讨论】:

  • 我需要在Sample_test.py中的test_execute方法之前运行confest.py中的fixture!
  • 等等,你想让setup_test()test_execute()之前还是在ExcelUtils.getinputrows()之前执行?
  • 我希望 setup_test() 在 test_execute() 之前执行。
  • 我仔细检查了一遍。 setup_test()test_execute() 之前执行
  • 所以你希望getinputrows()setup_test() 之前执行,而不是test_execute()。正如我所说,这是行不通的。您需要在测试中调用 getinputrows()
猜你喜欢
  • 1970-01-01
  • 2011-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-13
  • 1970-01-01
  • 2020-08-19
  • 1970-01-01
相关资源
最近更新 更多