unittest单元测试框架总结

(一)unittest简介

    unittest单元测试框架不仅可以适用于单元测试,还可以适用WEB自动化测试用例的开发与执行,该测试框架可组织执行测试用例,并且提供了丰富的断言方法,判断测试用例是否通过,最终生成测试结果。

参考:https://www.cnblogs.com/yufeihlf/p/5707929.html#test0

(二)代码演示

unittest单元测试框架总结_第1张图片

1.Calculator.py

class Calculator:
    def divide(self,x,y):
        return x / y

2.person.txt

user1|pwd1
user2|pwd2
user3|pwd3
user4|pwd4
user5|pwd5

3.TestCalculator1.py

import unittest
from unit.Calculator import Calculator
class TestCalculator(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        print("===先执行===\n")

    @classmethod
    def tearDownClass(cls) -> None:
        print("===后执行===")
    def testDivide01(self):
        cal = Calculator()
        result = cal.divide(10, 2)
        self.assertEqual(result, 5)
        print("testDivide01.......")

    def testDivide02(self):
        cal = Calculator()
        result = cal.divide(20, 0.5)
        self.assertEqual(result, 40)
        print("testDivide02.......")
if __name__ == '__main__':
    unittest.main()

4.TestCalculator2.py

import unittest

from unit.Calculator import Calculator
class TestCalculator(unittest.TestCase):
    def setUp(self) -> None:
        print("....start....")
    def tearDown(self) -> None:
        print("....end....")
    def testDivide01(self):
        cal = Calculator()
        result = cal.divide(10, 2)
        self.assertEqual(result, 5)
        print("testDivide01.......")

    def testDivide02(self):
        cal = Calculator()
        result = cal.divide(20, 0.5)
        self.assertEqual(result, 40)
        print("testDivide02.......")

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

5.TestCalculator3.py

import unittest

from unit.Calculator import Calculator
class TestCalculator(unittest.TestCase):
    def setUp(self):
        print("....start....")
    def tearDown(self):
        print("....end....")
    def testDivide01(self):
        cal = Calculator()
        result = cal.divide(10, 2)
        self.assertEqual(result, 5)
        print("testDivide01.......")

    def testDivide02(self):
        cal = Calculator()
        result = cal.divide(20, 0.5)
        self.assertEqual(result, 40)
        print("testDivide02.......")

    # def testDivide03(self):
    #     cal = Calculator()
    #     result = cal.divide(20, 0.5)
    #     self.assertEqual(result, 10)
if __name__ == '__main__':
    unittest.main()

6.TestCalculator4.py

import unittest

from unit.Calculator import Calculator
class TestCalculator(unittest.TestCase):
    def setUp(self):
        print("start....")
    def tearDown(self):
        print("end....")
    def testDivide01(self):
        cal = Calculator()
        result = cal.divide(10, 2)
        self.assertEqual(result, 5)
        print("testDivide01.......")

    def testDivide02(self):
        cal = Calculator()
        result = cal.divide(20, 0.5)
        self.assertEqual(result, 40)
        print("testDivide02.......")

    #断言的使用
    def testDivide03(self):
        cal = Calculator()
        result = cal.divide(20, 0.5)
        self.assertEqual(result, 10)
if __name__ == '__main__':
    unittest.main()

7.TestCalculator5.py

import unittest

from ddt import data, ddt, unpack, file_data


def testReadFile():
    params = []
    file = open('person.txt', encoding="utf8")
    for line in file.readlines():
        params.append(line.split("|"))
    # print(params)
    return params

@ddt
class TestCalculator(unittest.TestCase):
    @data(1,2,3,4,5,6)
    def testDivide01(self,arg):
       print("打印结果= "+str(arg))

    @data((1, 2), (3, 4))
    @unpack
    def test_tuple(self, value1, value2):
        print(value1, value2)
        self.assertEqual(value2, value1 + 1)

    @data([1, 2], [2, 3])
    @unpack
    def test_list(self, value1, value2):
        print(value1, value2)
        self.assertEqual(value2, value1 + 1)

    @data({'value1': 1, 'value2': 2}, {'value1': 1, 'value2': 2})
    @unpack
    def test_dict(self, value1, value2):
        print(value1, value2)
        self.assertEqual(value2, value1 + 1)


    @data(*testReadFile())
    @unpack
    def test_file(self, value1, value2):
        print(value1, value2)

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

8.TestCalculator6.py

#coding :utf-8
import unittest

from unit.Calculator import Calculator
# https://github.com/TesterlifeRaymond/BeautifulReport
# 测试报告--HTMLTestRunner、BeautifulReport可视化报告
class TestCalculator(unittest.TestCase):
    def setUp(self) -> None:
        print("start....")
    def tearDown(self) -> None:
        print("end....")
    def testDivide01(self):
        cal = Calculator()
        result = cal.divide(10, 2)
        self.assertEqual(result, 5)
        print("testDivide01.......")

    @unittest.skip("屏蔽这个测试用例")
    def testDivide02(self):
        cal = Calculator()
        result = cal.divide(20, 0.5)
        self.assertEqual(result, 40)
        print("testDivide02.......")

    @unittest.skipIf(1 < 2,"满足条件就屏蔽测试用例")
    def testDivide03(self):
        cal = Calculator()
        result = cal.divide(20, 0.5)
        self.assertEqual(result, 40)
        print("testDivide03.......")

    @unittest.skipUnless(1 > 2, "不满足条件就屏蔽测试用例")
    def testDivide04(self):
        cal = Calculator()
        result = cal.divide(20, 0.5)
        self.assertEqual(result, 40)
        print("testDivide04.......")

    @unittest.expectedFailure
    def testDivide05(self):
        self.assertEqual(1,2,msg="不相等")
        print("testDivide05.......")

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

9.TestCalculator7.py

#coding :utf-8
import unittest

from unit.Calculator import Calculator
class TestCalculator(unittest.TestCase):

    def testDivide01(self):
        print("testDivide01.......")

    def testDivide02(self):
        print("testDivide02.......")

    def testDivide03(self):
        print("testDivide03.......")

    def testDivide04(self):
        print("testDivide04.......")

    def testDivide05(self):
        print("testDivide05.......")

    #控制测试用例执行顺序
    def testDivide06(self):
        suite = unittest.TestSuite()
        suite.addTest(TestCalculator('testDivide05'))
        suite.addTest(TestCalculator('testDivide04'))
        suite.addTest(TestCalculator('testDivide03'))
        suite.addTest(TestCalculator('testDivide02'))
        suite.addTest(TestCalculator('testDivide01'))
        runner = unittest.TextTestRunner()
        runner.run(suite)

    #批量添加测试用例
    def testDivide07(self):
        case = [TestCalculator('testDivide05'),TestCalculator('testDivide04')]
        suite = unittest.TestSuite()
        suite.addTests(case)
        runner = unittest.TextTestRunner()
        runner.run(suite)
if __name__ == '__main__':

     unittest.main()

 

你可能感兴趣的:(测试篇)