def test_get_test_results_with_duplicate_class_names(self):
        with_duplicates = ParsedUnitTestResultsWithCommit(
            files=1,
            suites=2, suite_tests=3, suite_skipped=4, suite_failures=5, suite_errors=6, suite_time=7,
            cases=[
                UnitTestCase(result_file='result', test_file='test1', line=123, class_name='class1', test_name='test1', result='success', message='message1', content='content1', time=1),
                UnitTestCase(result_file='result', test_file='test2', line=123, class_name='class1', test_name='test1', result='success', message='message2', content='content2', time=2),
    
                # success state has precedence over skipped
                UnitTestCase(result_file='result', test_file='test1', line=123, class_name='class1', test_name='test2', result='success', message='message3', content='content3', time=3),
                UnitTestCase(result_file='result', test_file='test2', line=123, class_name='class1', test_name='test2', result='skipped', message='message4', content='content4', time=4),
    
                # only when all runs are skipped, test has state skipped
                UnitTestCase(result_file='result', test_file='test1', line=123, class_name='class1', test_name='test3', result='skipped', message='message5', content='content5', time=5),
                UnitTestCase(result_file='result', test_file='test2', line=123, class_name='class1', test_name='test3', result='skipped', message='message6', content='content6', time=6),
    
                UnitTestCase(result_file='result', test_file='test1', line=123, class_name='class1', test_name='test4', result='success', message='message7', content='content7', time=7),
                UnitTestCase(result_file='result', test_file='test2', line=123, class_name='class1', test_name='test4', result='failure', message='message8', content='content8', time=8),
    
                UnitTestCase(result_file='result', test_file='test1', line=123, class_name='class1', test_name='test5', result='success', message='message9', content='content9', time=9),
                UnitTestCase(result_file='result', test_file='test2', line=123, class_name='class1', test_name='test5', result='error', message='message10', content='content10', time=10),
            ],
            commit='commit'
        )

        self.assertEqual(get_test_results(with_duplicates, False), UnitTestResults(
            files=1,
            suites=2, suite_tests=3, suite_skipped=4, suite_failures=5, suite_errors=6, suite_time=7,
            cases=10, cases_skipped=3, cases_failures=1, cases_errors=1, cases_time=55,
            case_results=UnitTestCaseResults([
                ((None, 'class1', 'test1'), dict(success=[UnitTestCase(result_file='result', test_file='test1', line=123, class_name='class1', test_name='test1', result='success', message='message1', content='content1', time=1), UnitTestCase(result_file='result', test_file='test2', line=123, class_name='class1', test_name='test1', result='success', message='message2', content='content2', time=2)])),
                ((None, 'class1', 'test2'), dict(success=[UnitTestCase(result_file='result', test_file='test1', line=123, class_name='class1', test_name='test2', result='success', message='message3', content='content3', time=3)], skipped=[UnitTestCase(result_file='result', test_file='test2', line=123, class_name='class1', test_name='test2', result='skipped', message='message4', content='content4', time=4)])),
                ((None, 'class1', 'test3'), dict(skipped=[UnitTestCase(result_file='result', test_file='test1', line=123, class_name='class1', test_name='test3', result='skipped', message='message5', content='content5', time=5), UnitTestCase(result_file='result', test_file='test2', line=123, class_name='class1', test_name='test3', result='skipped', message='message6', content='content6', time=6)])),
                ((None, 'class1', 'test4'), dict(success=[UnitTestCase(result_file='result', test_file='test1', line=123, class_name='class1', test_name='test4', result='success', message='message7', content='content7', time=7)], failure=[UnitTestCase(result_file='result', test_file='test2', line=123, class_name='class1', test_name='test4', result='failure', message='message8', content='content8', time=8)])),
                ((None, 'class1', 'test5'), dict(success=[UnitTestCase(result_file='result', test_file='test1', line=123, class_name='class1', test_name='test5', result='success', message='message9', content='content9', time=9)], error=[UnitTestCase(result_file='result', test_file='test2', line=123, class_name='class1', test_name='test5', result='error', message='message10', content='content10', time=10)])),
            ]),
            tests=5, tests_skipped=1, tests_failures=1, tests_errors=1,
            commit='commit'
        ))

        self.assertEqual(get_test_results(with_duplicates, True), UnitTestResults(
            files=1,
            suites=2, suite_tests=3, suite_skipped=4, suite_failures=5, suite_errors=6, suite_time=7,
            cases=10, cases_skipped=3, cases_failures=1, cases_errors=1, cases_time=55,
            case_results=UnitTestCaseResults([
                (('test1', 'class1', 'test1'), dict(success=[UnitTestCase(result_file='result', test_file='test1', line=123, class_name='class1', test_name='test1', result='success', message='message1', content='content1', time=1)])),
                (('test2', 'class1', 'test1'), dict(success=[UnitTestCase(result_file='result', test_file='test2', line=123, class_name='class1', test_name='test1', result='success', message='message2', content='content2', time=2)])),
                (('test1', 'class1', 'test2'), dict(success=[UnitTestCase(result_file='result', test_file='test1', line=123, class_name='class1', test_name='test2', result='success', message='message3', content='content3', time=3)])),
                (('test2', 'class1', 'test2'), dict(skipped=[UnitTestCase(result_file='result', test_file='test2', line=123, class_name='class1', test_name='test2', result='skipped', message='message4', content='content4', time=4)])),
                (('test1', 'class1', 'test3'), dict(skipped=[UnitTestCase(result_file='result', test_file='test1', line=123, class_name='class1', test_name='test3', result='skipped', message='message5', content='content5', time=5)])),
                (('test2', 'class1', 'test3'), dict(skipped=[UnitTestCase(result_file='result', test_file='test2', line=123, class_name='class1', test_name='test3', result='skipped', message='message6', content='content6', time=6)])),
                (('test1', 'class1', 'test4'), dict(success=[UnitTestCase(result_file='result', test_file='test1', line=123, class_name='class1', test_name='test4', result='success', message='message7', content='content7', time=7)])),
                (('test2', 'class1', 'test4'), dict(failure=[UnitTestCase(result_file='result', test_file='test2', line=123, class_name='class1', test_name='test4', result='failure', message='message8', content='content8', time=8)])),
                (('test1', 'class1', 'test5'), dict(success=[UnitTestCase(result_file='result', test_file='test1', line=123, class_name='class1', test_name='test5', result='success', message='message9', content='content9', time=9)])),
                (('test2', 'class1', 'test5'), dict(error=[UnitTestCase(result_file='result', test_file='test2', line=123, class_name='class1', test_name='test5', result='error', message='message10', content='content10', time=10)])),
            ]),
            tests=10, tests_skipped=3, tests_failures=1, tests_errors=1,
            commit='commit'
        ))
