Beispiel #1
0
def create_junit_results(data, output=None, **kwargs):
    """
    Creates a Junit result, can write to a file if desired, or return xml string. (used by Jenkins)
    input either dict(dict(dict())) or dict(list(dict()))
    dict = {suite: {test: {stderr,stdout,time,class,err,fail,skip}}}
    list = {suite: [(test, {stderr,stdout,time,class,err,fail,skip})]}
    :param data: A dictionary with dict or list hierarchy
    :param output: A filename to write results to  /path/to/file/*.junit.xml
    :return: Returns an XML string if no output, else nothing.
    """
    log.debug('creating junit results: output={}'.format(output))
    stdout_format = kwargs.pop('stdout_format', None)
    test_class = kwargs.pop('test_class', None)
    package = kwargs.pop('package', None)
    from junit_xml import TestSuite, TestCase
    test_suites = []
    for suite, tests in data.items():
        test_cases = []
        for test, result in (tests if isinstance(tests, list) else tests.items()):
            tc = TestCase(test)
            stdout = result.get('stdout')
            if stdout_format is not None and callable(stdout_format):
                if hasattr(stdout_format, 'func_code') and 'kwargs' in stdout_format.func_code.co_varnames:
                    stdout = stdout_format(stdout, suite_name=suite, test_name=test, **kwargs)
                else:
                    stdout = stdout_format(stdout)
            tc.stdout = stdout
            tc.stderr = result.get('stderr')
            tc.elapsed_sec = result.get('time')
            tc.classname = result.get('class', test_class)
            err = result.get('err')
            if err:
                tc.add_error_info(*err if isinstance(err, (list, tuple)) else [err])
            fail = result.get('fail')
            if fail:
                tc.add_failure_info(*fail if isinstance(fail, (list, tuple)) else [fail])
            skip = result.get('skip')
            if skip:
                tc.add_skipped_info(*skip if isinstance(skip, (list, tuple)) else [skip])
            test_cases.append(tc)
        ts = TestSuite(suite, test_cases, package=package)
        test_suites.append(ts)

    if output:
        check_makedir(os.path.dirname(output))
        with open(output, 'w') as out:
            TestSuite.to_file(out, test_suites)
        return output
    else:
        return TestSuite.to_xml_string(test_suites)
Beispiel #2
0
def map_yaml_to_junit(test):
    yaml = test.yaml or {}
    # Even though the name is `duration_ms` the value is in seconds.
    elapsed_sec = yaml.get("duration_ms", None)
    t = TestCase(test.description, classname=None, elapsed_sec=elapsed_sec)
    if test.result == "ok":
        if test.directive in ("SKIP", "TODO"):
            t.add_skipped_info(test.comment)
        else:
            t.stdout = test.comment

    elif test.result == "not ok":
        err_code = yaml.get("exitcode", 0)
        err_severity = yaml.get("severity", "")
        err_output = yaml.get("stack", "")
        error_message = f"{err_severity} ({err_code})"
        if err_code < 0 or err_severity == "crashed":
            t.add_error_info(error_message, err_output, err_code)
        else:
            t.add_failure_info(error_message, err_output, err_code)
        t.stderr = test.diagnostics
    return t
