单元测试框架功能:1、提供用例组织和执行,2、提供丰富的断言方法;3、提供丰富的日志。
1、创建被测试文件:calculator.py
class Calculator: def __init__(self,a,b): self.a = a self.b = b def add(self): return self.a+self.b def sub(self): return self.a-self.b def mul(self): return self.a*self.b def div(self): return self.a / self.b
2、创建 test_calculator.py
import unittest from calculator import Calculator class TestCalculator(unittest.TestCase): def test_add(self): c = Calculator(3,5) result = c.add() self.assertEqual(result,8) def test_sub(self): c = Calculator(7,2) result = c.sub() self.assertEqual(result,4) def test_mul(self): c = Calculator(3,3) result = c.mul() self.assertEqual(result,9) def test_div(self): c = Calculator(6,2) result = c.div() self.assertEqual(result,4) if __name__=="__main__": unittest.main()
执行结果:
.F.F ====================================================================== FAIL: test_div (__main__.TestCalculator) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:/Users/Administrator/AppData/Local/Programs/Python/Python38-32/practise.py", line 24, in test_div self.assertEqual(result,4) AssertionError: 3.0 != 4 ====================================================================== FAIL: test_sub (__main__.TestCalculator) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:/Users/Administrator/AppData/Local/Programs/Python/Python38-32/practise.py", line 14, in test_sub self.assertEqual(result,4) AssertionError: 5 != 4 ---------------------------------------------------------------------- Ran 4 tests in 0.029s FAILED (failures=2)
unittest测试模块遵循的规则:(1)创建一个测试类,上列中为 TestCalculator类,必须要继承unittest模块的TestCase类。(2)创建一个测试方法,该方法必须以“test”开头。
测试结果解读:“ . ”表示一条通过的测试用例;“F”表示一条失败的测试用例;“E”表示一条运行错误的测试用例,“s”表示一条运行跳过的测试用例。
重要概念:
(1)、Test Case:最小测试单元,检查特定输入集合的特定返回值。
(2)、Test Suite:测试套件,组装一组要运行的测试。
(3)、Test Runner: 协调测试的执行并向用户提供结果。
(4)、Test Fixture: 代表执行一个或多个测试所需的环境准备,以及关联的清理动作。
语法:setUp():测试前置动作;tearDOWN():测试后置动作;addTest():添加测试用例;run():运行测试套件。
import unittest from calculator import Calculator class TestCalculator(unittest.TestCase): #测试前置动作 def setUp(self): print("test start:") #测试后置动作 def tearDown(self): print("test end") def test_add(self): c = Calculator(3,5) result = c.add() self.assertEqual(result,8) def test_sub(self): c = Calculator(7,2) result = c.sub() self.assertEqual(result,4) if __name__=="__main__": #创建测试套件 suit = unittest.TestSuite() suit.addTest(TestCalculator("test_add")) suit.addTest(TestCalculator("test_sub")) #创建测试运行器 runner = unittest.TextTestRunner() runner.run(suit)
执行结果:
test start: test end .test start: test end F ====================================================================== FAIL: test_sub (__main__.TestCalculator) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:/Users/Administrator/AppData/Local/Programs/Python/Python38-32/practise.py", line 22, in test_sub self.assertEqual(result,4) AssertionError: 5 != 4 ---------------------------------------------------------------------- Ran 2 tests in 0.046s FAILED (failures=1)
断言方法:assertEqual(a,b)、assertNotEqual(a,b);assertTrue(x)、assertFalse(x) ;assertIn(a,b)、assertNotIn(a,b);
import unittest class TestAssert(unittest.TestCase): def test_equal(self): self.assertEqual(2+2,5) def test_in(self): self.assertNotIn("ho","hello") def test_true(self): self.assertTrue(1+2>7) if __name__=="__main__": unittest.main()
执行结果:
F.F ====================================================================== FAIL: test_equal (__main__.TestAssert) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:/Users/Administrator/AppData/Local/Programs/Python/Python38-32/practise.py", line 6, in test_equal self.assertEqual(2+2,5) AssertionError: 4 != 5 ====================================================================== FAIL: test_true (__main__.TestAssert) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:/Users/Administrator/AppData/Local/Programs/Python/Python38-32/practise.py", line 13, in test_true self.assertTrue(1+2>7) AssertionError: False is not true ---------------------------------------------------------------------- Ran 3 tests in 0.013s FAILED (failures=2)
测试用例组织与 discover方法:
1、一个功能对应多个测试用例;2、一个测试类对应一个功能。
import unittest from calculator import Calculator class TestAdd(unittest.TestCase): """add()方法""" def test_add_int(self): """整除相加""" c = Calculator(3,5) result = c.add() self.assertEqual(result,8) def test_add_str(self): """字符串相加""" c = Calculator("10","8") result = c.add() self.assertEqual(result,8) def test_add_decimals(self): """小数相加""" c = Calculator(1.2,2.4) result = c.add() self.assertEqual(result,4) class TestSub(unittest.TestCase): pass if __name__=="__main__": unittest.main()