unittest框架系列七(unittest的TestLoader常用api说明)

TestLoader(测试加载)

欢迎加入测试交流群:夜行者自动化测试(816489363)进行交流学习QAQ

–成都-阿木木


Loading and running tests

class unittest.TestLoader

​ TestLoader类被用来创建测试套件和测试模块,使用时,通常不需要创建实例,unittest模块提供了一个实例可以被共享的实例,unittest.defaultTestLoader,如果使用子类和实例可以自定义一些可配置的属性。

TestLoader对象具有以下方法:

  • loadTestsFromTestCase(testCaseClass):返回一个套件中包含的所有测试用例TestCase的子类
#!/user/bin/env python
# -*- coding: utf-8 -*-

"""
------------------------------------
@Project : mysite
@Time    : 2020/8/31 9:29
@Auth    : chineseluo
@Email   : 848257135@qq.com
@File    : run.py
@IDE     : PyCharm
------------------------------------
"""
import unittest
from unittest_demo import TestStringMethods


if __name__ == '__main__':
    suite = unittest.TestSuite()
    load_case = unittest.TestLoader().loadTestsFromTestCase(TestStringMethods)
    suite.addTest(load_case)
    unittest.TextTestRunner().run(suite)
结果:
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
运行于测试方法前,主要用于环境初始化
this is a test_isupper method
运行于测试方法后,主要用户环境数据清理
运行于测试方法前,主要用于环境初始化
1
this is a test_split method
运行于测试方法后,主要用户环境数据清理
运行于测试方法前,主要用于环境初始化
this is a test_upper method
运行于测试方法后,主要用户环境数据清理
  • loadTestsFromName(name, module=None):传递测试方法的名字,测试模块
#!/user/bin/env python
# -*- coding: utf-8 -*-

"""
------------------------------------
@Project : mysite
@Time    : 2020/8/31 9:29
@Auth    : chineseluo
@Email   : 848257135@qq.com
@File    : run.py
@IDE     : PyCharm
------------------------------------
"""
import unittest
from unittest_demo import TestStringMethods


if __name__ == '__main__':
    suite = unittest.TestSuite()
    load_case = unittest.TestLoader().loadTestsFromName("test_upper", module=TestStringMethods)
    suite.addTest(load_case)
    unittest.TextTestRunner().run(suite)
  • loadTestsFromNames(name,module=None):传递测试模块中的测试方法名称,以及模块名
#!/user/bin/env python
# -*- coding: utf-8 -*-

"""
------------------------------------
@Project : mysite
@Time    : 2020/8/31 9:29
@Auth    : chineseluo
@Email   : 848257135@qq.com
@File    : run.py
@IDE     : PyCharm
------------------------------------
"""
import unittest
from unittest_demo import TestStringMethods


if __name__ == '__main__':
    suite = unittest.TestSuite()
    load_case = unittest.TestLoader().loadTestsFromNames(["test_upper", "test_isupper"], module=TestStringMethods)
    suite.addTest(load_case)
    unittest.TextTestRunner().run(suite)
结果为:
运行于测试方法前,主要用于环境初始化
this is a test_upper method
运行于测试方法后,主要用户环境数据清理
运行于测试方法前,主要用于环境初始化
this is a test_isupper method
运行于测试方法后,主要用户环境数据清理
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK
  • getTestCaseNames(TestCaseClass):获取testCaseClass子类中的测试方法名称排序
#!/user/bin/env python
# -*- coding: utf-8 -*-

"""
------------------------------------
@Project : mysite
@Time    : 2020/8/31 9:29
@Auth    : chineseluo
@Email   : 848257135@qq.com
@File    : run.py
@IDE     : PyCharm
------------------------------------
"""
import unittest
from unittest_demo import TestStringMethods


if __name__ == '__main__':
    print(unittest.TestLoader().getTestCaseNames(TestStringMethods))

  • discover(start_dir, pattern='test*.py',top_level_dir=None)start_dir指定开始目录,从指定目录开始测试模块扫描,找到所有测试模块,然后返回他们的testSuite对象。仅加载与模式匹配的测试文件,pattern是测试模块匹配规则,存放用例的目录属性必须是python package,必须要有__init__.py

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JZdoF7EX-1598930235604)(C:UsersluozhongwenAppDataRoamingTypora ypora-user-imagesimage-20200901111009307.png)]

#!/user/bin/env python
# -*- coding: utf-8 -*-

"""
------------------------------------
@Project : mysite
@Time    : 2020/8/28 11:32
@Auth    : chineseluo
@Email   : 848257135@qq.com
@File    : unittest_demo.py
@IDE     : PyCharm
------------------------------------
"""
import unittest
import sys


class TestStringMethods(unittest.TestCase):
    def setUp(self):
        print("运行于测试方法前,主要用于环境初始化")

    def tearDown(self):
        print("运行于测试方法后,主要用户环境数据清理")

    def test_upper(self):
        print("this is a test_upper method")
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        """
        这是一个描述
        :return:
        """
        print("this is a test_isupper method")
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        print(self.countTestCases())
        print("this is a test_split method")
        s = 'hello world'
        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)

    def fail(self, msg="test fail ha ha ha"):
        print("用例执行错误信息:{}".format(msg))


if __name__ == '__main__':
    unittest.main()

import unittest


if __name__ == '__main__':
    suite = unittest.defaultTestLoader.discover(start_dir="case", pattern="unittest_*.py", top_level_dir="../mysite")
    unittest.TextTestRunner().run(suite)
原文地址:https://www.cnblogs.com/chineseluo/p/13710526.html