zoukankan      html  css  js  c++  java
  • python学习笔记(28)-unittest单元测试-执行用例

    执行用例

    #写一个测试类
    import unittest
    import HTMLTestRunnerNew   #写好的模块可以直接调用
    #import HTMLTest  #测试报告模板
    from class_004_unittest.class_003 import TestMathMethod
    
    suite=unittest.TestSuite() #存储用例,创建一个容器,存储用例
    #测试用例们是一个类,如何加载用例,首先创建这个测试用例类的实例
    #这个类的实例们是一个个的单独的用例
    #在模块外的地方用到测试用例类的函数,就要需要创建这个类的实例。
    
    #方法一(添加用例):
    #创建实例的时候要不要传参,要看类(继承类)里面有没有初始化函数,这里的继承类是unittest.TestCase ,需要看这个类有没有初始化函数
    #添加用例(该方法太繁琐,需要挨条用例添加)
    # suite.addTest(TestMathMethod('test_add_two_positive'))
    # suite.addTest(TestMathMethod('test_add_two_zero'))
    # suite.addTest(TestMathMethod('test_add_two_negtive'))
    
    #方法二:(添加用例)  TestLoader
    loader=unittest.TestLoader()  #创建一个加载器
    #从测试类里面去找
    suite.addTest(loader.loadTestsFromTestCase(TestMathMethod))
    #去测试模块里去找
    #suite.addTest(loader.loadTestsFromModule(class_003))
    #这种方法就要导入具体的模块,而不是类,否则会找不到   from class_004 import class_003
    
    #执行用例  上下文管理器
    #file=open("test.txt",'w',encoding='utf-8') #指定输入文件,乱码加encoding
    # with open("test.txt",'w',encoding='utf-8') as file:
    #     runner=unittest.TextTestRunner(stream=file,descriptions=True,verbosity=0)
    #     #stream=None(指定输入文件),descriptions=True(),verbosity=1(0,1,2) 0没有执行状态,1只有执行状态,2最详细,显示每条用例以及执行状态
    #     runner.run(suite)
    
    #测试报告
    with open("test_report.html",'wb') as file:
        runner=HTMLTestRunnerNew.HTMLTestRunner(stream=file,
                                                verbosity=2,
                                                title="武宗梅的单元测试报告",
                                                description="wuzongmei的第一次单元测试报告",
                                                tester='武宗梅')
        runner.run(suite)
    #

     

  • 相关阅读:
    android 选择图片或拍照时旋转了90度问题
    拍照选择图片(Activity底部弹出)
    Dialog 自定义使用1
    Dialog 基本使用
    秒杀主流应用的二维码扫描
    gen already exists but is not a source folder. Convert to a source folder or rename it.
    gen already exists but is not a source folder. Convert to a source folder or rename it.
    Unable to execute dex: Multiple dex files define
    xxxx is not translated in zh-rCN, zh-rTW
    Android Application 对象介绍
  • 原文地址:https://www.cnblogs.com/wuzm/p/11977673.html
Copyright © 2011-2022 走看看