Search code examples
pythonpytest

Pytest not picking up configurations inside pytest.ini


I am trying to use values that I have in pytest.ini during testing but keep getting an error FAILED tests/test_post_train.py::test_config - ValueError: unknown configuration value: 'rmse'. I am using the method outline in this similar question.

pytest.ini:

[pytest]
rmse = 40_000
inference_time = 0.5

tests/test_post_train.py:

def pytest_addoption(parser):
    parser.addini("rmse", "Min RMSE score for a model to past post-train test")
    parser.addini(
        "inference_time", "Max inference time for the model to be making predictions"
    )


def test_config(request):
    score = request.config.getini("rmse")
    assert score == 40_000

Solution

  • pytest_addoption is a hook, so it should be in conftest.py and not in the test file

    conftest.py:

    def pytest_addoption(parser):
        parser.addini("rmse", "Min RMSE score for a model to past post-train test")
        parser.addini(
            "inference_time", "Max inference time for the model to be making predictions"
        )
    

    test_post_train.py:

    def test_config(request):
        score = request.config.getini("rmse")
        assert int(score) == 40_000