【发布时间】:2017-11-22 07:00:03
【问题描述】:
想知道如何读取“Pytest”的命令行参数以获取输入,并使用该变量不作为带有夹具的测试输入,而是作为参数来执行一些其他操作。 这是我想要实现的目标:
pytest --folder=< label > test_my_logic.py
其中标签可以是 a、b 和 c。根据标签值,我将获得包含预期数据的实际“文件夹”路径。例如
- 标签=a,文件夹=common/test_data/a
- 标签=b,文件夹=common/test_data/b
我添加了 conftest.py 如下:
import pytest
def pytest_addoption(parser):
parser.addoption("--folder", action="store", default="All",
help="Please enter the folder which needs to be executed")
@pytest.fixture
def folder(request):
return request.config.getoption("--folder")
我有一个 json 和 util 方法,我可以使用它读取 json 以获取 a、b 等的实际文件夹值的值。我正在寻求帮助以了解我的脚本中如何获取参数 --folder 和用它来做其他操作,而不是在我的脚本中用夹具将它传递给测试方法?在我正在读取各种全局变量的测试脚本中,我有:
test_my_logic.py
>import pytest
import json
import sys
sys.path.append(os.path.realpath("%s/../../../../../common/utils" % os.path.dirname(os.path.abspath(__file__))))
import utils
print folder
TEST_ATTRIB = utils.getTestAttributes()['LOL']
PLACE_REQUEST_URL = utils.getURLs()['IMO']
...
...
在命令行中:
py.test --folder=a tests/test_my_logic.py
返回错误:
tests/test_my_logic.py:16: in <module>
print folder
E NameError: name 'folder' is not defined
提前致谢!
【问题讨论】:
标签: command arguments line pytest