Use of UnitTest framework

Insert image description here

1. What is the UnitTest framework?

The UnitTest framework is a unit testing framework that comes with python. It is mainly used for unit testing. It has the following characteristics:

  • Ability to organize multiple use cases for execution
  • Provides rich assertion methods
  • Ability to generate test reports

2. Core elements of UnitTest

1.TestCase (the core module)
TestCase (test case), please note that our test case is a component of the unittest framework, not what we call test case in manual and automated processes

Main function: Each TestCase is a code file. The real use case code is written in this code file.

2.TestSuite
TestSuite (test suite) is used to manage and assemble multiple TestCase

3.TestRunner
TestRunner (test execution, test run), used to execute TestSuite (test suite)

4.TestLoader
TestLoader (test loading), the function is a supplement to the TestSuite (test suite) function, used to manage and assemble multiple TestCase

5.Fixture
Fixture (test fixture), written in the TestCase (test case) code, is a code structure that can be executed before and after each method.

3. TestCase

TestCase is a code file. The real use case code is written in the code file. The name of the code file must be written according to the rules of the identifier (the function of the code can be explained at the beginning of the file using annotations)

Steps for usage:

  1. Guide package (unittest)
  2. Custom test class
  3. Write test methods in test class
  4. Execute use case
"""
    代码目的: 学习TestCase(测试用例)模块的书写方法
"""

# 1. 导包
import unittest

# 2. 自定义测试类,需要继承unittest模块中的TestCase类即可
class TestDemo(unittest.TestCase):
    # 3. 书写测试方法, 也就是真正的测试代码
    # 书写要求,测试方法必须以test_开头(本质是以test开头)
    def test_method1(self):
        print('TestCase1')
    def test_method2(self):
        print('TestCase2')
# 4. 执行测试用例

When executing the test case method, we have the following two ways:

  1. Put the cursor after the class name and run, all test methods in the class will be executed.
    Insert image description here

Insert image description here
2. Place the cursor after the method name and execute only the current method.
Insert image description here
Insert image description here

Frequently Asked Questions
1. Irregular naming of code files

1. The name of the code file starts with a number
2. There are spaces in the name of the code file
3. There are Chinese characters in the name of the code file
4. Other special symbols
(composed of numbers, letters, and underscores, cannot start with numbers)

2. There is no result when running the code

There is no prompt for unittests for when right-clicking to run, and
the solution to the problem is:

  1. Create a new code file and copy the written code into it
  2. Delete the existing running mode
    Insert image description here
    Insert image description here
    so that the unittest mode is used by default.

3. No use case found.
The test method does not start with test, or the word is written incorrectly.

四、TestSuite & TestRunner

TestSuite (test suite): manages the
TestRunner (test execution) that packages and assembles TestCase (test case) files: executes the TestSuite (suite)
steps:

  1. Guide package (unittest)
  2. Instantiate (create an object) a suite object
  3. Add use case method using suite object
    4. Instantiate run object
    Execute suite object using run object

Specific code:
TestSuite (test suite): is used to manage multiple TestCase, we need to create multiple TestCase files first
Insert image description here

"""
    学习 TestSuite 和 TestRunner使用
"""

# 1. 导包
import unittest

from test.testcase_1 import TestDemo1
from test.testcase_2 import TestDemo2

# 2. 实例化套件对象
suite = unittest.TestSuite()

# 3. 使用套件对象添加用例方法
     # 方式一: 套件对象.addTest(测试类名('方法名')) 建议直接复制
suite.addTest(TestDemo1('test_method1'))
suite.addTest(TestDemo1('test_method2'))
suite.addTest(TestDemo2('test_method1'))
suite.addTest(TestDemo2('test_method1'))

# 4. 实例化运行对象
runner = unittest.TextTestRunner()

# 5. 使用运行对象去执行套件
runner.run(suite)

Insert image description here
Second way to add test case methods using test suite object:

    # 方式二: 将一个测试类的所有方法进行添加
    # 套件对象.addTest(unitest.makeSuite(测试类名))
    # 缺点: makeSuite() 这个方法官方是不太建议用的
suite.addTest(unittest.makeSuite(TestDemo1))
suite.addTest(unittest.makeSuite(TestDemo2))

The following prompt message is given: DeprecationWarning: unittest.makeSuite() is deprecated and will be removed in Python 3.13. Please use unittest.TestLoader.loadTestsFromTestCase() instead.
suite.addTest(unittest.makeSuite(TestDemo1)). But the results can be output correctly.
Insert image description here
After we finish running, the above symbols have certain meanings:
. means the use case passed, F means the use case failed, and E error means there is a problem with the use case code.

Notes:
1. unittest.TestSuite() and unittest.TextTestRunner() are methods and require parentheses
Insert image description here
Insert image description here
2. The runtime object is .TextTestRunner() not .TestRunner()
Insert image description here

Guess you like

Origin blog.csdn.net/buhuisuanfa/article/details/133966461