pytest -我一直试图在pycharm终端中运行命令以执行pytest脚本。尝试运行代码时,显示错误。 引发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")
答案 0 :(得分:0)
我无法重现您的错误。但是您的缩进与您发布的缩进相同,我认为问题出在此。使用下面的代码,它可以正常工作:
conftest.py :
import pytest
@pytest.fixture()
def setUp():
print("Running method level setUp")
yield
print("Running method level tearDown")
@pytest.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")
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")
我也将yield_fixture
更改为fixture
,因为它是deprecated