zoukankan      html  css  js  c++  java
  • 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)
    
  • 相关阅读:
    SSM框架搭建(二) 创建MAVEN项目
    SSM框架搭建(一) JDK和MAVEN环境搭建
    Sublime Text3 手动 配置 NodeJs 环境
    spring-petclinic性能调优实战(转)
    algs4 使用 DrJava 编写 Hello World on Windows
    系统学习数据结构算法
    Algorithm 学习环境准备
    Gradle构建多模块项目
    使用 Gradle 构建 Java 项目
    Gradle目录结构详解
  • 原文地址:https://www.cnblogs.com/amize/p/13262204.html
Copyright © 2011-2022 走看看