如何解决当断言放在pytest中的main以外的其他线程中时,XML文件为空
在线程中的断言失败时,XML文件不会得到更新。
- Pytest版本-5.2.1
- 环境-Ubuntu 18.04
嗨,我编写了一个脚本,该脚本每10秒检查一次断言是否为真,并且在一分钟后(故意)失败。 脚本如下:
import datetime
import time
from threading import Thread,Condition
DURATION = 1 # 1 minute
START_TIME = 0
class Monitoring(Thread):
def __init__(self):
super(Monitoring,self).__init__()
def run(self):
global DURATION
while True:
if (datetime.datetime.now() - START_TIME).seconds <= DURATION*60:
assert 1+2 == 3,"No"
time.sleep(10)
else:
assert 1+3 == 5,"oh no"
break
def test_thread():
global START_TIME
START_TIME = datetime.datetime.now()
Monitoring().start()
以 test_for_thread.py 的名称存储此脚本。 还可以像这样执行此脚本
python3 -m pytest --junit-xml=error_report.xml test_for_thread.py
这就是我得到的
- generated xml file: /home/deepanshu/notebooks/error_report.xml -
============================== 1 passed in 0.01s ===============================
Exception in thread Thread-1:
Traceback (most recent call last):
File "/home/deepanshu/anaconda3/lib/python3.7/threading.py",line 926,in _bootstrap_inner
self.run()
File "/home/deepanshu/foglamp_workspace/csv_plugins/notebooks/test_for_thread.py",line 18,in run
assert 1+3 == 5,"oh no"
AssertionError: oh no
assert (1 + 3) == 5
我希望 error_report.xml 应该包含有关失败的信息。文件已创建,但其中没有任何内容。我怀疑pytest退出了,但是线程继续运行。有什么方法可以确保失败信息反映在xml文件中,或者可以强制pytest不退出。
任何帮助表示赞赏。 预先感谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。