断言内容是自动化脚本的重要内容,正确设置断言以后才能帮助我们判断测试用例执行结果。
断言方法
- 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)
实例
#coding=utf-8
import unittest
from caculator import Math
class TestMath(unittest.TestCase):
def setUp(self):
print("Start test") def test_add(self):
j=Math(5,10)
self.assertEqual(j.add(),15)
#用例失败场景
# self.assertEqual(j.add(),12)
def test_add1(self):
j=Math(5,10)
self.assertNotEqual(j.add(),10) def test_add2(self):
j=Math(5,10)
self.assertTrue(j.add()>10) def test_add3(self):
self.assertIs("博客", '博客')
# self.assertIs("博客",'abc') def test_add4(self):
self.assertIn("博客", "hello,博客")
self.assertIn("", "hello,博客") def tearDown(self):
print("test end") if __name__=='__main__':
#构造测试集
suite=unittest.TestSuite()
suite.addTest(TestMath("test_add2"))
#执行测试
runner=unittest.TextTestRunner()
runner.run(suite)