Pytest 没有在 pytest.ini 中获取配置

Pytest not picking up configurations inside pytest.ini

我在测试期间尝试使用 pytest.ini 中的值,但一直收到错误 FAILED tests/test_post_train.py::test_config - ValueError: unknown configuration value: 'rmse'。我正在使用 .

中的方法概述

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

pytest_addoption是一个钩子,所以它应该在conftest.py而不是在测试文件

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