Selenium 求助,pytest+allure 报错

fengzx120 · 2019年04月18日 · 最后由 张翔宇 回复于 2019年12月17日 · 1754 次阅读

最近在改 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 上也没有答案

共收到 4 条回复 时间 点赞

同问,楼主这个问题后续如何解决的,我是 pytest+allure 并发就出错了,但是单例跑是 OK 的

使用 pytest-xdist 插件也能生成 html 报告,完美支持 pytest-html 插件

pytest -n 3 --html=report.html --self-contained-html

刚试了可以,allure 没试过

fengzx120 回复

我们用的是 unittest + htmlrunner,改并发执行的时候,也会遇到问题,根本原因是要把不同线程执行后的结果汇总,pytest 直接提供的并发应该有 return 结果吧,我们直接用的 futures.ProcessPoolExecutor() 然后汇总结果,在一起写入 htmlrunner,我觉得你可以往这个方向查查看

我单个用例跑时没有报错,并发的时候就这样了,求助😭 😭 😭 😭

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册