Explain unittest and its application in Django in detail

Time:2022-4-5
catalogue
  • About assertions
  • Unittest module attribute description
    • Properties of unittest
    • Properties of testcase class
    • Properties of textrunner
  • Unittest framework usage

Unittest is a unit test framework of Python

About assertions

It is used to judge a certain result and prediction result. If the result is correct, there is no return effect, and if the result is wrong; An assertion error exception will be thrown. If MSG is followed, the MSG content will be returned

?
1
2
3
4
5
6
7
8
9
10
11
12
assert 2 == 2
assert 2 == 1 * 2
assert 2 + 0 == 1 * 2
#----------------- there is no return effect above
 
assert 1 > 3, 'wrong'
'''
Traceback (most recent call last):
  File "D: / project_of_3 courses / day22 / class. Py", line 259, in < module >
    assert 1 > 3, 'wrong'
AssertionError: wrong
'''

Unittest module attribute description

Properties of unittest

unittest. Main (): it is convenient to turn a unit test module into a test script that can be run directly. The main () method uses the testloader class to search all the test methods that start with “test” in the module and automatically execute them. The default order of execution method is: load test cases according to the order of ASCII code, and the order of numbers and letters is: 0-9, A-Z, A-Z. Therefore, the test case methods starting with a will be implemented first and then after the meeting.

unittest. TestSuite (): the TestSuite () class of the unittest framework is used to create test suites.

unittest. Texttextrunner (): the texttextrunner () class of unittest framework. Run the test cases assembled by suite through the run () method under this class. The input parameters are suite test suite.

Properties of testcase class

Setup (): this method is used to initialize the test case before execution. If the database needs to be accessed in the test case, the database connection can be established and initialized in setup. If the test case needs to log in to the web, you can instantiate the browser first.

Teardown(): this method is used to deal with the aftermath after the test case is executed. Such as closing the database connection. Close the browser.

Assert * (): for some assertion methods, in the process of executing test cases, whether the final use cases pass the execution is determined by judging whether the actual results obtained from the test are equal to the expected results.

Assert equal (a, B, [MSG = ‘information printed when test fails’): assert whether a and B are equal. If they are equal, the test case passes.

Assertnotequal (a, B, [MSG = ‘information printed when test fails’): assert whether a and B are equal. If they are not equal, the test case passes.

Assert true (x, [MSG = ‘information printed when test fails’]): assert whether x is true. If it is true, the test case passes.

Assertfalse (x, [MSG = ‘information printed when the test fails’): assert whether x is false. If it is false, the test case passes.

Assertis (a, B, [MSG = ‘information printed when test fails’): assert whether a is B, and if yes, the test case passes.

Assert notis (a, B, [MSG = ‘information printed when test fails’): assert whether a is B, otherwise the test case passes.

Assertisnone (x, [MSG = ‘information printed when test fails’): assert whether x is none. If it is none, the test case passes.

Assertisnotnone (x, [MSG = ‘information printed when test fails’]): assert whether x is none, otherwise the test case passes.

Assert in (a, B, [MSG = ‘information printed when test fails’]): assert whether a is in B, and if it is in B, the test case passes.

Assertnotin (a, B, [MSG = ‘information printed when test fails’): assert whether a is in B, otherwise the test case passes.

Assertisinstance (a, B, [MSG = ‘information printed when test fails’): assert that a is an instance of B, and if yes, the test case passes.

Assertnotisinstance (a, B, [MSG = ‘information printed when test fails’): assert that a is an instance of B, otherwise the test case passes.

Properties of textrunner

Run (): it is the test case to run the test suite, and the input parameter is suite test suite.

?
1
unittest.TextTestRunner(verbosity=2).run(suite)

Unittest framework usage

Method 1: unittest Main() to start the unit test module

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# coding=utf-8
import unittest
#Method 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('environment destruction')
if __name__ == '__main__':
    unittest.main()

Method 2: add it to the TestSuite set, and then load all the tested objects

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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)

This is the end of this article about the unittest and application in Django. For more information about the application of Django unittest, please search the previous articles of developepper or continue to browse the relevant articles below. I hope you will support developepper in the future!