python单元测试框架-unittest(二)之断言

断言内容是自动化脚本的重要内容,正确设置断言以后才能帮助我们判断测试用例执行结果。

断言方法

  • assertEqual(a, b) 判断a==b
  • assertNotEqual(a, b) 判断a!=b
  • assertTrue(x) bool(x) is True
  • assertFalse(x) bool(x) is False
  • assertIs(a, b) a is b
  • assertIsNot(a, b) a is not b
  • assertIsNone(x) x is None
  • assertIsNotNone(x) x is not None
  • assertIn(a, b) a in b
  • assertNotIn(a, b) a not in b
  • assertIsInstance(a, b) isinstance(a, b)
  • assertNotIsInstance(a, b) not isinstance(a, b)

实例

 

 1 #coding=utf-8
 2 import unittest
 3 from caculator import Math
 4 class TestMath(unittest.TestCase):
 5     def setUp(self):
 6         print("Start test")
 7 
 8     def test_add(self):
 9         j=Math(5,10)
10         self.assertEqual(j.add(),15)
11         #用例失败场景
12         # self.assertEqual(j.add(),12)
13     def test_add1(self):
14         j=Math(5,10)
15         self.assertNotEqual(j.add(),10)
16 
17     def test_add2(self):
18         j=Math(5,10)
19         self.assertTrue(j.add()>10)
20 
21     def test_add3(self):
22         self.assertIs("博客", '博客')
23         # self.assertIs("博客",'abc')
24 
25     def test_add4(self):
26         self.assertIn("博客", "hello,博客")
27         self.assertIn("888", "hello,博客")
28 
29     def tearDown(self):
30         print("test end")
31 
32 if __name__=='__main__':
33     #构造测试集
34     suite=unittest.TestSuite()
35     suite.addTest(TestMath("test_add2"))
36     #执行测试
37     runner=unittest.TextTestRunner()
38     runner.run(suite)

 

posted on 2018-07-20 16:07  深海溺心  阅读(183)  评论(0编辑  收藏  举报

导航