• 单元测试报告的生成HTMLTestRunner


    第三步

    1.把单元测试脚本插入断言

    2.生成测试报告

    import unittest
    from API_AUTO.UnitTest import HTMLTestRunnerNew
    #import HTMLTestRunnerNew #更新的测试报告模板
    from API_AUTO.UnitTest.Practice_UnitTest import TestMathMethod

    suite=unittest.TestSuite()#存储用例
    #way1 只执行一条测试用例,少量测试用例适用
    suite.addTest(TestMathMethod('test_add_two_zero'))
    suite.addTest(TestMathMethod('test_add_two_positive'))
    #way2 TestLoader,使用批量成千上百条测试用例的执行
    loader=unittest.TestLoader()#创建一个加载器
    #suite.addTest(loader.loadTestsFromTestCase(TestMathMethod))

    from API_AUTO.UnitTest import Practice_UnitTest
    suite.addTest(loader.loadTestsFromModule(Practice_UnitTest))

    #执行 上下文管理器
    #file=open("test.txt",'w+',encoding='UTF-8')
    # with open("test.txt",'w+',encoding='UTF-8') as file:
    # runner=unittest.TextTestRunner(stream=file,verbosity=2) #verbosity =0,1,2,2是最详细的
    # runner.run(suite)
    # # file.close()
    # print(file.closed) #判断file是否关闭
    #使用htmltestRunner
    with open("test_report.html",'wb') as file:
    runner=HTMLTestRunnerNew.HTMLTestRunner(stream=file,
    verbosity=2,
    title='My unit test_小肥羊',
    description='小肥羊的第一个单元测试报告.',
    tester="小肥羊")
    runner.run(suite)


    ------------

    第一步写的单元测试的脚本-开发写的
    class MathMethod:
    def __init__(self,a,b):
    self.a=a
    self.b=b

    def add(self):
    return self.a+self.b

    def multi(self):
    return self*self.b






    ------------
    第二步骤: 进行单元测试编写的脚本-测试写的

    #接口测试的本质:测试类的函数,类里主要封装函数和属性,测试别人写好的代码,通过数据驱动测试
    #单元测试的本质:测试函数 通过代码测试
    #单元测试的框架 unittest+API(接口),pytest+web
    #功能测试步骤-unittest可以实现如下步骤
    #1.写用例 Testcase
    #2.执行用例 2个方法 1:TestSuite 存储用例 2.TestLoader 找用例-》 加载用例 -》存储到TestSuite
    #3.对比实际结果 期望结果 判定用例是否通过 断言
    #4.出具测试报告 TestRunner

    import unittest
    from API_AUTO.UnitTest.Math_Method import MathMethod
    #编写3个测试用例如下:1+1/0+1/-1+(-2)
    #写一个测试类,对D:PycharmProjectTestAPI_AUTOUnitTestMath_Method.py这个文件里模块的类进行单元测试
    class TestMathMethod(unittest.TestCase): #继承unittest里的testcase,用来编写测试用例
    #编写测试用例
    #1.一个函数就是一个测试用例,不能传参,只有self关键字
    #2.所有用例都是test开头,按照ascii码执行顺序
    def test_add_two_positive(self): # test method names begin with 'test'
    res=MathMethod(1,1).add() #创建实例
    print("1+1的结果是: ",res)
    try:
    self.assertEqual(2,res,"计算结果出错!")
    except AssertionError as e:
    print("出错了,断言结果是{0}".format(e))
    raise e #异常处理后抛出去

    def test_add_two_zero(self): # test method names begin with 'test'
    res=MathMethod(0,0).add() #创建实例
    print("0+0的结果是: ",res)
    try:
    self.assertEqual(0,res,"计算结果出错!")
    except AssertionError as e:
    print("出错了,断言结果是{0}".format(e))
    raise e
    def test_add_two_negative(self): # test method names begin with 'test'
    res=MathMethod(-1,-2).add() #创建实例
    print("-1+(-2)的结果是: ",res)
    try:
    self.assertEqual(-33,res,"计算结果出错!")
    except AssertionError as e:
    print("出错了,断言结果是{0}".format(e))
    raise e

    class TestMultiMethod(unittest.TestCase): #继承unittest里的testcase,用来编写测试用例
    #编写测试用例
    #1.一个函数就是一个测试用例,不能传参,只有self关键字
    #2.所有用例都是test开头,按照ascii码执行顺序
    def test_multi_two_positive(self): # test method names begin with 'test'
    res=MathMethod(1,1).add() #创建实例
    print("1*1的结果是: ",res)
    try:
    self.assertEqual(1,res,"计算结果出错!")
    except AssertionError as e:
    print("出错了,断言结果是{0}".format(e))
    raise e

    def test_multi_two_zero(self): # test method names begin with 'test'
    res=MathMethod(0,0).add() #创建实例
    print("0*0的结果是: ",res)
    try:
    self.assertEqual(0,res,"计算结果出错!")
    except AssertionError as e:
    print("出错了,断言结果是{0}".format(e))
    raise e

    def test_multi_two_negative(self): # test method names begin with 'test'
    res=MathMethod(-1,-2).add() #创建实例
    print("-1*(-2)的结果是: ",res)
    try:
    self.assertEqual(22,res,"计算结果出错!")
    except AssertionError as e:
    print("出错了,断言结果是{0}".format(e))
    raise e

    if __name__ == '__main__':
    unittest.main()
  • 相关阅读:
    .NET MVC后台发送post请求
    (整理)Sublime Text 3 安装、破解、安装Package Control、汉化、添加到右键菜单、代码格式化、禁止更新
    百度api查询多个地址的经纬度的问题
    try{}里有一个 return 语句,那么紧跟在这个 try 后的 finally {}里的 code 会 不会被执行,什么时候被执行,在 return 前还是后?
    js获取某个日期所在周周一的日期
    重学C++ (1)
    基于.NET平台常用的框架整理
    指针常量和常量指针的区别
    C++四种强制类型转换关键字
    const define 定义常量的区别
  • 原文地址:https://www.cnblogs.com/JacquelineQA/p/12709116.html
Copyright © 2020-2023  润新知