unittest 常用断言

1 assertEqual(a,b)  判断a==b

2 assertNotEqual(a,b) 判断a !=b

3 assertTrue(x)  bool (x) is True

4 assertFalse(x)  bool(x) is False

5 assertIs(a, b)  a is b

6 assertIsNot(a,b) a is not b

7 assertIsNone(x) x is None

8 assertIsNotNone(x) x is not None

9 assertIn(a,b) a in b

10 assertIsInstance(a,b) isinstance(a,b)

11 sssertNotIsInstance(a,b) not isinstance(a,b)

 

#!/usr/bin/python3  

from calculator import Math
import unittest

class TestMath(unittest.TestCase):
  def setUp(self):
    print('test start')
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(),12)
def test_assertTrue(self):
    j = Math(5,10)
    self.assertTrue(j.add()>10)
def test_assertIn(self):
    self.assertIn('888','hello 51zxw')
def test_assertIs(self):
    self.assertIs('51zxw','51zxw')
def tearDown(self):
    print('test end')


if __name__ == '__main__':
  suite = unittest.TestSuite()
  suite.addTest(TestMath('test_assertIs'))



  runner = unittest.TextTestRunner()
  runner.run(suite)
posted @ 2018-02-14 17:47  holly&j  阅读(769)  评论(0编辑  收藏  举报