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)
  • 相关阅读:
    面试题19:包含min函数的栈
    编程之美 计算字符串的相似度
    android 数据持久化——I/O操作
    SSD磁盘,CPU居高不下,高并发的情况下,是不是mysql解析器耗费的cpu资源高?
    Eclipse、MyEclipse优化,提高运行速度
    Sonar入门学习
    Oracle 生成指定范围内随机日期
    ios中的GCD
    如何使用Win8系统自带杀毒软件
    安装Ubuntu版本linux过程中没有提示设置root用户密码问题的解决办法
  • 原文地址:https://www.cnblogs.com/zhang-da/p/12291945.html
Copyright © 2011-2022 走看看