【问题标题】:Unable to run py.test from command line when using a marker and text fixture option使用标记和文本夹具选项时无法从命令行运行 py.test
【发布时间】:2013-04-03 17:26:54
【问题描述】:

我是这样设置灯具的:

def pytest_addoption(parser):
    parser.addoption('--env', action='store', default='qa', 
                     help='Specify environment: "qa", "aws", "prod".')

@pytest.fixture(scope='module')
def testenv(request):
    return request.config.getoption('--env')

当我针对文件名调用 py.test 时,这会起作用,例如:

  • py.test -v --env prod functionaltests/test_health_apps.py

但是当我使用标记调用 py.test 时它不起作用,如下所示:

  • py.test -m selenium --env prod
  • py.test -m 'selenium' --env prod
  • py.test --env prod -m selenium
  • py.test --env prod -m 'selenium'

这些返回:

用法:py.test [options] [file_or_dir] [file_or_dir] [...]

py.test:错误:没有这样的选项:--env

标记和命令行选项是否不兼容?

【问题讨论】:

    标签: python pytest


    【解决方案1】:

    它们是兼容的。我的猜测是您的配置文件 (conftest.py) 与您启动测试的目录不同。 (我这里可能错了)

    我的建议是为配置创建单独的文件:

    #configs.py
    def pytest_addoption(parser):
        parser.addoption('--env', 
        dest='testenv',
        choices=["qa","aws","prod"],
        default='qa', 
        help='Specify environment: "qa", "aws", "prod".')
    
    @pytest.fixture(scope='session')
    def testenv(request):
        return request.config.option.testenv
    

    并创建您将用作py.test 命令的runner.py

    #runner.py
    import pytest
    import sys
    import configs
    
    def main():    
        plgns = [configs]
        pytest.main(sys.argv[1:], plugins=plgns)
    
    if __name__=="__main__":
        main()
    

    那么你可以从python runner.py --env prod -m selenium开始
    这对我很有效。

    【讨论】:

    • 谢谢亚历克斯,这成功了。 conftest.py 与测试文件在同一目录中;不过,我将 runner.py 放在根目录中,这样我就可以避免将其路径用于命令行指令。
    猜你喜欢
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 2016-08-24
    • 2016-10-20
    • 1970-01-01
    • 2018-07-22
    • 2014-01-15
    • 2018-08-27
    相关资源
    最近更新 更多