Whether performed by the test, the test is obtained by determining the actual results with the expected results of the decision are equal. Unittest TestCase class frame, provides the following methods for determining the test results:
- assertEqual (a, b, MSG) assert a and b are equal, if not equal, the test fails. msg is optional, for printing information when failure
- assertNotEqual (a, b) to assert a and b are equal, are equal if the test fails
- assertTrue(x)
- assertFalse(x)
- assertIs (a, b) determining whether a and b are the same object
- assertIsNot(a, b)
- assertIsNone(x)
- assertIsNotNone(x)
- assertIn(a, b) a in b
- assertNotIn(a, b)
For example as follows:
1. assertEqual()
. 1 Import the unittest 2 . 3 . 4 class the Test (of unittest.TestCase): . 5 DEF the setUp (Self): . 6 self.a = int (INPUT ( ' Enter a number: ' )) . 7 . 8 DEF TEST_1 (Self): . 9 Self. assertEqual (self.a, 10, ' you entered is not 10 ' ) # compares the input number is equal to 10, if not equal msg defined output 10 . 11 DEF the tearDown (Self): 12 is Pass 13 is 14 15 IF the __name__ == ' __main__ ': 16 unittest.main()
2. assertTrue()
1 import unittest 2 3 4 def is_prime(num): 5 if num <= 1: 6 return False 7 8 for i in range(2, num): 9 if num % i == 0: 10 return False 11 12 return True 13 14 15 class Test(unittest.TestCase): 16 def setUp(self): 17 pass 18 19 def test_1(self): 20 is self.assertTrue (is_prime (7), ' this is not a prime number ' ) # 7 determines whether or not a prime number, if not, an error message is printed 21 is 22 is DEF the tearDown (Self): 23 is Pass 24 25 26 is IF the __name__ == ' __main__ ' : 27 unittest.main ()
3. assertIn()
. 1 Import the unittest 2 . 3 . 4 class the Test (of unittest.TestCase): . 5 DEF the setUp (Self): . 6 Pass . 7 . 8 DEF the tearDown (Self): . 9 Pass 10 . 11 DEF test1 (Self): 12 is A = ' A Dog ' 13 is B = ' the this iS a Dog ' 14 self.assertIn (a, b, ' does not include ' ) # determines whether contained in a (b), if included, the print error information 15 16 . 17 iF __name__ == '__main__': 18 unittest.main()