Can't get pytest to understand command-line arguments on setups

蓝咒 提交于 2019-12-18 13:26:31

问题


So I have been trying to get pytest to run selenium tests on different environments based on some command-line argument. But it keeps throwing this error:

TypeError: setup_class() takes exactly 2 arguments (1 given)

It seems that it is understanding that setup_class takes 2 arguments, but host is not being passed. Here's the code for setup_class:

def setup_class(cls, host):
        cls.host = host

And here is the conftest.py file:

def pytest_addoption(parser):
    parser.addoption("--host", action="store", default='local',
        help="specify the host to run tests with. local|stage")

@pytest.fixture(scope='class')
def host(request):
    return request.config.option.host

What is strange is that host is being seen by the functions (so if I make a test_function and pass host as a parameter, it gets it just fine), it is just the setup fixtures that are not working.

I looked around and found this, pytest - use funcargs inside setup_module but that doesn't seem to be working (and it is outdated since 2.3.

Does anyone know what I'm doing wrong? Using py.test 2.3.2.

Thanks


回答1:


setup_module/class/method and friends cannot work with pytest fixtures directly. The pytest-2.3 way of doing the equivalent is to use autouse fixtures. If you can it's better to pass fixtures explicitely to test functions or to put a usefixtures marker on a class or module.




回答2:


To access the command line options from inside the setup functions, you can use the pytest.config object. Here is an example... adapt as needed.

import pytest

def setup_module(mod):
    print "Host is %s" % pytest.config.getoption('host')


来源:https://stackoverflow.com/questions/13275738/cant-get-pytest-to-understand-command-line-arguments-on-setups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!