unittest系统(七)产生漂亮的测试报告

之前分享了一系列的文章,分别从原理,运行,断言,执行,测试套件,如何跳过用例来讲解unittest,如何初始化一次,那么我们继续分享漂亮的html测试报告。

  在之前我们的测试报告中,我们有测试报告,但是原生的给我们带的比较简单,我们要想出来漂亮的html测试报告,是否可以呢,答案是可以的,这里我展示两个漂亮的unittest的测试报告,简洁大方。

        github地址:https://github.com/easonhan007/HTMLTestRunner

        下载后,我们直接复制BSTestRunner.py到项目目录下,我们在代码中演示下

复制代码
import unittest
from  BSTestRunner import BSTestRunner

class TestDemo(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testEqual(self):
        self.assertEqual("12","122")
def suite():
    suite = unittest.TestSuite()
    suite.addTests(unittest.TestLoader().loadTestsFromName("testdemoone.TestDemo"))
    return suite

if __name__ == "__main__":
    report="test.html"
    suitone=suite()
    openone= open(report,'w+')
    bstest=BSTestRunner(title="演示",description="演示测试报告",stream=openone)
    bstest.run(suitone)
复制代码

我们展示下 运行的结果。

 

 

这是第一个开源的库,我们去看下另外一个好看的html测试报告的库。

          开源地址:https://github.com/TesterlifeRaymond/BeautifulReport,这是有mock大佬开源。

         我们下载后直接复制BeautifulReport.py,template到项目路径下。创建一个路径report。我们复制到本地所以,我稍微改动了 PATH。

class PATH:
    """ all file PATH meta """
    config_tmp_path = os.getcwd() +'/template/template'

 我们如何组织用例呢。

复制代码
import unittest
from BeautifulReport import BeautifulReport

class TestDemo(unittest.TestCase):
    def setUp(self):
        pass
    def tearDown(self):
        pass

    def testEqual(self):
        self.assertEqual("12","122")
def suite():
    suite = unittest.TestSuite()
    suite.addTests(unittest.TestLoader().loadTestsFromName("testdemoone.TestDemo"))
    return suite
if __name__ == "__main__":
    report = "test.html"
    suitone = suite()
    result = BeautifulReport(suitone)
    result.report(filename='test.html', description='测试deafult报告', log_path='report')
复制代码

我们看下运行结果

 

 

 我们看下测试报告。

 

 

 

 新版本的还可以展示图片,大家可以结合自己的实际情况使用,使用方法可以见官网。https://github.com/TesterlifeRaymond/BeautifulReport。

大家可以根据自己的实际情况去选择适合自己的即可。

欢迎关注我的个人公众号

posted @   北漂的雷子  阅读(291)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示