def main(settings: Settings) -> None:
    gha = GithubAction()

    # resolve the files_glob to files
    files = [str(file) for file in pathlib.Path().glob(settings.files_glob)]
    if len(files) == 0:
        gha.warning(f'Could not find any files for {settings.files_glob}')
    else:
        logger.info('reading {}'.format(settings.files_glob))
        logger.debug('reading {}'.format(list(files)))

    # get the unit test results
    parsed = parse_junit_xml_files(files).with_commit(settings.commit)
    [
        gha.error(message=f'Error processing result file: {error.message}',
                  file=error.file,
                  line=error.line,
                  column=error.column) for error in parsed.errors
    ]

    # process the parsed results
    results = get_test_results(parsed, settings.dedup_classes_by_file_name)

    # turn them into stats
    stats = get_stats(results)

    # derive check run conclusion from files
    conclusion = get_conclusion(parsed)

    # publish the delta stats
    gh = github.Github(login_or_token=settings.token,
                       base_url=settings.api_url)
    Publisher(settings, gh, gha).publish(stats, results.case_results,
                                         conclusion)
 def test_get_test_results(self):
     self.assertEqual(get_test_results(ParsedUnitTestResultsWithCommit(
         files=1,
         suites=2, suite_tests=3, suite_skipped=4, suite_failures=5, suite_errors=6, suite_time=7,
         cases=[
             UnitTestCase(result_file='result', test_file='test', line=123, class_name='class1', test_name='test1', result='success', message='message1', content='content1', time=1),
             UnitTestCase(result_file='result', test_file='test', line=123, class_name='class1', test_name='test2', result='skipped', message='message2', content='content2', time=2),
             UnitTestCase(result_file='result', test_file='test', line=123, class_name='class1', test_name='test3', result='failure', message='message3', content='content3', time=3),
             UnitTestCase(result_file='result', test_file='test', line=123, class_name='class2', test_name='test1', result='error', message='message4', content='content4', time=4),
             UnitTestCase(result_file='result', test_file='test', line=123, class_name='class2', test_name='test2', result='skipped', message='message5', content='content5', time=5),
             UnitTestCase(result_file='result', test_file='test', line=123, class_name='class2', test_name='test3', result='failure', message='message6', content='content6', time=6),
             UnitTestCase(result_file='result', test_file='test', line=123, class_name='class2', test_name='test4', result='failure', message='message7', content='content7', time=7),
         ],
         commit='commit'
     ), False), UnitTestResults(
         files=1,
         suites=2, suite_tests=3, suite_skipped=4, suite_failures=5, suite_errors=6, suite_time=7,
         cases=7, cases_skipped=2, cases_failures=3, cases_errors=1, cases_time=28,
         case_results=UnitTestCaseResults([
             ((None, 'class1', 'test1'), dict(success=[UnitTestCase(result_file='result', test_file='test', line=123, class_name='class1', test_name='test1', result='success', message='message1', content='content1', time=1)])),
             ((None, 'class1', 'test2'), dict(skipped=[UnitTestCase(result_file='result', test_file='test', line=123, class_name='class1', test_name='test2', result='skipped', message='message2', content='content2', time=2)])),
             ((None, 'class1', 'test3'), dict(failure=[UnitTestCase(result_file='result', test_file='test', line=123, class_name='class1', test_name='test3', result='failure', message='message3', content='content3', time=3)])),
             ((None, 'class2', 'test1'), dict(error=[UnitTestCase(result_file='result', test_file='test', line=123, class_name='class2', test_name='test1', result='error', message='message4', content='content4', time=4)])),
             ((None, 'class2', 'test2'), dict(skipped=[UnitTestCase(result_file='result', test_file='test', line=123, class_name='class2', test_name='test2', result='skipped', message='message5', content='content5', time=5)])),
             ((None, 'class2', 'test3'), dict(failure=[UnitTestCase(result_file='result', test_file='test', line=123, class_name='class2', test_name='test3', result='failure', message='message6', content='content6', time=6)])),
             ((None, 'class2', 'test4'), dict(failure=[UnitTestCase(result_file='result', test_file='test', line=123, class_name='class2', test_name='test4', result='failure', message='message7', content='content7', time=7)])),
         ]),
         tests=7, tests_skipped=2, tests_failures=3, tests_errors=1,
         commit='commit'
     ))
 def test_get_test_results_with_empty_cases(self):
     self.assertEqual(get_test_results(ParsedUnitTestResultsWithCommit(
         files=0,
         suites=0, suite_tests=0, suite_skipped=0, suite_failures=0, suite_errors=0, suite_time=0,
         cases=[],
         commit='commit'
     ), False), UnitTestResults(
         files=0,
         suites=0, suite_tests=0, suite_skipped=0, suite_failures=0, suite_errors=0, suite_time=0,
         cases=0, cases_skipped=0, cases_failures=0, cases_errors=0, cases_time=0, case_results=UnitTestCaseResults(),
         tests=0, tests_skipped=0, tests_failures=0, tests_errors=0,
         commit='commit'
     ))
