这可能是由于多线程同时写入报告文件导致的。建议在使用多线程执行用例时,将BeautifulReporter的输出流重定向到一个单独的文件中,以避免多线程写入同一个文件导致的日志错乱问题。可以使用下面的代码实现:

import unittest
from ddt import ddt, data, unpack
from BeautifulReport import BeautifulReport
import threading

@ddt
class MyTestCase(unittest.TestCase):

    @data((1, 2), (3, 4), (5, 6))
    @unpack
    def test_add(self, a, b):
        print("Thread: ", threading.current_thread().name, "a=", a, "b=", b)
        self.assertEqual(a + b, 3)

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(MyTestCase)
    result = BeautifulReport(suite)
    # 将输出流重定向到一个单独的文件中
    with open('report.log', 'w') as f:
        result.report(filename='report.html', description='测试报告', report_dir='.', log_path=f)

在上面的代码中,我们使用了with open('report.log', 'w') as f语句将输出流重定向到一个名为report.log的文件中。这样,每个线程都会将日志写入自己的文件中,避免了多线程写入同一个文件导致的日志错乱问题。最后,我们调用result.report()方法生成HTML格式的测试报告,并将日志写入report.log文件中。

unittest使用ddt数据驱动后使用多线程执行用例后 BeautifulReporter生成的报告日志完全错乱

原文地址: http://www.cveoy.top/t/topic/bQCW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录