【问题标题】:pytest.yield_fixture using cmd line optionspytest.yield_fixture 使用命令行选项
【发布时间】:2014-10-30 20:07:07
【问题描述】:

是否可以使用 pytest_addoption(parser) 创建一个供 pytest.yield_fixture 使用的列表?即

def pytest_addoption(parser):
  parser.addoption("-foo", action="store",defaults="1,2,3")

@pytest.yield_fixture(params=request.config.getoption('--foo').split(','))
def test_bar(request):
  do_something(request.param)

假设您有 6 个浏览器,并且您希望能够针对 1 个浏览器运行测试作为快速检查。在测试发现/生成之前,我无法弄清楚如何就位。帮助

【问题讨论】:

    标签: python selenium-webdriver pytest


    【解决方案1】:

    这显然不起作用,因为您的 request 变量在全局模块范围内不存在,这是在执行装饰器中的表达式时。解决这个问题的方法是使用pytest_generate_tests 钩子:

    # conftest.py
    def pytest_addoption(parser):
        parser.addoption('--foo', action='store', defaults='1,2,3')
    
    def pytest_configure(config):
        config.foo = config.getoption('foo').split(',')
    
    def pytest_generate_tests(metafunc):
        if 'foo' in metafunc.fixturenames:
            metafunc.parametrize('foo', metafunc.config.foo)
    
    # test_bar.py
    def test_bar(foo):
        do_something(foo)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多