在我的 docker 图像上 运行 pytest with allure 失败,"invalid syntax"

Failed to run pytest with allure on my docker image,"invalid syntax"

我尝试在我的 docker 图像上使用 allure 运行 pytest,但它报告“语法无效”错误。 allure有python版本要求吗?我的 docker 图像上的 python 版本是 2.7.13。 谁能帮帮我?

root@ubuntu:/fuego-rw/buildzone# pytest allure_title.py 
Traceback (most recent call last):
  File "/usr/local/bin/pytest", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 65, in main
    config = _prepareconfig(args, plugins)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 214, in _prepareconfig
    pluginmanager=pluginmanager, args=args
  File "/usr/local/lib/python2.7/dist-packages/pluggy/hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pluggy/manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pluggy/manager.py", line 87, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/usr/local/lib/python2.7/dist-packages/pluggy/callers.py", line 203, in _multicall
    gen.send(outcome)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/helpconfig.py", line 94, in pytest_cmdline_parse
    config = outcome.get_result()
  File "/usr/local/lib/python2.7/dist-packages/pluggy/callers.py", line 81, in get_result
    _reraise(*ex)  # noqa
  File "/usr/local/lib/python2.7/dist-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 789, in pytest_cmdline_parse
    self.parse(args)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 997, in parse
    self._preparse(args, addopts=addopts)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 943, in _preparse
    self.pluginmanager.load_setuptools_entrypoints("pytest11")
  File "/usr/local/lib/python2.7/dist-packages/pluggy/manager.py", line 299, in load_setuptools_entrypoints
    plugin = ep.load()
  File "/usr/local/lib/python2.7/dist-packages/importlib_metadata/__init__.py", line 105, in load
    module = import_module(match.group('module'))
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/usr/local/lib/python2.7/dist-packages/_pytest/assertion/rewrite.py", line 304, in load_module
    exec(co, mod.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/allure_pytest/plugin.py", line 3, in <module>
    import allure
  File "/usr/local/lib/python2.7/dist-packages/allure.py", line 1, in <module>
    from allure_commons._allure import title
  File "/usr/local/lib/python2.7/dist-packages/allure_commons/__init__.py", line 3, in <module>
    from allure_commons._allure import fixture  # noqa: F401
  File "/usr/local/lib/python2.7/dist-packages/allure_commons/_allure.py", line 165
    def __call__(self, func: _TFunc) -> _TFunc:
                           ^
SyntaxError: invalid syntax

此语法:

func: _TFunc

称为类型提示,意味着变量 func 应为 _TFunc 类型(简单来说,num: int 表示变量 num 应为int).

type hint feature was only made available from Python3.0 (PEP 3107) and Python3.5 (PEP 484), 因此在您使用的 Python2.7.

版本中不可用
def func(num: int):
    print(num)

func(1)

使用Python2

  File "Main.py", line 1
    def func(num: int):
                ^
SyntaxError: invalid syntax

使用Python3

1

将您的 Python 版本升级到 >=3.5 或使用旧版本的 allure-pytest。我建议升级 Python,因为那样会更可持续。