【问题标题】:"ValueError: option names {'--testrail'} already added" when starting autotest with pytest-alure plugin使用 pytest-alure 插件启动自动测试时出现“ValueError:选项名称 {'--testrail'} 已添加”
【发布时间】:2019-12-14 04:18:34
【问题描述】:

尝试设置 pytest-testrail 插件。当我使用 --testrail --tr-config=path/to/config.cfg 参数开始测试运行时,它返回错误 ValueError: option names {'--testrail'} already added

我正在使用 appium+pytest+allure。它对我来说效果很好,但现在我正在尝试添加 testrail 集成。我正确设置了 pytest-testrail 插件(在 testrail 中成功创建了测试运行并带有我的自动测试列表),但是当我将 from pytest_testrail.conftest import * 添加到我自己的 conftest 文件时,返回 error 。 我尝试多次重新安装 pytest 和 pytest-testrail 插件,但没有帮助

conf测试文件:

import pytest
from appium import webdriver
from pytest_testrail.conftest import *
import sys
import os.path

sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir, os.pardir))
PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)

@pytest.fixture
def driver_setup(request):
    desired_caps = {
        //desired caps for appium driver
    }

    url = 'http://localhost:4723/wd/hub'

    request.instance.driver = webdriver.Remote(url, desired_caps)


    def teardown():
        request.instance.driver.quit()
    request.addfinalizer(teardown)

完整的错误描述:

Traceback (most recent call last):
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/_pytest/config/__init__.py", line 415, in _importconftest
    return self._conftestpath2mod[conftestpath]
KeyError: local('/Users/anton/autotests/conftest.py')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/helpers/pycharm/_jb_pytest_runner.py", line 31, in <module>
    pytest.main(args, plugins_to_load)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/_pytest/config/__init__.py", line 55, in main
    config = _prepareconfig(args, plugins)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/_pytest/config/__init__.py", line 200, in _prepareconfig
    pluginmanager=pluginmanager, args=args
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/hooks.py", line 289, in _call_
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/manager.py", line 87, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/manager.py", line 81, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/callers.py", line 203, in _multicall
    gen.send(outcome)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/_pytest/helpconfig.py", line 89, in pytest_cmdline_parse
    config = outcome.get_result()
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/_pytest/config/__init__.py", line 661, in pytest_cmdline_parse
    self.parse(args)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/_pytest/config/__init__.py", line 869, in parse
    self._preparse(args, addopts=addopts)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/_pytest/config/__init__.py", line 825, in _preparse
    early_config=self, args=args, parser=self._parser
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/hooks.py", line 289, in _call_
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/manager.py", line 87, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/manager.py", line 81, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/callers.py", line 208, in _multicall
    return outcome.get_result()
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/_pytest/config/__init__.py", line 724, in pytest_load_initial_conftests
    self.pluginmanager._set_initial_conftests(early_config.known_args_namespace)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/_pytest/config/__init__.py", line 364, in _set_initial_conftests
    self._try_load_conftest(anchor)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/_pytest/config/__init__.py", line 370, in _try_load_conftest
    self._getconftestmodules(anchor)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/_pytest/config/__init__.py", line 399, in _getconftestmodules
    mod = self._importconftest(conftestpath.realpath())
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/_pytest/config/__init__.py", line 449, in _importconftest
    self.consider_conftest(mod)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/_pytest/config/__init__.py", line 502, in consider_conftest
    self.register(conftestmodule, name=conftestmodule.__file__)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/_pytest/config/__init__.py", line 302, in register
    ret = super().register(plugin, name)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/manager.py", line 121, in register
    hook._maybe_apply_history(hookimpl)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/hooks.py", line 336, in _maybe_apply_history
    res = self._hookexec(self, [method], kwargs)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/manager.py", line 87, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/manager.py", line 81, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/callers.py", line 208, in _multicall
    return outcome.get_result()
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/pytest_testrail/conftest.py", line 20, in pytest_addoption
    help='Create and update testruns with TestRail')
  File "/Users/anton/MercuryTest/mercury_env/lib/python3.6/site-packages/_pytest/config/argparsing.py", line 304, in addoption
    raise ValueError("option names %s already added" % conflict)
ValueError: option names {'--testrail'} already added

这里是我的所有装饰器的测试用例示例:

import pytest
import allure
from pytest_testrail.plugin import pytestrail

@pytest.mark.usefixtures('driver_setup')
@allure.epic('application name ')
class Test_app_name:

    @allure.feature('feature')
    @pytestrail.case('C850')
    @allure.title('test title')
    @allure.severity(allure.severity_level.CRITICAL)
    def test_case_name(self):
        page = pageObjectClassWithLocatorsAndActions(self.driver)
        with allure.step('step'):
            page.do_something()

【问题讨论】:

  • 为什么要导入pytest_testrail.conftest
  • 如果不导入它的工作正常,在 testrail 中创建测试运行,但所有测试仍未测试。我认为这是因为部分代码 pytest-testrail 插件没有启动(当插件将测试用例推送到 testrail 时,我在控制台中看不到任何输出,我的 testran 刚刚完成,什么也没发生,集成没有单个错误),并且我认为,像在 GitHub 示例 (github.com/allankp/pytest-testrail/blob/master/tests/livetest/…) 中那样导入 pytest_testrail.conftest 将帮助我解决这个问题
  • 如果您可以运行pytest --testrail 而不会引发错误Unrecognized argument,则可以确定testrail 插件已正确启动。您的问题与插件无关,而是与您的测试有关 - 显示一个未找到的测试示例,包括代码中的测试函数签名(def test...(): ...,包括装饰器)及其完整路径(/Users/you/project/tests/test_module.py::TestCls::test_func[test_param] 等)。
  • 您所指的 Github 示例不是示例;这是插件作者用来从存储库导入代码以便能够在本地运行测试的一种解决方法。不要使用它。另外,当你不明白它在做什么时,不要盲目地复制一些代码(例如sys.path.append 行)——在你的情况下,它会将/Users/anton 目录添加到 Python 的模块搜索路径中。它弊大于利。
  • 感谢您的帮助和提示。 Pytest-testrail 插件成功创建了测试运行,我的所有自动测试都包含在 testrail 站点中,所以我认为插件启动正确。我在一般描述中添加了所有装饰器的测试用例示例。就我而言, pytets-testrail 不会在测试运行完成后标记所有通过/失败的案例。 Allure 生成正确的报告以及所有更正的测试用例状态

标签: python integration pytest testrail


【解决方案1】:

它可能与此处描述的问题重复 Pytest Testrail Module - Post Test Results for Test Runs

如果是这样,你不需要在自己的conftest中导入“pytest_testrail.conftest”

你需要在 "page.do_something()" 中断言一些东西来让 testrail 插件知道 "test_case_name" 的状态

【讨论】:

    猜你喜欢
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 2018-04-06
    • 2020-02-21
    相关资源
    最近更新 更多