python+unittest+html测试报告

demo


import unittest
import HTMLTestRunner

suite = unittest.TestSuite()
class Count():
    def __init__(self,a,b):
        self.a = a
        self.b = b
    def add(self):
        sum = self.a + self.b
        return sum

class Grade():
    def __init__(self,a,b):
        self.a = a
        self.b = b
    def add(self):
        sum = self.a + self.b
        return sum  

  
class TestCount(unittest.TestCase):
    
    
    @classmethod  
    def setUpClass(self) -> None:
        print("test start")
    

    def test_addCount(self):
        '''测试相加'''
        j= Count(2,3)
        self.assertEqual(j.add(),5)
    def test_addCount1(self):
        '''测试相加'''
        j= Count(2,3)
        self.assertEqual(j.add(),6)
    @classmethod  
    def tearDownClass(self) -> None:
         print("test end")


class TestGrade(unittest.TestCase):
    '''试一下'''
    @classmethod  
    def setUpClass(self) -> None:
        print("test start")
    
    def test_addGrade(self):
        '''测试相加'''
        j= Grade(2,3)
        self.assertEqual(j.add(),5)
    @classmethod  
    def tearDownClass(self) -> None:
         print("test end")

def test():
    suite.addTest(TestCount("test_addCount"))
    suite.addTest(TestCount("test_addCount1"))
    suite.addTest(TestGrade("test_addGrade"))


if __name__ == "__main__":

    # unittest.main(verbosity=2)
    
    test()
    # runner = unittest.TextTestRunner(verbosity=2).run(suite)

    test_report_path = "index.html"
    st = open(test_report_path, 'wb')
    HTMLTestRunner.HTMLTestRunner(stream=st, title='测试报告',description='用例执行情况:',verbosity=2).run(suite)

报告:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 python+unittest+HTMLTestRunner 配置 HTML 测试报告的步骤如下: 1. 安装 HTMLTestRunner 模块 可以使用以下命令安装 HTMLTestRunner 模块: ``` pip install HTMLTestRunner ``` 2. 编写测试用例 使用 unittest 编写测试用例,例如: ```python import unittest class TestStringMethods(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2) if __name__ == '__main__': unittest.main() ``` 3. 编写 HTML 测试报告生成代码 编写 HTML 测试报告生成代码,例如: ```python import unittest import HTMLTestRunner class TestStringMethods(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2) if __name__ == '__main__': suite = unittest.TestSuite() suite.addTest(TestStringMethods('test_upper')) suite.addTest(TestStringMethods('test_isupper')) suite.addTest(TestStringMethods('test_split')) filename = 'test_report.html' fp = open(filename, 'wb') runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title='Test Report', description='This is a report of the test.' ) runner.run(suite) fp.close() ``` 4. 运行测试用例并生成 HTML 测试报告 运行测试用例并生成 HTML 测试报告,例如: ``` python test_report.py ``` 在当前目录下会生成一个名为 `test_report.html` 的 HTML 测试报告文件。打开该文件,即可查看测试结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值