【发布时间】:2019-09-08 18:51:35
【问题描述】:
pytest - 我一直在尝试在 pycharm 终端中运行命令来执行 pytest 脚本。尝试运行代码时显示错误。 raise ValueError("选项名称 %s 已添加" % 冲突) ValueError:选项名称 {'--browser'} 已添加 在此处输入代码 来自终端的命令 - py.test -v -s test_file.py --browser firefox
**test_file.py**
import pytest
def test_command_line_methodA(oneTimeSetUp, setUp):
print("Running method A")
def test_command_line_methodB(oneTimeSetUp, setUp):
print("Running method B")
**conftest.py**
import pytest
@pytest.yield_fixture()
def setUp():
print("Running method level setUp")
yield
print("Running method level tearDown")
@pytest.yield_fixture(scope="module")
def oneTimeSetUp(browser, osType):
print("Running one time setUp")
if browser == 'firefox':
print("Running tests on FF")
else:
print("Running tests on chrome")
yield
print("Running one time tearDown")
def pytest_addoption(parser):
parser.addoption("--browser")
parser.addoption("--osType", help="Type of operating
system")
@pytest.fixture(scope="session")
def browser(request):
return request.config.getoption("--browser")
@pytest.fixture(scope="session")
def osType(request):
return request.config.getoption("--osType")
【问题讨论】:
-
您的代码在问题中的格式是否与您计算机上文件中的格式相同?你的pytest是什么版本的?格式正确,您的代码可以在我的电脑上使用 python 3.7.4 和 pytest-5.1.2 完美运行。
-
是的,代码格式正确。有两个文件 conftest.py 和 test_file.py 与上面提到的相同的代码。 python 3.7.3 & pytest 版本-5.1.2
标签: python-3.x pytest