Пример #5
0
def main(settings: Settings) -> None:
    gha = GithubAction()

    # we cannot create a check run or pull request comment
    # when running on pull_request event from a fork
    if settings.event_name == 'pull_request' and \
            settings.event.get('pull_request', {}).get('head', {}).get('repo', {}).get('full_name') != settings.repo:
        gha.warning(
            f'This action is running on a pull_request event for a fork repository. '
            f'It cannot do anything useful like creating check runs or pull request comments.'
        )
        return

    # resolve the files_glob to files
    files = [str(file) for file in pathlib.Path().glob(settings.files_glob)]
    if len(files) == 0:
        gha.warning(f'Could not find any files for {settings.files_glob}')
    else:
        logger.info(f'reading {settings.files_glob}')
        logger.debug(f'reading {list(files)}')

    # get the unit test results
    parsed = parse_junit_xml_files(files).with_commit(settings.commit)
    [
        gha.error(message=f'Error processing result file: {error.message}',
                  file=error.file,
                  line=error.line,
                  column=error.column) for error in parsed.errors
    ]

    # process the parsed results
    results = get_test_results(parsed, settings.dedup_classes_by_file_name)

    # turn them into stats
    stats = get_stats(results)

    # derive check run conclusion from files
    conclusion = get_conclusion(parsed,
                                fail_on_failures=settings.fail_on_failures,
                                fail_on_errors=settings.fail_on_errors)

    # publish the delta stats
    retry = Retry(total=10, backoff_factor=1)
    gh = github.Github(login_or_token=settings.token,
                       base_url=settings.api_url,
                       retry=retry)
    Publisher(settings, gh, gha).publish(stats, results.case_results,
                                         conclusion)
