使用了ddt数据驱动然后多线程执行用例BeautifulReporter的testresult中的log不正确
这个问题可能是由于多线程执行用例导致的。由于多个线程同时执行测试用例,因此在写入日志时可能会出现竞争条件,从而导致日志记录不正确。为了解决这个问题,您可以使用线程锁来确保每个线程都可以正确地写入日志。
以下是一个使用线程锁的示例代码:
import threading
from BeautifulReport import BeautifulReport
lock = threading.Lock()
@ddt
class MyTestCase(unittest.TestCase):
@data(...)
def test_my_case(self, ...):
with lock:
self.log.info("test_my_case start")
# 执行测试用例
self.log.info("test_my_case end")
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(MyTestCase)
result = BeautifulReport(suite)
result.report(description='My Test Report', filename='my_report.html')
在上面的代码中,我们使用了一个名为“lock”的线程锁来确保每个线程都可以正确地写入日志。在每个测试用例的开始和结束处,我们使用了“with lock”语句来获取锁并写入日志。这样,我们就可以避免多个线程同时写入日志而导致的竞争条件。
请注意,这只是一个示例代码。您需要根据自己的具体情况来修改代码以实现正确的日志记录。
原文地址: https://www.cveoy.top/t/topic/bQhT 著作权归作者所有。请勿转载和采集!