zoukankan      html  css  js  c++  java
  • 对自己写的代码进行单元测试_UnitTest

    1.写一个简单的代码,以便后续对此进行单元测试

    # @File    : Math_Method.py
    # @Software: PyCharm
    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
    2。使用unittest框架进行单元测试,执行所有测试用例
    # -*- coding: utf-8 -*-
    # @Time : 2020/4/11 22:35
    # @Author : Jacqueline
    # @File : Practice_UnitTest.py
    # @Software: PyCharm

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

    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)

    def test_add_two_zero(self): # test method names begin with 'test'
    res=MathMethod(0,0).add() #创建实例
    print("0+0的结果是: ",res)

    def test_add_two_negative(self): # test method names begin with 'test'
    res=MathMethod(-1,-2).add() #创建实例
    print("-1+(-2)的结果是: ",res)

    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)

    def test_multi_two_zero(self): # test method names begin with 'test'
    res=MathMethod(0,0).add() #创建实例
    print("0*0的结果是: ",res)

    def test_multi_two_negative(self): # test method names begin with 'test'
    res=MathMethod(-1,-2).add() #创建实例
    print("-1*(-2)的结果是: ",res)

    if __name__ == '__main__':
    unittest.main()

    3.想执行部分测试用例,选择性执行需要的测试用例,而不是执行全部测试用例
    # -*- coding: utf-8 -*-
    # @Time : 2020/4/11 23:22
    # @Author : Jacqueline
    # @File : Practice_SkipTestCase.py
    # @Software: PyCharm

    import unittest
    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))

    #执行
    runner=unittest.TextTestRunner()
    runner.run(suite)
  • 相关阅读:
    The Joy of Clojure – Laziness(6.3)
    Python Decorator Closure
    Programming clojure – Recursion and Lazyseq
    Programming Clojure Unifying Data with Sequences
    SharePoint Workflow的code运行在哪个进程? w3wp.exe 还是OWSTimer.exe?
    利用PsExec提升命令行的安全级别, 绕过组策略执行命令
    WinDBG脚本入门
    记录一个SPS2010中RSS Web Part报错的问题
    User的Delegation选项卡在Active Directory Users and Computers找不到?
    修改SPS2010的Search Core Results webpart, 令其显示文档被索引了的所有属性
  • 原文地址:https://www.cnblogs.com/JacquelineQA/p/12683230.html
Copyright © 2011-2022 走看看