我在根目录中有一个 conftest.py 文件。在里面我有这样的东西:
def pytest_addoption(parser):
parser.addoption("--log", action="store", default="")
parser.addoption("--validation", action="store", default="")
@pytest.fixture
def log_path(request):
return request.config.getoption("--log")
@pytest.fixture
def validation_path(request):
return request.config.getoption("--validation")
在命令行上,我这样称呼:
(venv)C:\Users\Me\MyProject>pytest --log "D:\temp\a_text_file.txt" --validation "D:\temp\a_json_file.json"
我收到这样的错误:
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --log --validation D:\temp\a_json_file.json
inifile: None
rootdir: C:\Users\Me\MyProject
我哪里错了?如您所见,我的数据文件与我的项目不在同一目录中,这是应该的方式;无论如何,我都需要一种输入这些文件的方法。尝试了很多东西。
如果我使用 3 个参数,将这些作为我的第二个和第三个参数,那么无论文件位于哪个目录,都可以找到这些文件。
解决了!一位聪明的同事输入了一个等号,现在它可以工作了,所以 '--log="D:\temp\a_text_file.txt"' 而不是 '--log "D:\temp\a_text_file.txt"'