unittest module in Python

Unittest is a built-in module of Python. In testing, you only need to import the unittest module to use it. Unittest contains four major components, namely test firmware, test cases, test suites, and test executors.

1. Test the firmware
  1. The test firmware is divided into a pre-execution part and a post-execution part. The pre-execution part is called setUp(), which is generally used to open the browser, connect to the database, etc. before the actual test starts; the post-execution part is called tearDown(), which is mainly used Close the browser, disconnect the database link, etc.
  2. When setUp() and teardowm() are executed, each use case needs to be opened and closed once. That is to say, if there are 3 use cases in the test script, then the browser needs to be opened three times and closed three times. In order to solve this problem, you need to use setUpClass() and tearDownClass, but before using this, you only need to add @classmethod in front of the method. In this way, when executing the three use cases, the browser will only open and close once, as shown in the figure :
import unittest
class Testproject(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("测试开始。。。")
    @classmethod
    def tearDownClass(cls):
        print("结束测试。。。")
    def test_001(self):
        print("这是第一个测试用例")
    def test_two(self):
        print("这是第二个测试用例")
    def test_003(self):
        print("这是第三个测试用例")
if __name__ == '__main__':
        unittest.main()

operation result:Insert image description here

2. Test cases
  1. In unitt, the function of the test case starts with test. When executing the test case, unittest will automatically recognize that the function starting with test is the test code. Remember to start with test in lowercase! As shown in the above pictures, they all start with test. The test firmware is included in the test case and can be written or not.
  2. In the test case, when we do not want to execute a certain use case, we only need to add the decorator @unittest.skip before the test case to skip the use case and execute the following use case
import unittest
class Testproject(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("测试开始。。。")
    @classmethod
    def tearDownClass(cls):
        print("结束测试。。。")
    def test_001(self):
        print("这是第一个测试用例")
    def test_two(self):
        print("这是第二个测试用例")
    def test_003(self):
        print("这是第三个测试用例")
    def test_four(self):
        print("这是第四个测试用例")
if __name__ == '__main__':
        unittest.main()

Before adding decoration, all four use cases will be executed. After adding decorator, the code is as follows:

import unittest
class Testproject(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("测试开始。。。")
    @classmethod
    def tearDownClass(cls):
        print("结束测试。。。")
    def test_001(self):
        print("这是第一个测试用例")
    def test_two(self):
        print("这是第二个测试用例")
    def test_003(self):
        print("这是第三个测试用例")
    @unittest.skip
    def test_four(self):
        print("这是第四个测试用例")
if __name__ == '__main__':
        unittest.main()

At this time, the fourth use case will not be executed, and the results are as follows:
Insert image description here

3. Test actuator

The test executor (testrunner) is used to execute test cases. It can execute a single test case or execute test cases in batches through a test suite. The test executor has two execution methods.

  1. Directly use unittest.main() to execute. When unittest.main() is used to execute directly, unittest will search for all test cases starting with test and execute multiple test cases in ASCII order. The above picture does not execute in the order of one, two, and three. That's it
  2. Use the TextTestRunner() method to initialize a test executor, then add test cases to the test suite, and execute the test suite through run() to execute the test cases. So what is a test suite? A test suite is a collection of test cases. When the test cases are executed according to the test suite, they will be executed according to the loading order of the use cases. Use the addTest() method to load the test method into the test suite. If you want to addTest() Pass in the test method under another package: package name.class name (test method function)
if __name__=='__main__':
    suite = unittest.TestSuite()
    suite.addTest(Testproject('test_001'))  # Testproject是类名,test_001是测试函数
    suite.addTest(Testproject('test_two'))  # 如果是Testproject是其他包下的,就是包名.Testproject(‘test_002’)
    suite.addTest(Testproject('test_003'))  # 从上到下依次执行
    runner = unittest.TextTestRunner()
    runner.run(suite)
4. Test suite
  1. Use makeSuite() to load all test methods under a class into the test suite
import unittest
class Testproject(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("测试开始。。。")
    @classmethod
    def tearDownClass(cls):
        print("结束测试。。。")
    def test_001(self):
        print("这是第一个测试用例")
    def test_two(self):
        print("这是第二个测试用例")
    def test_003(self):
        print("这是第三个测试用例")
    @unittest.skip
    def test_four(self):
        print("这是第四个测试用例")
suite = unittest.TestSuite(unittest.makeSuite(Testproject))
if __name__=='__main__':
    runner = unittest.TextTestRunner()
    runner.run(suite)
  1. The most commonly used method is to use the discover() method to load all test methods under the specified path into the test suite.
test_dir = r'C:\Users\fsy\PycharmProjects\testing\testing'  #测试用例的路径
discover = unittest.defaultTestLoader.discover(test_dir, pattern='fi*.py')
runner = unittest.TextTestRunner()
runner.run(discover)

Insert image description here
test_dir is the specified path, pattern is the specified file name. The pattern parameter defines the file name matching rules and can be modified at will.
For example, fi*.py here refers to the file whose file name starts with fi. It
can also be written as test file. If the file with test
is *fist.py, it refers to the file name. Files ending with fist.py
* play a similar role to wildcards
3. During testing, you can also add the entire test class to the test suite and then execute it. suite.addTest(Testproject) will execute all use cases under the Testproject class

Guess you like

Origin blog.csdn.net/qq_41500249/article/details/106367114