def import_junit(junit_file, **kwargs):
    """Reads the content of the junit-results file produced by pytest and returns imported data."""
    xml_root = _get_xml_root(junit_file)

    results = []
    for test_data in xml_root:
        if test_data.tag != 'testcase':
            continue

        verdict, comment, properties = _parse_testcase_record(test_data)

        title = test_data.get('name')
        classname = test_data.get('classname')
        time = test_data.get('time', 0)
        filepath = test_data.get('file')

        data = [
            ('title', title),
            ('classname', classname),
            ('verdict', verdict),
            ('comment', comment),
            ('time', time),
            ('file', filepath),
        ]
        for key in properties:
            data.append((key, properties[key]))

        results.append(OrderedDict(data))

    return exporter.ImportedData(results=results, testrun=None)
Exemplo n.º 2
0
def get_imported_data(csv_file, **kwargs):
    """Reads the content of the Polarion exported csv file and returns imported data."""
    open_args = []
    open_kwargs = {}
    try:
        # pylint: disable=pointless-statement
        unicode
        open_args.append('rb')
    except NameError:
        open_kwargs['encoding'] = 'utf-8'
    with open(os.path.expanduser(csv_file), *open_args, **open_kwargs) as input_file:
        reader = _get_csv_reader(input_file)

        fieldnames = _get_csv_fieldnames(reader)
        if not fieldnames:
            raise Dump2PolarionException(
                "Cannot find field names in CSV file '{}'".format(csv_file))

        results = _get_results(reader, fieldnames)
        if not results:
            raise Dump2PolarionException(
                "No results read from CSV file '{}'".format(csv_file))

        testrun = _get_testrun_from_csv(input_file, reader)

    return exporter.ImportedData(results=results, testrun=testrun)
def _parse_ostriz(ostriz_data):
    """Reads the content of the input JSON and returns testcases results."""
    if not ostriz_data:
        raise NothingToDoException("No data to import")

    results = []
    found_build = None
    for test_path, test_data in six.iteritems(ostriz_data):
        curr_build = test_data.get('build')
        if not curr_build:
            continue

        # set `found_build` from first record where it's present
        if not found_build:
            found_build = curr_build

        # make sure we are collecting data for the same build
        if found_build != curr_build:
            continue

        if not test_data.get('statuses'):
            continue

        _append_record(test_data, results, test_path)

    testrun_id = _get_testrun_id(found_build)
    return exporter.ImportedData(results=results, testrun=testrun_id)
Exemplo n.º 4
0
def import_junit(junit_file, **kwargs):
    """Reads the content of the junit-results file produced by pytest and returns imported data."""
    try:
        tree = ElementTree.parse(os.path.expanduser(junit_file))
    except Exception as err:
        raise Dump2PolarionException(
            "Failed to parse XML file '{}': {}".format(junit_file, err))
    xml_root = tree.getroot()

    results = []
    for test_data in xml_root:
        if test_data.tag != 'testcase':
            continue

        verdict = None
        verdict_found = False
        comment = ''
        properties = {}
        for element in test_data:
            if not verdict_found:
                if element.tag == 'error':
                    verdict = 'failed'
                    comment = element.get('message')
                    # continue to see if there's more telling verdict for this record
                elif element.tag == 'failure':
                    verdict = 'failed'
                    comment = element.get('message')
                    verdict_found = True
                elif element.tag == 'skipped':
                    verdict = 'skipped'
                    comment = element.get('message')
                    verdict_found = True
            if element.tag == 'properties':
                for prop in element:
                    properties[prop.get('name')] = prop.get('value')
        if not verdict:
            verdict = 'passed'

        title = test_data.get('name')
        classname = test_data.get('classname')
        time = test_data.get('time', 0)
        filepath = test_data.get('file')

        data = [
            ('title', title),
            ('classname', classname),
            ('verdict', verdict),
            ('comment', comment),
            ('time', time),
            ('file', filepath),
        ]
        for key in properties:
            data.append((key, properties[key]))

        record = OrderedDict(data)
        results.append(record)

    return exporter.ImportedData(results=results, testrun=None)
Exemplo n.º 5
0
def _parse_ostriz(ostriz_data):
    """Reads the content of the input JSON and returns testcases results."""
    if not ostriz_data:
        raise NothingToDoException("No data to import")

    results = []
    found_build = None
    for test_path, test_data in six.iteritems(ostriz_data):
        # make sure we are collecting data for the same build
        if found_build:
            if found_build != test_data.get('build'):
                continue
        # Every record should have "build" key. Skip if doesn't and
        # set `found_build` from first record where it's present.
        else:
            found_build = test_data.get('build')
            if not found_build:
                continue

        statuses = test_data.get('statuses')
        if not statuses:
            continue

        jenkins_data = test_data.get('jenkins', {})

        data = [('title', test_data.get('test_name')
                 or _get_testname(test_path)),
                ('verdict', statuses.get('overall')),
                ('source', test_data.get('source')),
                ('job_name', jenkins_data.get('job_name')),
                ('run', jenkins_data.get('build_number')),
                ('params', _filter_parameters(test_data.get('params'))),
                ('time',
                 _calculate_duration(test_data.get('start_time'),
                                     test_data.get('finish_time')) or 0)]
        test_id = test_data.get('polarion')
        if test_id:
            if isinstance(test_id, list):
                test_id = test_id[0]
            data.append(('test_id', test_id))

        results.append(OrderedDict(data))

    testrun_id = _get_testrun_id(found_build)
    return exporter.ImportedData(results=results, testrun=testrun_id)
Exemplo n.º 6
0
def import_sqlite(db_file, older_than=None, **kwargs):
    """Reads the content of the database file and returns imported data."""
    conn = _open_sqlite(db_file)
    cur = conn.cursor()
    # get rows that were not exported yet
    select = "SELECT * FROM testcases WHERE exported != 'yes'"
    if older_than:
        cur.execute(' '.join((select, "AND sqltime < ?")), (older_than, ))
    else:
        cur.execute(select)
    columns = [description[0] for description in cur.description]
    rows = cur.fetchall()

    # map data to columns
    results = []
    for row in rows:
        record = OrderedDict(list(zip(columns, row)))
        results.append(record)

    testrun = _get_testrun_from_sqlite(conn)

    conn.close()

    return exporter.ImportedData(results=results, testrun=testrun)
Exemplo n.º 7
0
def _parse_ostriz(ostriz_data):
    """Reads the content of the input JSON and returns testcases results."""
    if not ostriz_data:
        raise Dump2PolarionException("No data to import")
    test_data = []
    data = found_version = None
    for data in ostriz_data.values():
        # make sure we are collecting data for the same appliance version
        if found_version:
            if found_version != data.get('version'):
                continue
        else:
            found_version = data.get('version')
        statuses = data.get('statuses')
        if not statuses:
            continue
        test_data.append(
            OrderedDict([('verdict', statuses.get('overall')),
                         ('title', data.get('test_name')),
                         ('time',
                          _calculate_duration(data.get('start_time'),
                                              data.get('finish_time')) or 0)]))
    testrun_id = _get_testrun_id(found_version)
    return exporter.ImportedData(results=test_data, testrun=testrun_id)