【问题标题】:nosetest - run all tests in subdirectoriesnosetest - 运行子目录中的所有测试
【发布时间】:2018-11-01 15:26:49
【问题描述】:

我正在运行下面的nosetest 命令,但它只运行第一个文件夹中的测试,我在子目录中有额外的测试。如何编辑命令以在我指定的文件夹的所有子目录中运行以下所有测试?

nosetests -s -v --ckan --with-pylons=test-core.ini ckan/tests/legacy/*

这是下面目录中测试的基本结构

https://github.com/ckan/ckan/tree/ckan-2.7.3/ckan/tests/legacy

【问题讨论】:

    标签: python unit-testing nose


    【解决方案1】:

    您可以编写生成所有必需路径的脚本。

    我用的东西是:

    #coding: utf-8
    import os
    import logging
    log = logging.getLogger()
    skip_list = []
    
    def get_test_files(cur_path, test_files = []):
        """ Find all files that started with test_ """
        cur_dir_content = os.listdir(cur_path)
        for i in cur_dir_content:
            full_path = os.path.join(cur_path, i)
            if os.path.islink(full_path):
                continue
            if os.path.isfile(full_path) and is_file_test(full_path):
                test_files.append(full_path)
            elif os.path.isdir(full_path):
                get_test_files(full_path, test_files)
        return test_files
    
    def is_file_test(full_path):
        """ Check if file is test"""
        file_name, file_ext = os.path.splitext(os.path.basename(full_path))
        if file_name[:5] == "test_" and file_ext == ".py":
            if is_in_skip(full_path):
                log.info("FILE: %s ... SKIP", full_path)
                return False
            return True
        return False
    
    def is_in_skip(full_path):
        for i in skip_list:
            if full_path.find(i) >= 0:
                return True
        return False
    
    def execute_all_with_nosetests(path_list):
        """ Test with nosetests.
        """
        #compute paths
        cur_dir = os.getcwd()
        paths = map(lambda x: os.path.abspath(x), path_list)
        relpath = "INSERT YOUR PATH"
        paths = map(lambda x: os.path.relpath(x, relpath), paths)
        paths = " ".join(paths)
        os.chdir("INSERT TEST RUN FOLDER")
        cmd = "nosetests -s -v --ckan --with-pylons=test-core.ini " + paths
        os.system(cmd)
        os.chdir(cur_dir)
    
    if __name__ == "__main__":
        test_list = get_test_files("INSERT_BASE_PATH")
        execute_all_with_nosetests(test_list)
    

    将带有“INSERT”的字符串替换到您自己的文件夹中。此外,还需要更正 execute_all_with_nosetests 函数。 我不运行它,但它是我的测试运行脚本的下标。

    【讨论】:

    • 哇,成功了。不知道我们需要编写脚本来循环它。
    【解决方案2】:

    如果您按以下方式执行测试,则应执行目录中的所有测试,包括子目录:

    nosetests --reset-db --nologcapture --with-pylons=test-core.ini ckan.tests.legacy -s -v

    希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-25
      相关资源
      最近更新 更多