【问题标题】:ValueError: option names {'--browser'} already addedValueError:选项名称 {'--browser'} 已添加
【发布时间】: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


【解决方案1】:

我无法重现您的错误。但是您的缩进与您发布的缩进相同,我认为问题出在于此。使用下面的代码,它可以正常工作:

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

【讨论】:

    猜你喜欢
    • 2019-08-02
    • 2019-12-14
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多