如何解决通过位桶管道中的xdist并行运行pytest的错误



我们在比特桶管道中为python repo执行单元测试。最近我们开始使用pytest xdist(https://pypi.org/project/pytest-xdist/)包,并在通过coverage run -m pytest -n auto命令本地运行时成功并行运行我们的单元测试。这将在本地机器上的所有可用cpu上执行所有单元测试。然而,当我们通过位桶管道使用相同的命令运行测试时,我们会遇到以下异常。关于如何解决这个问题,有什么建议吗?

+ coverage run -m pytest -n auto
============================= test session starts ==============================
platform linux -- Python 3.7.5, pytest-5.4.3, py-1.10.0, pluggy-0.13.1
rootdir: /opt/atlassian/pipelines/agent/build
plugins: celery-4.4.7, forked-1.3.0, xdist-2.2.1, mock-3.5.1
gw0 I / gw1 I / gw2 I / gw3 I
INTERNALERROR> def worker_internal_error(self, node, formatted_error):
INTERNALERROR>         """
INTERNALERROR>         pytest_internalerror() was called on the worker.
INTERNALERROR>     
INTERNALERROR>         pytest_internalerror() arguments are an excinfo and an excrepr, which can't
INTERNALERROR>         be serialized, so we go with a poor man's solution of raising an exception
INTERNALERROR>         here ourselves using the formatted message.
INTERNALERROR>         """
INTERNALERROR>         self._active_nodes.remove(node)
INTERNALERROR>         try:
INTERNALERROR> >           assert False, formatted_error
INTERNALERROR> E           AssertionError: Traceback (most recent call last):
INTERNALERROR> E               File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 191, in wrap_session
INTERNALERROR> E                 session.exitstatus = doit(config, session) or 0
INTERNALERROR> E               File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 246, in _main
INTERNALERROR> E                 config.hook.pytest_collection(session=session)
INTERNALERROR> E               File "/usr/local/lib/python3.7/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR> E                 return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR> E               File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR> E                 return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR> E               File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 87, in <lambda>
INTERNALERROR> E                 firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR> E               File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR> E                 return outcome.get_result()
INTERNALERROR> E               File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR> E                 raise ex[1].with_traceback(ex[2])
INTERNALERROR> E               File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR> E                 res = hook_impl.function(*args)
INTERNALERROR> E               File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 257, in pytest_collection
INTERNALERROR> E                 return session.perform_collect()
INTERNALERROR> E               File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 453, in perform_collect
INTERNALERROR> E                 self.config.pluginmanager.check_pending()
INTERNALERROR> E               File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 277, in check_pending
INTERNALERROR> E                 % (name, hookimpl.plugin),
INTERNALERROR> E             pluggy.manager.PluginValidationError: unknown hook 'pytest_warning_recorded' in plugin <__channelexec__.WorkerInteractor object at 0x7f11772d84d0>
INTERNALERROR> E           assert False
INTERNALERROR> 
INTERNALERROR> /usr/local/lib/python3.7/site-packages/xdist/dsession.py:187: AssertionError
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 191, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/usr/local/lib/python3.7/site-packages/_pytest/main.py", line 247, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "/usr/local/lib/python3.7/site-packages/pluggy/hooks.py", line 286, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 93, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 87, in <lambda>
INTERNALERROR>     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR>   File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/usr/local/lib/python3.7/site-packages/xdist/dsession.py", line 112, in pytest_runtestloop
INTERNALERROR>     self.loop_once()
INTERNALERROR>   File "/usr/local/lib/python3.7/site-packages/xdist/dsession.py", line 135, in loop_once
INTERNALERROR>     call(**kwargs)
INTERNALERROR>   File "/usr/local/lib/python3.7/site-packages/xdist/dsession.py", line 175, in worker_workerfinished
INTERNALERROR>     self._active_nodes.remove(node)
INTERNALERROR> KeyError: <WorkerController gw0>

提前感谢您的帮助!

我也遇到了同样的问题,但在将pytest升级到6.2.2后,我无法解决这个问题

发现问题!我运行的是旧版本的pytest。将最新版本的pytest(在撰写本文时是pytest==6.2.2到我的requirements.txt(固定在一起解决了这个问题。随后的一个问题是,more-itertools包没有内置到我配置的位桶管道容器的python环境中,因此也将其固定在要求中

最新更新