Cannot get pytest to understand command line arguments in settings

So, I'm trying to run pytest to run selenium tests in different environments based on some command line argument. But he continues to throw this error:

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

It seems that it understands that setup_class takes 2 arguments, but host not passed. Here is 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 

It's strange that host functions are visible by functions (therefore, if I do test_function and pass the host as a parameter, it gets it just fine), these are just setup tags that don't work,

I looked around and found this, pytest - to use funcargs inside setup_module , but it doesn't seem to work (and it is deprecated since version 2.3.

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

thanks

+7
source share
2 answers

setup_module / class / method and friends cannot work directly with pytest fixtures. The way pytest-2.3 makes the equivalent is to use autouse fixtures . If you can better pass fixtures for function testing or put a usefixtures marker in a class or module.

+1
source

To access the command line options inside the installation functions, you can use the pytest.config object. Here's an example ... adapt if necessary.

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

All Articles