Detailed explanation of unittest in Django and its application
- 2021-12-12 09:08:48
- OfStack
unittest is a unit test framework of python
About Assertion
It is used to judge a certain result and a predicted result. If the result is correct, there is no return effect, and if the result is wrong; AssertionError exception is thrown, and msg content is returned if it is followed by msg
assert 2 == 2
assert 2 == 1 * 2
assert 2 + 0 == 1 * 2
# ----------------- The above has no return effect
assert 1 > 3, 'wrong'
'''
Traceback (most recent call last):
File "D:/project_of_3 Period course /day22/ Class .py", line 259, in <module>
assert 1 > 3, 'wrong'
AssertionError: wrong
'''
unittest Module Attribute Description
Properties of unittest
unittest. main (): It makes it easy to turn a unit test module into a test script that can be run directly. The main () method uses the TestLoader class to search for all test methods contained in the module that begin with the name "test" and automatically execute them. The default order for executing methods is to load test cases in the order of ASCII codes, numeric and alphabetical order: 0-9, A-Z, a-z. Therefore, test case methods that begin with A will be executed first, and those that begin with a will be executed later.
unittest. TestSuite (): The TestSuite () class of the unittest framework is used to create test suites.
unittest. TextTextRunner (): The TextTextRunner () class of the unittest framework, which runs the test cases assembled by suite through the run () method below the class, and the input parameters are suite test suite.
Properties of the TestCase class
setUp (): Method is used to initialize the test case before execution. If you need to access the database in your test case, you can establish a database connection and initialize it in setUp. If the test case needs to log in to web, you can instantiate the browser first.
tearDown (): The method is used to clean up the aftermath of test case execution. Such as closing a database connection. Close the browser.
assert* (): Some assertion methods, in the process of executing test cases, whether the final use case passes or not is determined by judging whether the actual results obtained by the test are equal to the expected results.
assertEqual (a, b, [msg = 'Information printed when test fails']): Asserts whether a and b are equal, and if they are equal, the test case passes.
assertNotEqual (a, b, [msg= 'Information printed when test fails']): Asserts whether a and b are equal or not, and if not, the test case passes.
assertTrue (x, [msg= 'Information printed when test fails']): Asserts whether x is True, and if it is True, the test case passes.
assertFalse (x, [msg= 'Information printed when test fails']): Asserts whether x is False, and if False is False, the test case passes.
assertIs (a, b, [msg = 'Information printed when test fails']): Asserts whether a is b, and if so the test case passes.
assertNotIs (a, b, [msg= 'Message printed when test fails']): Asserts whether a is b, and if not, the test case passes.
assertIsNone (x, [msg= 'Information printed when test fails']): Asserts whether x is None, and if None is None, the test case passes.
assertIsNotNone (x, [msg= 'Information printed when test fails']): Asserts whether x is None, and if it is not None, the test case passes.
assertIn (a, b, [msg= 'Information printed when test fails']): Asserts whether a is in b, and in b the test case passes.
assertNotIn (a, b, [msg= 'Information printed when test fails']): Asserts whether a is in b, and if it is not in b, the test case passes.
assertIsInstance (a, b, [msg= 'Information printed when test fails']): Assert that a is an instance of b, and yes, the test case passes.
assertNotIsInstance (a, b, [msg= 'Message printed when test fails']): Assert that a is an instance of b, and if not, the test case passes.
Properties of TextTextRunner
run (): Is the test case from which the test suite is run, and the reference is the suite test suite.
unittest.TextTestRunner(verbosity=2).run(suite)
The unittest framework uses
Mode 1: unittest. main () to start the unit test module
# coding=utf-8
import unittest
# Mode 1 : unittest.main() To start the unit test module
class MyTestCase(unittest.TestCase):
def setUp(self):
print(' Test environment ')
def test(self):
print(' Test case ')
self.assertEquals(4, 2 * 2)
self.assertEqual(1, 3, 'something was wrong')
def tearDown(self):
print(' Environmental destruction ')
if __name__ == '__main__':
unittest.main()
Mode 2: Add to the testsuite collection and then load all the objects under test
# coding=utf-8<br>import unittest<br><br>class TestCase(unittest.TestCase):
def test1(self):
print('one')
def test2(self):
print('two')
class TestCase1(unittest.TestCase):
def test1(self):
print('three')
def test2(self):
print('four')
if __name__ == '__main__':
un1 = unittest.TestLoader().loadTestsFromTestCase(TestCase)
un2 = unittest.TestLoader().loadTestsFromTestCase(TestCase1)
suite = unittest.TestSuite([un1, un2])
unittest.TextTestRunner(verbosity=2).run(suite)