当前位置:网站首页>Assertion of unittest framework

Assertion of unittest framework

2022-07-04 14:09:00 Field test record

1、 Meet first Unittest Assertion

1) Assertion is to judge whether the actual result is consistent with the expected result ;

2) although Python There are also self-contained assertions , But it's better to use Unittest Own assertion , Work well ;

3)unittest The assertion is that TestCase Implemented in , Need to inherit TestCase Before using assertions ;

2、 Common assertion methods

assertEqual(a,b): Check a and b Whether it is equal or not

assertTrue(a): Check a Is it for true

assertIs(a,b): Check a and b Is it exactly the same (assert is)

assertIsNone(a): Check a Is it right? None

assertIn(a,b): Check a Is it right? b Subset

assertIsInstance(a,b): Check a、b Whether the two object instance types are the same

3、 Example

unittest_assert_demo.py
#  Assertion 

import unittest

class AssertDemo(unittest.TestCase):
        def setUp(self):
            self.l1, self.l2 = [1, 2], [1, 2]
            self.a, self.b = 1, 1

        def test01_assertEqual(self):
            self.assertEqual(self.a, self.b)  # a and b equal 

        def test02_assertIs(self):
            self.assertIs(self.l1, self.l2)  # l1 and l2 inequality 
            self.assertIs(self.a, self.b)  # a and b identical 

        def test03_assertTrue(self):
            self.assertTrue(self.a)  # 1  yes true
            self.assertTrue(0)  # 0  yes false

        def test04_assertIsNone(self):
            self.assertIsNone(self.b)  # 1  No  None
            self.assertIsNone(None)  # None  yes  None

        def test05_assertIn(self):
            self.assertIn(self.l1, self.l2)  # l1  No l2 Subset 

        def test06_assertIsInstance(self):
            self.assertIsInstance(self.a, int)  # a yes int integer 
            self.assertIsInstance(self.l1, list)  # l1 yes list List 

if __name__ == '__main__':
    unittest.main()

原网站

版权声明
本文为[Field test record]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041107208701.html