zoukankan      html  css  js  c++  java
  • TestLoader

    该discover方法接收三个参数:

    • start_dir:要测试的模块名或者测试用例的目录。

    • pattern="test*.py":表示用例文件名的匹配原则,默认匹配以test开头的文件名,星号表示后续的多个字符。

    • top_level_dir=None:测试模块的顶层目录,如果没有顶层目录,默认为None。

    注意!!!意!!

    • discover对给定的目录是有要求的,它只识别Python的包,也就是目录内有init.py文件的才算是Python的包,只要是要读取的目录,都必须是包

    • 关于start_dir和top_level_dir的几种情况:

      • start_dir目录可以单独指定,这个时候,让top_level_dir保持默认(None)即可。

      • start_dir == top_level_dir, start_dir目录与top_level_dir目录一致,discover寻找start_dir指定目录内的符合规则的模块。

      • start_dir < top_level_dir,start_dir目录是top_level_dir目录的子目录。discover寻找start_dir指定目录内的符合规则的模块。

      • start_dir > top_level_dir,start_dir目录如果大于top_level_dir目录,等待你的是报错AssertionError: Path must be within the project。说你指定的路径(start_dir)必须位于项目内(top_level_dir)。

    这里再补充一点。 我们知道,TestLoader类根据各种标准加载测试用例,并将它们返回给测试套件(suite)。但一般的,我们也可以不需要创建这个类实例(想要用某个类的方法,通常都是通过个该类的实例化对象调用)。unittest已经帮我们实例化好了TestLoader对象————defaultTestLoader,我们可以直接使用defaultTestLoader.discover。

    import os
    import unittest
    BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)))
    discover_path = os.path.join(BASE_DIR, 'discover')
    login_dir = os.path.join(discover_path, 'login')
    register_dir = os.path.join(discover_path, 'register')
    if __name__ == '__main__':
    suite = unittest.TestLoader().discover(
    start_dir=login_dir,
    pattern='test*.py',
    top_level_dir=discover_path,
    )
    print(suite.countTestCases())
    unittest.TextTestRunner(verbosity=2).run(suite)

    suite = unittest.defaultTestLoader.discover(
    start_dir=login_dir,
    pattern='test*.py',
    top_level_dir=discover_path,
    )
    print(suite.countTestCases())
    unittest.TextTestRunner(verbosity=2).run(suite)
  • 相关阅读:
    NOI2015 品酒大会
    网络流算法进阶详解
    ZROJ 1267 我要打数论
    后缀自动机构建图解
    WC集训DAY2笔记 组合计数 part.1
    最小割的数学模型
    二次剩余学习笔记
    Hadoop:eclipse配置hadoop-eclipse-plugin(版本hadoop2.7.3)
    MapReduce:实现文档倒序排序,且字符串拼接+年+月+日
    MapReduce:汇总学生表和成绩表为----学生成绩表
  • 原文地址:https://www.cnblogs.com/zhang-da/p/12291945.html
Copyright © 2011-2022 走看看