(2022.11.27 Sun)
 unittest是Python自带的单元测试框架。unittest+html和pytest+allure(测试报告)成为常用的自动测试和报告的框架组合。

unittest-archi-2022-11-23-2114.png
概念
- test case测试用例:测试用例是测试的基本单元,用于测试一组特定输入的特定响应,unittest提供了基类unittest.TestCase用于创建测试案例。案例包括“输入用户名不输入密码,则提示密码为空”等。
- test fixture测试脚手架:为开展测试需要进行的准备工作,以及所有相关的清理操作(cleanup actions),比如创建临时或代理数据库、目录,启动一个服务器进程等。
- test suite测试套件:一系列的测试用例或测试套件,用于整合一些一起执行的测试。
- test runner测试运行器:用于执行和输出测试结果的组件,可使用图形接口、文本接口,或返回运行测试结果的特定值。
案例
# unittest_basic_example01.py
import logging
import unittest
class Login:
    pass
class TestStringMethods(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # 必须使用@classmethod 装饰器,所有test运行前运行一次
        super().setUpClass()
        logging.info("setUpClass")
    @classmethod
    def tearDownClass(cls):
        # 必须使用@classmethod, 所有test运行完后运行一次
        super().tearDownClass()
        logging.info("tearDownClass")
    def setUp(self):
        # 每个测试用例执行之后做操作
        # do preparation
        super().setUp()
        logging.info("setUp")
    def tearDown(self):
        # 每个测试用例执行之前做操作
        super().tearDown()
        logging.info("tearDown")
    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')
    def test_isupper(self):
        logging.info('method test_isupper is in progress')
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())
    def test_split(self):
        s = 'hello world'
        logging.info('method test_split is in progress')
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)
if __name__ == '__main__':
    format = "%(asctime)s: %(message)s"
    logging.basicConfig(format=format, level=logging.INFO) #, datefmt="%H:%M:%S.%f"    
    unittest.main()
运行结果
$ python unittest_basic_example01.py 
2022-11-27 14:48:37,872: setUpClass
2022-11-27 14:48:37,873: setUp
2022-11-27 14:48:37,873: method test_isupper is in progress
2022-11-27 14:48:37,873: tearDown
.2022-11-27 14:48:37,873: setUp
2022-11-27 14:48:37,873: method test_split is in progress
2022-11-27 14:48:37,873: tearDown
.2022-11-27 14:48:37,873: setUp
2022-11-27 14:48:37,873: tearDown
.2022-11-27 14:48:37,873: tearDownClass
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK
该案例给出若干测试用例的结果和方法的执行顺序。
案例中定义了单元测试类TestStringMethods,在import unittest之后,定义该单元测试类之时,继承unittest.TestCase,使得该类成为一个unittest类。
unittest.TestCase有四个基本方法
- setUpClass
- setUp
- tearDownClass
- tearDown
注意这四个方法针对不同的测试用例和测试用例类,两两成一对,即setUp和tearDown,setUpClass和tearDownClass。
setUpClass和tearDownClass都用@classmethod装饰器装饰为类方法,这两个方法分别在TestStringMethods的所有test case之前和之后运行一次。
setUp和tearDown则针对test case而言,每个test case执行前、后分别执行这两个方法。用于做测试的准备和收尾工作。
test case都以test作为方法的开始,这个命名传统用于向test runner通知哪些方法代表着test case。
观察测试结果,两个test case中, test_isupper先于test_split执行。默认情况下,test case的执行顺序为方法名的字典序(alphabetical)。同时还有其他若干方法可以调整test case的执行顺序。
断言assert
test case中最常用的断言方法
| method | checks that | new in | 
|---|---|---|
| assertEqual(a, b) | a == b | |
| assertNotEqual(a, b) | a != b | |
| assertTrue(x) | bool(x) is True | |
| assertFalse(x) | bool(x) is False | |
| assertIs(a, b) | a is b | 3.1 | 
| assertIsNot(a, b) | a is not b | 3.1 | 
| assertIsNone(x) | x is None | |
| assertIsNotNone(x) | x is not None | |
| assertIn(a, b) | a in b | 3.1 | 
| assertNotIn(a, b) | a not in b | 3.1 | 
| assertIsInstance(a, b) | isinstance(a, b) | 3.2 | 
| assertNotIsInstance(a, b) | not isinstance(a, b) | 3.2 | 
Reference
1 经验分享:自动化测试框架之unittest,测试小涛
 2 unittest教程(2w字实例合集)——Python自动化测试一文入门,是羽十八ya
 3 python unittest official doc
 4 Python unittest.TestCase execution order, stackoverflow



















