user197674
user197674

Reputation: 798

Can I add ini style configuration to pytest suites?

I am using pytest to run tests in multiple environments and I wanted to include that information (ideally) in an ini style config file. I would also like to override parts or all of the configuration at the command line as well. I tried using the hook pytest_addoption in my conftest.py like so:

def pytest_addoption(parser):
    parser.addoption("--hostname", action="store", help="The host")
    parser.addoption("--port", action="store", help="The port")

@pytest.fixture
def hostname(request):
    return request.config.getoption("--hostname")

@pytest.fixture
def port(request):
    return request.config.getoption("--port")

Using this I can add the configuration info at the command line, but not in a config file. I also tried adding

[pytest]
addopts = --hostname host --port 311

to my pytest.ini file, but that didn't work. Is there a way to do this without building my own plugin? Thanks for your time.

Upvotes: 2

Views: 2646

Answers (2)

jxramos
jxramos

Reputation: 8266

Perhaps this example didn't work on earlier versions of pytest, but this actually runs and executes for me

test_proj/pytest.ini

[pytest]
addopts = --hostname host --port 311

test_proj/conftest.py

def pytest_addoption(parser):
    parser.addoption("--hostname", action="store", help="The host")
    parser.addoption("--port", action="store", help="The port")

@pytest.fixture
def hostname(request):
    return request.config.getoption("--hostname")

@pytest.fixture
def port(request):
    return request.config.getoption("--port")

test_proj/test_module.py

def always_true():
    assert True

def test_hostname(hostname):
    assert hostname == "HOST_X"

def test_port(port):
    assert port == "111"

and the corresponding console output

$ pytest test_proj
============================ test session starts ================================
platform linux -- Python 3.6.8, pytest-4.6.9, py-1.8.1, pluggy-0.13.1
rootdir: ./test_proj, inifile: pytest.ini
collected 2 items

test_proj/test_module.py FF                                                                                 [100%]

=================================== FAILURES ===================================
__________________________________ test_hostname _______________________________

hostname = 'host'

    def test_hostname(hostname):
>       assert hostname == "HOST_X"
E       AssertionError: assert 'host' == 'HOST_X'
E         - host
E         + HOST_X

test_proj/test_module.py:9: AssertionError
__________________________________ test_port ____________________________________

port = '311'

    def test_port(port):
>       assert port == "111"
E       AssertionError: assert '311' == '111'
E         - 311
E         + 111

test_proj/test_module.py:12: AssertionError
============================ 2 failed in 0.04 seconds ============================

Additionally it appears specifying at the commandline takes precedence over the ini value which is good to know

$ pytest test_proj --host HOST_X --port 111
=========================== test session starts =======================
platform linux -- Python 3.6.8, pytest-4.6.9, py-1.8.1, pluggy-0.13.1
rootdir: ./test_proj, inifile: pytest.ini
collected 2 items                                                                                                                                                                                                                       

test_proj/test_module.py ..                                                                                                                                                                                                       [100%]

======================== 2 passed in 0.01 seconds ========================

Upvotes: 0

Vikram Bhat
Vikram Bhat

Reputation: 46

The parser object does have an addini method as well that you can use to specify configuration options through an ini file. Here is the documentation for it: https://pytest.org/latest/writing_plugins.html?highlight=addini#_pytest.config.Parser.addini

addini(name, help, type=None, default=None)[source] registers an ini-file option.

Name: name of the ini-variable

Type: type of the variable, can be pathlist, args, linelist or bool.

Default: default value if no ini-file option exists but is queried.

The value of ini-variables can be retrieved via a call to config.getini(name).

Upvotes: 3

Related Questions