def main(settings: Settings) -> None:
    files = [str(file) for file in pathlib.Path().glob(settings.files_glob)]
    logger.info('reading {}: {}'.format(settings.files_glob, list(files)))

    # get the unit test results
    parsed = parse_junit_xml_files(files).with_commit(settings.commit)

    # process the parsed results
    results = get_test_results(parsed, settings.dedup_classes_by_file_name)

    # turn them into stats
    stats = get_stats(results)

    # publish the delta stats
    gh = Github(settings.token)
    Publisher(settings, gh).publish(stats, results.case_results)
 def test_get_test_results_with_some_nones(self):
     self.assertEqual(get_test_results(ParsedUnitTestResultsWithCommit(
         files=1,
         suites=2, suite_tests=3, suite_skipped=4, suite_failures=5, suite_errors=6, suite_time=7,
         cases=[
             UnitTestCase(result_file='result', test_file=None, line=None, class_name='class', test_name='test1', result='success', message='message1', content='content1', time=1),
             UnitTestCase(result_file='result', test_file=None, line=None, class_name='class', test_name='test1', result='skipped', message='message2', content='content2', time=None),
             UnitTestCase(result_file='result', test_file=None, line=None, class_name='class', test_name='test2', result='failure', message='message3', content='content3', time=2),
             UnitTestCase(result_file='result', test_file=None, line=None, class_name='class', test_name='test2', result='skipped', message='message4', content='content4', time=None),
         ],
         commit='commit'
     ), False), UnitTestResults(
         files=1,
         suites=2, suite_tests=3, suite_skipped=4, suite_failures=5, suite_errors=6, suite_time=7,
         cases=4, cases_skipped=2, cases_failures=1, cases_errors=0, cases_time=3,
         case_results=UnitTestCaseResults([
             ((None, 'class', 'test1'), dict(success=[UnitTestCase(result_file='result', test_file=None, line=None, class_name='class', test_name='test1', result='success', message='message1', content='content1', time=1)], skipped=[UnitTestCase(result_file='result', test_file=None, line=None, class_name='class', test_name='test1', result='skipped', message='message2', content='content2', time=None)])),
             ((None, 'class', 'test2'), dict(failure=[UnitTestCase(result_file='result', test_file=None, line=None, class_name='class', test_name='test2', result='failure', message='message3', content='content3', time=2)], skipped=[UnitTestCase(result_file='result', test_file=None, line=None, class_name='class', test_name='test2', result='skipped', message='message4', content='content4', time=None)])),
         ]),
         tests=2, tests_skipped=0, tests_failures=1, tests_errors=0,
         commit='commit'
     ))