复杂运行
导入unittest模块中的TestCase方法
写好一个方法
写好一个类来运行测试用例 self.assertEqual(预期结果,实际结果,实际和预期对不上时默认提示可省)
实例化类
运行
eg:
1 from unittest import TestCase 2 3 def calc(a,b): 4 c = a//b 5 return c 6 7 class MyTest(TestCase): 8 def calc1(self): 9 #正常的测试用例 10 res = calc(4,2) 11 self.assertEqual(2,res,'预期结果和实际结果不符合 预期结果是 2 实际结果是%s'%res)#断言 12 13 def calc2(self): 14 # 不正常的测试用例 15 res = calc(5, 1) 16 self.assertEqual(1, res) 17 18 m = MyTest() 19 m.calc1() 20 m.calc2()
用unittest.main()全部运行,用例必须是以test开头
1 import unittest 2 from unittest import TestCase 3 4 def calc(a,b): 5 c = a//b 6 return c 7 8 class MyTest(TestCase): 9 def test_calc1(self): 10 #正常的测试用例 11 res = calc(4,2) 12 self.assertEqual(2,res,'预期结果和实际结果不符合 预期结果是 2 实际结果是%s'%res)#断言 13 14 def test_calc2(self): 15 # 不正常的测试用例 16 res = calc(5, 1) 17 self.assertEqual(1, res) 18 unittest.main()
运行结果:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/ihealth-qa/PycharmProjects/untitled/test.py
.F
======================================================================
FAIL: test_calc2 (__main__.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/ihealth-qa/PycharmProjects/untitled/test.py", line 17, in test_calc2
self.assertEqual(1, res)
AssertionError: 1 != 5
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (failures=1)
Process finished with exit code 1