尝试在pytest.ini文件动态归档。testpaths选项决定pytest将从哪个目录收集测试。我想让用户能够选择测试目录a或{}。在
conftest.py
第一个钩子创建一个解析器选项。
第二个钩子拉动解析器选项读取值,并将testpath添加到config对象下的testpaths选项中。在@pytest.hookimpl()
def pytest_addoption(parser):
"""Creates a parser option"""
# Allows the user to select the test suite they want to run
parser.addoption("--suite", action="store", default="None"
, choices=['a', 'b']
, help="Choose which test suite to run.")
@pytest.hookimpl()
def pytest_configure(config):
print("Determining test directory")
suite = config.getoption("--suite")
if suite == "a":
config.addinivalue_line("testpaths", "tests/a")
elif suite == "b":
config.addinivalue_line("testpaths", "tests/b")
因此,如果我运行pytest --suite a,它应该加载a测试套件下的所有测试。它没有。它加载所有测试,就像选项不存在一样。在