1

is there such thing as a Test Suite Setup/Teardown in Karate API? Basically, I want to do something before everything starts and after everything is finished.

jezztify
  • 75
  • 8

2 Answers2

1

If you are using Java / JUnit as the entry point this is quite simple, just add lines of code before / after.

Also refer this answer: https://stackoverflow.com/a/59080128/143475 - the ExecutionHook (which still does require you to write Java code) has beforeAll() and afterAll() callbacks.

In practice it is probably simplest to use a callSingle() in your karate-config.js and do a pre-cleanup.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • I tried using the `ExecutionHook` but one thing I noticed is that it doesn't allow me to use the `public static Results parallel(Class> clazz, int threadCount)` format for the parallel execution and it gives me errors when generating the cucumber test report – jezztify Sep 18 '20 at 07:45
-1

Fixed via below code.

<...truncated imports...>
@KarateOptions()
public class TestRunner {
  @Test
  public void testParallel() {
    Results results = Runner.path("classpath:THISCLASS").hook(new ExecHook()).parallel(1);
    generateReport(results.getReportDir());
    assertTrue(results.getErrorMessages(), results.getFailCount() == 0);
  }
}

class ExecHook implements ExecutionHook {
  @Override
  public void afterAll(Results results) {
    System.out.println("DO SOMETHING HERE");
  }  

  @Override
  public boolean beforeScenario(Scenario scenario, ScenarioContext context) {
    return true;
  }

  @Override
  public void afterScenario(ScenarioResult result, ScenarioContext context) {

  }    

  @Override
  public boolean beforeFeature(Feature feature, ExecutionContext context) {
    return true;
  }

  @Override
  public void afterFeature(FeatureResult result, ExecutionContext context) {
      
  }    

  @Override
  public void beforeAll(Results results) {

  }     

  @Override
  public boolean beforeStep(Step step, ScenarioContext context) {
    return true;
  }

  @Override
  public void afterStep(StepResult result, ScenarioContext context) {

  }        
      
  @Override
  public String getPerfEventName(HttpRequestBuilder req, ScenarioContext context) {
    return null;
  }    
  
  @Override
  public void reportPerfEvent(PerfEvent event) {
      
  }
  
}

Just one question, is there a way to to take off the @Overrides for the methods I'm not using?

jezztify
  • 75
  • 8