最近在改 selenium+unittest 的 UI 自动化测试项目,用 pytest 代替 unittest,现在遇到个问题,pytest 结合 allure 使用
比如我有 2 个测试用例,我使用 pytest 的并发测试的插件 pytest-xdist,命令:pytest -n auto -m form,同时跑 2 个用例,这样子没有问题,不会报错,全部跑完会提示运行了 2 个测试用例,耗时多久。
但当我结合 allure 的时候,命令:pytest -n auto -m form --alluredir=UIreport,每次用例跑完(很奇怪,就是最后一个用例跑完后,本来应该关闭浏览器的,但他就是不关),然后提示我只运行了 1 个测试用例,中间会报错,报错如下

DevTools listening on ws://127.0.0.1:52495/devtools/browser/d45e5c01-e7d7-4af7-b035-d9043a14f6c3

DevTools listening on ws://127.0.0.1:52497/devtools/browser/a2178afe-099d-458b-8e3b-81ff683ef97d
.INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "c:\python3\lib\site-packages_pytest\main.py", line 209, in wrap_session
INTERNALERROR> session.exitstatus = doit(config, session) or 0
INTERNALERROR> File "c:\python3\lib\site-packages_pytest\main.py", line 249, in main
INTERNALERROR> config.hook.pytest_runtestloop(session=session)
INTERNALERROR> File "c:\python3\lib\site-packages\pluggy\hooks.py", line 289, in __call
_
INTERNALERROR> return self.hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR> File "c:\python3\lib\site-packages\pluggy\manager.py", line 68, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR> File "c:\python3\lib\site-packages\pluggy\manager.py", line 62, in
INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR> File "c:\python3\lib\site-packages\pluggy\callers.py", line 208, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "c:\python3\lib\site-packages\pluggy\callers.py", line 80, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "c:\python3\lib\site-packages\pluggy\callers.py", line 187, in _multicall
INTERNALERROR> res = hook_impl.function(args)
INTERNALERROR> File "C:\python3\lib\site-packages\xdist\dsession.py", line 115, in pytest_runtestloop
INTERNALERROR> self.loop_once()
INTERNALERROR> File "C:\python3\lib\site-packages\xdist\dsession.py", line 138, in loop_once
INTERNALERROR> call(
*kwargs)
INTERNALERROR> File "C:\python3\lib\site-packages\xdist\dsession.py", line 244, in worker_logfinish
INTERNALERROR> self.config.hook.pytest_runtest_logfinish(nodeid=nodeid, location=location)
INTERNALERROR> File "c:\python3\lib\site-packages\pluggy\hooks.py", line 289, in __call
_
INTERNALERROR> return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR> File "c:\python3\lib\site-packages\pluggy\manager.py", line 68, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR> File "c:\python3\lib\site-packages\pluggy\manager.py", line 62, in
INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR> File "c:\python3\lib\site-packages\pluggy\callers.py", line 203, in _multicall
INTERNALERROR> gen.send(outcome)
INTERNALERROR> File "c:\python3\lib\site-packages\allure_pytest\listener.py", line 205, in pytest_runtest_logfinish
INTERNALERROR> uuid = self._cache.pop(nodeid)
INTERNALERROR> File "c:\python3\lib\site-packages\allure_pytest\listener.py", line 261, in pop
INTERNALERROR> return self._items.pop(str(_id))
INTERNALERROR> KeyError: 'test_form_002.py::TestForm002::test_form_002'

====================================================================== 1 passed in 86.58 seconds ======================================================================

我其实运行了 2 个(浏览器都打开了,步骤都跑了),但他只说运行了 1 个,然后报告里就只显示 1 个用例的报告,求教怎么解决 pytest 结合 allure 并发测试的这个问题?这个 keyError 我百度了好久,Stack Overflow 上也没有答案


↙↙↙阅读原文可查看相关链接,并与作者交流