Esempio n. 1
0
def get_test_params():
    tests_to_execute = collect_tests()
    pytest_args = []
    if get_core_args().rerun:
        failed_tests_file = os.path.join(PathManager.get_working_dir(),
                                         'lastfail.txt')
        tests_dir = os.path.join(PathManager.get_tests_dir(),
                                 get_core_args().target)
        failed_tests = []
        with open(failed_tests_file, 'r') as f:
            for line in f:
                failed_tests.append(line.rstrip('\n'))
        f.close()
        # Read first line to see if these tests apply to current target.
        if tests_dir in failed_tests[0]:
            pytest_args = failed_tests
        else:
            logging.error(
                'The -a flag cannot be used now because the last failed tests don\'t match current target.'
            )
    else:
        if len(tests_to_execute) > 0:
            for running in tests_to_execute:
                pytest_args.append(running)
        else:
            exit_iris('No tests to execute.', status=1)
    return pytest_args
Esempio n. 2
0
def scan_all_tests():
    tests_directory = PathManager.get_tests_dir()
    logger.debug('Path %s found. Checking content ...', tests_directory)

    test_list = {}
    rootdir = tests_directory.rstrip(os.sep)
    start = rootdir.rfind(os.sep) + 1
    exclude_dirs = {'images', '.pytest_cache', '__pycache__'}
    exclude_files = {'__init__.py', 'pytest.ini', '.DS_Store'}

    for path, dirs, files in PathManager.sorted_walk(rootdir):
        [dirs.remove(d) for d in list(dirs) if d in exclude_dirs]
        [files.remove(d) for d in list(files) if d in exclude_files]

        folders = path[start:].split(os.sep)
        subdir = dict.fromkeys(files)
        parent = reduce(dict.get, folders[:-1], test_list)
        parent[folders[-1]] = subdir

        if len(files) > 0:
            if os.path.isdir(path):
                my_plugin = TestCollector()
                pytest.main(['--collect-only', '-p', 'no:terminal', path],
                            plugins=[my_plugin])
                for module in my_plugin.get_collected_items():
                    try:
                        module_path = str(module.fspath)
                        module_name = os.path.basename(module_path)
                        temp = module_path.split(
                            '%stests%s' %
                            (os.sep, os.sep))[1].split(module_name)[0]
                        package = os.path.join('tests', temp)
                        current_test = module.own_markers[0].kwargs
                        test_object = {
                            'name': module_name,
                            'module': module_path,
                            'description': current_test.get('description'),
                            'package': package
                        }
                        if not current_test.get('values'):
                            pass
                        else:
                            for value in current_test.get('values').kwargs:
                                test_object[value] = current_test.get(
                                    'values').kwargs[value]
                        subdir[module_name] = test_object

                    except TypeError as e:
                        logger.warning('Error in test - %s: %s' %
                                       (module, e.message))
                    except AttributeError:
                        logger.warning('[%s] is not a test file. Skipping...',
                                       module)
    return test_list
Esempio n. 3
0
def collect_tests():
    """Collects tests based on include/exclude criteria and selected target."""
    target = core_args.target
    test_list = []

    include = core_args.test
    exclude = core_args.exclude
    if os.path.isfile(include):
        with open(include, 'r') as f:
            for line in f:
                test_list.append(line.rstrip('\n'))
        f.close()
    else:
        tests_dir = os.path.join(PathManager.get_tests_dir(), target)
        if not os.path.exists(tests_dir):
            path_warning(tests_dir)
            return test_list

        logger.debug('Path %s found. Checking content ...', tests_dir)
        for dir_path, sub_dirs, all_files in PathManager.sorted_walk(tests_dir):
            for current_file in all_files:
                directory = '%s%s%s' % (os.sep, core_args.directory, os.sep)
                include_params = [include]
                exclude_params = [exclude]
                if ',' in include:
                    include_params = include.split(',')
                if ',' in exclude:
                    exclude_params = exclude.split(',')
                current_full_path = os.path.join(dir_path, current_file)
                if current_file.endswith('.py') and not current_file.startswith('__'):
                    if include is '' and exclude is '' and directory is '':
                        if not current_full_path in test_list:
                            test_list.append(current_full_path)
                    else:
                        if core_args.directory is '' or directory in current_full_path:
                            for include_param in include_params:
                                if include_param is '' or include_param in current_full_path:
                                    for exclude_param in exclude_params:
                                        if exclude_param is '':
                                            if not current_full_path in test_list:
                                                test_list.append(current_full_path)
                                        else:
                                            if exclude_param not in current_full_path:
                                                if not current_full_path in test_list:
                                                    test_list.append(current_full_path)
        if len(test_list) == 0:
            logger.error('\'%s\' does not contain tests based on your search criteria. Exiting program.' % tests_dir)
        else:
            logger.debug('List of all tests found: [%s]' % ', '.join(map(str, test_list)))

    return test_list