Пример #1
0
def _get_tests(fname, selector=None, nose_params=NOSE_COLLECT_PARAMS):
    """
    Collect tests and return them.
    
    :param fname: The tests will be written to fname in xunit format 
    :param selector: Tests are filtered based on selector
    :return: A test suite as returned by xunitparser with all the tests available
             in the w3af framework source code, without any selectors.
    """
    output_file = os.path.join(ARTIFACT_DIR, fname)
    collect_with_output = nose_params % output_file
    
    if selector is not None:
        cmd = '%s %s -A "%s" w3af/' % (NOSETESTS,
                                       collect_with_output,
                                       selector)
    else:
        cmd = '%s %s w3af/' % (NOSETESTS, collect_with_output)
    
    cmd_args = shlex.split(cmd)
    
    logging.debug('Collecting tests: "%s"' % cmd)
    p = subprocess.Popen(
        cmd_args,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        shell=False,
        universal_newlines=True
    )
    
    # Wait for it to finish
    stdout, stderr = p.communicate()

    if p.returncode != 0:
        logging.critical('Error while collecting tests!')
        logging.critical(stdout)
        logging.critical(stderr)
        sys.exit(1)

    test_suite, test_result = parse_xunit(output_file)
    
    normalize_test_names(test_suite)
    
    logging.debug('Collected %s tests.' % test_result.testsRun)
    
    return test_suite
Пример #2
0
def _get_tests(fname, selector=None, nose_params=NOSE_COLLECT_PARAMS):
    """
    Collect tests and return them.
    
    :param fname: The tests will be written to fname in xunit format 
    :param selector: Tests are filtered based on selector
    :return: A test suite as returned by xunitparser with all the tests available
             in the w3af framework source code, without any selectors.
    """
    output_file = os.path.join(ARTIFACT_DIR, fname)
    collect_with_output = nose_params % output_file

    if selector is not None:
        cmd = '%s %s -A "%s" w3af/' % (NOSETESTS, collect_with_output,
                                       selector)
    else:
        cmd = '%s %s w3af/' % (NOSETESTS, collect_with_output)

    cmd_args = shlex.split(cmd)

    logging.debug('Collecting tests: "%s"' % cmd)
    p = subprocess.Popen(cmd_args,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         shell=False,
                         universal_newlines=True)

    # Wait for it to finish
    stdout, stderr = p.communicate()

    if p.returncode != 0:
        logging.critical('Error while collecting tests!')
        logging.critical(stdout)
        logging.critical(stderr)
        sys.exit(1)

    test_suite, test_result = parse_xunit(output_file)

    normalize_test_names(test_suite)

    logging.debug('Collected %s tests.' % test_result.testsRun)

    return test_suite
Пример #3
0
def get_run_tests():
    """
    Merge all the information from the command outputs into one consolidated
    test suite which contains all tests which were run.
    
    :return: A list with the names of the tests which were run in the same
             format as collect_all_tests to be able to compare them.
    """
    test_suite = None
    msg_fmt = 'Reading %s run tests from: "%s"'

    for fname in os.listdir(ARTIFACT_DIR):
        if fname.startswith(NOSE_OUTPUT_PREFIX) and \
        fname.endswith(NOSE_XUNIT_EXT):

            path_fname = os.path.join(ARTIFACT_DIR, fname)
            try:
                curr_test_suite, test_result = parse_xunit(path_fname)
            except ElementTree.ParseError:
                logging.warning('"%s" is an invalid XML file.' % fname)
                continue

            logging.debug(msg_fmt % (test_result.testsRun, fname))

            # Merge all the tests.
            if test_suite is None:
                test_suite = curr_test_suite
            else:
                for test in curr_test_suite:
                    test_suite.addTest(test)

    normalize_test_names(test_suite)

    run_str = '\n'.join(sorted([t.id() for t in test_suite._tests]))

    logging.debug('Run %s tests.' % len(test_suite._tests))
    logging.debug('The following tests were run:\n%s' % run_str)

    return test_suite
Пример #4
0
def get_run_tests():
    """
    Merge all the information from the command outputs into one consolidated
    test suite which contains all tests which were run.
    
    :return: A list with the names of the tests which were run in the same
             format as collect_all_tests to be able to compare them.
    """
    test_suite = None
    msg_fmt = 'Reading %s run tests from: "%s"'
    
    for fname in os.listdir(ARTIFACT_DIR):
        if fname.startswith(NOSE_OUTPUT_PREFIX) and \
        fname.endswith(NOSE_XUNIT_EXT):
            
            path_fname = os.path.join(ARTIFACT_DIR, fname)
            try:
                curr_test_suite, test_result = parse_xunit(path_fname)
            except ElementTree.ParseError:
                logging.warning('"%s" is an invalid XML file.' % fname)
                continue
            
            logging.debug(msg_fmt % (test_result.testsRun, fname))
            
            # Merge all the tests.
            if test_suite is None:
                test_suite = curr_test_suite
            else:
                for test in curr_test_suite:
                    test_suite.addTest(test)
    
    normalize_test_names(test_suite)
    
    run_str = '\n'.join(sorted([t.id() for t in test_suite._tests]))
    
    logging.debug('Run %s tests.' % len(test_suite._tests))
    logging.debug('The following tests were run:\n%s' % run_str)

    return test_suite