python unittest 加载测试用例名称实现不是test开头的

纯属无聊忽然想到,想修改后unittest会执行以amize开头的case

  1. 方法1
    修改源码loader.py

'''
loader.py
'''
class TestLoader(object):
      ....
      testMethodPrefix = 'test' #修改成amize就会变成只加载amize开头的
  1. 方法2
    利用继承的方法
#a 如果用defaultTestLoader.discover 加载case就如下:
class MyTestLoader(unittest.TestLoader):
    testMethodPrefix = 'amize'   #修改成amize
defaultTestLoader = MyTestLoader()

suites = defaultTestLoader.discover('../testcase', '*.py')
runner = unittest.TextTestRunner()
runner.run(suites)

#b 如果用main()直接执行的:
    class MyTestLoader(unittest.TestLoader):
        testMethodPrefix = 'amize'
    defaultTestLoader = MyTestLoader()
    main = unittest.TestProgram
    main(testLoader=defaultTestLoader)

  1. 方法3
    看到HTMLTestRunner里面的用法
# Note: Reuse unittest.TestProgram to launch test. In the future we may
# build our own launcher to support more specific command line
# parameters like test title, CSS, etc.
class TestProgram(unittest.TestProgram):
    """
    A variation of the unittest.TestProgram. Please refer to the base
    class for command line parameters.
    """
    def runTests(self):
        # Pick HTMLTestRunner as the default test runner.
        # base class's testRunner parameter is not useful because it means
        # we have to instantiate HTMLTestRunner before we know self.verbosity.
        if self.testRunner is None:
            self.testRunner = HTMLTestRunner(verbosity=self.verbosity)
        unittest.TestProgram.runTests(self)

main = TestProgram

##############################################################################
# Executing this module from the command line
##############################################################################

if __name__ == "__main__":
    main(module=None)
posted @ 2020-07-07 17:44  该显示昵称已被使用了  阅读(501)  评论(0编辑  收藏  举报