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
You can pass arguments to fixtures with the params keyword argument in the fixture decorator, and you can also pass arguments to tests with the @pytest.
To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative.
Pytest automatically identifies those files as test files. We can make pytest run other filenames by explicitly mentioning them. Pytest requires the test function names to start with test. Function names which are not of format test* are not considered as test functions by pytest.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With