Beispiel #3
0
def test_availible_endpoints(output):

    s = Settings([
        'PLATFORM', 'ENVIRONMENT', 'SERVICEURL', 'WSDLURL', "CONSUMERHSAID",
        "CERTFILE"
    ])
    test_cases = []

    cache = SqliteCache(path='/tmp/_gcdrunner_sqlite.db', timeout=60 * 24 * 7)

    session = requests.Session()
    #session.verify = False
    #session.cert=s.CERTFILE

    transport = Transport(session=session, cache=cache)
    client = Client(s.WSDLURL, transport=transport)
    service = client.create_service(client.service._binding.name.text,
                                    s.SERVICEURL)

    session.verify = False
    session.cert = s.CERTFILE

    pid_fact = client.type_factory('ns2')
    adr_fact = client.type_factory('ns5')
    pid_t = pid_fact.PersonIdType('191212121212', '1.2.752.129.2.1.3.1')
    connectionPointId = get_connection_point_id(s.PLATFORM, s.ENVIRONMENT)

    requests.packages.urllib3.disable_warnings()
    endpoints = get_tak_la_for_tk(connectionPointId, s.CONSUMERHSAID)
    endpoints = list(
        filter(lambda e: e["logicalAddress"] not in IGNORE_LA_LIST, endpoints))
    endpoints.sort(key=lambda e: e["description"])
    for endpoint in endpoints:
        testname = "{} ({})".format(endpoint["description"],
                                    endpoint["logicalAddress"])

        print("Running test: {}".format(testname))
        logicalAddress_t = adr_fact.LogicalAddressType(
            endpoint['logicalAddress'])

        tc = TestCase(name=testname)
        tc.stdout = "Testing Logical address: {},  with id: {},  description: {}".format(
            endpoint['logicalAddress'], endpoint['id'],
            endpoint['description'])

        try:
            start = time.time()
            response = service.GetCareDocumentation(
                patientId=pid_t,
                _soapheaders={'LogicalAddress': logicalAddress_t})
            tc.elapsed_sec = time.time() - start
            tc.stdout = repr(response['result'])
        except Exception as e:
            tc.stderr = str(e)
            failtype = None
            if tc.stderr.startswith("VP"):
                failtype = tc.stderr.split()[0]
            tc.add_failure_info(output=str(e), failure_type=failtype)
        test_cases.append(tc)

    ts = TestSuite("Spider TK tests for TK: " + TKNS, test_cases)
    with open(output, 'w') as f:
        TestSuite.to_file(f, [ts])
Beispiel #4
0
def unittest_harness(tests, xml_outfile):
    xml_parser = TestSuite('unittest_harness')
    tests_results = {'Pass': Counter(), 'Warn': Counter(), 'Fail': Counter()}
    error_list, skip_list = [], []

    for test in tests:
        print(f'\t-> New test: {test.name}')

        if (tests_dir / test.name).is_file() is False:
            print(f'WARNING: {test.name} not found')
            skip_list.append(test.name)
            xml_entry = TestCase('',
                                 classname=test.name,
                                 elapsed_sec='None',
                                 status='Skipped')
            xml_entry.add_skipped_info(message='Not found')
            xml_parser.test_cases.append(xml_entry)
            continue

        test_id = f'unittest.{test.resolve().parts[-3]}.{test.name}'

        try:
            begin = process_time()
            test_proc = subprocess.run('./' + test.name,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE,
                                       shell=True,
                                       cwd=test.parent,
                                       check=True,
                                       encoding='utf-8')
            wall_time = process_time() - begin
        except subprocess.CalledProcessError as test_error:
            print(f'ERROR: {test.name} exited with a non-zero exit code.')
            print(f'Exit status: {test_error.returncode}')
            print(f'Output: {test_error.output}')
            print(f'Stderr output: {test_error.stderr}')
            error_list.append(test.name)
            xml_entry = TestCase('',
                                 classname=test_id,
                                 elapsed_sec=process_time() - begin,
                                 status='Error')
            xml_entry.add_error_info(message=test_error.stderr)
            xml_parser.test_cases.append(xml_entry)
            continue

        other_out = ''
        for test_output in test_proc.stdout.splitlines():
            try:
                tests_results[test_output[:4]][test.name] += 1
            except KeyError:
                print(f'\t\t\t{test_output.lstrip()}')
                other_out += test_output.lstrip() + '\n'
                continue

            print(f'\t\t{test_output}')

            try:
                # Look for the test output message enclosed between brackets
                test_msg = re.search(r'(?<=\[).+(?=\])', test_output).group()
            except AttributeError:
                # If brackets are missing, extract output message
                # independently of an eventual error message
                test_msg = test_output[6:].split('; error:')[0]

            xml_entry = TestCase(test_msg,
                                 classname=test_id,
                                 elapsed_sec=wall_time,
                                 status=test_output[:4])

            try:
                fail_msg = re.search('(?<=error:).+', test_output).group()
                if not fail_msg.lstrip():
                    fail_msg += 'Empty message'
                if test_output[:4] == 'Warn':
                    xml_entry.add_failure_info(message=fail_msg.lstrip(),
                                               failure_type='warning')
                else:
                    xml_entry.add_failure_info(message=fail_msg.lstrip())
            except AttributeError:
                pass

            if test_proc.stderr:
                xml_entry.stderr = test_proc.stderr

            if other_out:
                xml_entry.stdout = other_out[:-1]
                other_out = ''

            xml_parser.test_cases.append(xml_entry)

    display_results(tests_results, error_list, skip_list)

    with open(xml_outfile, 'w') as fid:
        xml_parser.to_file(fid, [xml_parser])