Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation: show how to rerun failed tests #2578

Open
ericdriggs opened this issue Jul 5, 2024 · 9 comments
Open

Documentation: show how to rerun failed tests #2578

ericdriggs opened this issue Jul 5, 2024 · 9 comments

Comments

@ericdriggs
Copy link

ericdriggs commented Jul 5, 2024

Purpose: documentation for rerunning failed tests

Use case: large suites of api tests with downstream service dependencies on other services have a high probability of failure. Option to rerun failed tests can make test management orders of magnitude easier and can be useful for distinguishing between logical errors and race conditions.

see:

Code examples

@Test
    void testParallel() {
        Results results = Runner.path("classpath:demo")
                .outputCucumberJson(true)
                .karateEnv("demo")
                .parallel(5);
        results = rerunFailedTests(results,3);
        generateReport(results.getReportDir());
        assertTrue(results.getFailCount() == 0, results.getErrorMessages());
    }
 //note: requires log variable
 @SuppressWarnings("SameParameterValue")
    public static Results rerunFailedTests(Results results, int rerunCount) {

        for (int i = 1; i <= rerunCount; i++) {

            //if no failures, no need to retry
            List<ScenarioResult> failed = results.getScenarioResults().filter(ScenarioResult::isFailed).collect(Collectors.toList());
            if (failed.isEmpty()) {
                return results;
            }

            final Suite suite = results.getSuite();
            final int attempt = i;
            log.info("{} failed tests. retry attempt: {}", failed.size(), attempt);

            Set<ScenarioResult> retriedResults = new ConcurrentSkipListSet<>();

            //parallel retries
            failed.parallelStream().forEach(scenarioResult -> {
                Scenario scenario = scenarioResult.getScenario();
                log.info("retrying scenario: {}, attempt: {}", scenario.getName(), attempt);
                ScenarioResult sr = suite.retryScenario(scenario);
                retriedResults.add(sr);
            });

            //update results synchronously in case of race conditions
            for (ScenarioResult sr : retriedResults) {
                results = results.getSuite().updateResults(sr);
            }
        }
        //return after max rerunCount attempts
        return results;
    }
@sadiqkassamali
Copy link

peter has already mentioned it here
https://github.com/karatelabs/karate/blob/develop/karate-core/src/test/java/com/intuit/karate/core/retry/RetryTest.java#L35

i++ will make it continues loop without break; ideally you would want to run it once

@ericdriggs
Copy link
Author

@sadiqkassamali
The example in the code shows a single retry for a single test.
The example I provided is a drop-in function which provides multiple retries with parallel execution.

@sadiqkassamali
Copy link

i am doing this this way.
List failed = results.getScenarioResults().filter(sr -> sr.isFailed())
.collect(Collectors.toList());

        try {
            boolean rerun = true;
            while(rerun)
                if (!failed.isEmpty()) {

                log.info("Fail size: + " + failed.size());
                log.info("May have found failures for Rerun..checking");
                for (int i = 0; i < failed.size(); i++) {
                    log.info("Total number of test case that failed, which will be rerun: " + failed.size());
                    Scenario scenario = failed.get(i).getScenario();
                    ScenarioResult sr = results.getSuite().retryScenario(scenario);
                    results = results.getSuite().updateResults(sr);
                    collector.checkThat(results.getFailCount(), equalTo(0));
                    rerun = false;
                    break;
                }

            } else {
                log.info("Fail size: + " + failed.size());
                log.info("Wow No Failures in the first run");
                collector.checkThat(results.getFailCount(), equalTo(0));
                rerun = false;
                break;
            }
        } catch (Exception e) {
            log.info("Error occurred Sending out email" + e.getMessage());

        }

@ericdriggs
Copy link
Author

@sadiqkassamali

  1. possible infinite loop
  2. single threaded

@sadiqkassamali
Copy link

thanks for letting me know appreciate it, i stand corrected

@sturose
Copy link

sturose commented Sep 13, 2024

@ericdriggs I would probably do:

    // parallel retries
    List<ScenarioResult> retriedResults =
        failed.parallelStream()
            .map(
                scenarioResult -> {
                  Scenario scenario = scenarioResult.getScenario();
                  log.info("retrying scenario: {}, attempt", scenario.getName());
                  return suite.retryScenario(scenario);
                })
            .toList();

@ericdriggs
Copy link
Author

@sturose If you want a single retry

@sturose
Copy link

sturose commented Sep 13, 2024

I'm not sure I follow. I'm just saying that instead of doing

            Set<ScenarioResult> retriedResults = new ConcurrentSkipListSet<>();

            //parallel retries
            failed.parallelStream().forEach(scenarioResult -> {
                Scenario scenario = scenarioResult.getScenario();
                log.info("retrying scenario: {}, attempt: {}", scenario.getName(), attempt);
                ScenarioResult sr = suite.retryScenario(scenario);
                retriedResults.add(sr);
            });

it reads a bit cleaner to do

    // parallel retries
    List<ScenarioResult> retriedResults =
        failed.parallelStream()
            .map(
                scenarioResult -> {
                  Scenario scenario = scenarioResult.getScenario();
                  log.info("retrying scenario: {}, attempt", scenario.getName());
                  return suite.retryScenario(scenario);
                })
            .toList();

They are functionality equivalent.

@ptrthomas
Copy link
Member

tagging @bischoffdev since he maintains https://github.com/trivago/cluecumber - that supports generating reports for karate runs. I saw a recent update that improves support for when tests are re-tried - and perhaps Benjamin you have some experience with re-trying failed tests in karate ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants