修改 unittest 发现测试方法的规则

文章目录

unittest 能自动发现并执行测试用例,规则如下:

  • 首先指定要查找的目录(默认是当前目录)。
    如果子目录属于 Python 包,则也加入查找范围。
  • 然后在该目录下查找名称匹配 test*.py 的文件。
  • 接着在该文件中查找继承 unittest.TestCase 的类,
  • 最后查找该类中名称以 test 开头的方法,作为测试用例来执行。

为了发现不是以 test 开头的测试方法,乃至更灵活地匹配,本文定义了一个继承 unittest.TestLoader 的子类。

代码

import sys
import unittest
import functools
from fnmatch import fnmatchcase


class MyTestLoader(unittest.TestLoader):
    testNamePatterns = None

    def getTestCaseNames(self, testCaseClass):
        """
        Customize this code to allow you to filter test methods through testNamePatterns.
        """
        def shouldIncludeMethod(attrname):
            if not attrname.startswith(self.testMethodPrefix):
                return False
            testFunc = getattr(testCaseClass, attrname)
            if not callable(testFunc):
                return False
            return self.testNamePatterns is None or \
                 any(fnmatchcase(attrname, pattern) for pattern in self.testNamePatterns)

        testFnNames = list(filter(shouldIncludeMethod, dir(testCaseClass)))
        if self.sortTestMethodsUsing:
            testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing))
        return testFnNames


if __name__ == '__main__':
    testLoader = MyTestLoader()
    testLoader.testMethodPrefix = ''
    testLoader.testNamePatterns = ['test*', '*test']    # 查找以 test 开头或结尾的测试方法(注意这里是 shell 风格的通配符)
    discovered_tests = testLoader.discover(start_dir='.', pattern='test*.py')
    result = unittest.TextTestRunner(verbosity=2).run(discovered_tests)
    sys.exit(0 if len(result.failures) + len(result.errors) == 0 else 1)

运行效果

[root@CentOS ~]# python3 runner.py
minus_test (test1.TestMath) ... FAIL
test_add (test1.TestMath) ... ok
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值