Class Karate

java.lang.Object
io.karatelabs.junit6.Karate
All Implemented Interfaces:
Iterable<org.junit.jupiter.api.DynamicNode>

public class Karate extends Object implements Iterable<org.junit.jupiter.api.DynamicNode>
Fluent API for running Karate tests with JUnit 6.

Provides streaming dynamic test generation where tests appear in the JUnit tree as scenarios execute, enabling true late-binding test discovery for features that generate scenarios dynamically.

Example usage with @TestFactory:

 class ApiTests {
     @TestFactory
     Stream<DynamicNode> karateTests() {
         return Karate.run("classpath:features/")
             .tags("@smoke")
             .threads(4)
             .stream();
     }
 }
 

Or using the convenience annotation (note: return type must be Iterable<DynamicNode> for JUnit 6 static validation):

 class ApiTests {
     @Karate.Test
     Iterable<DynamicNode> testAll() {
         return Karate.run("sample").relativeTo(getClass());
     }
 }
 
  • Method Details

    • run

      public static Karate run(String... paths)
      Creates a new Karate instance configured to run the specified paths.
      Parameters:
      paths - feature file or directory paths
      Returns:
      a new Karate instance
    • relativeTo

      public Karate relativeTo(Class<?> clazz)
      Set paths relative to the given class.

      This enables discovering feature files in the same package as the test class. If no paths were specified, all .feature files in the class's package will be discovered.

      Parameters:
      clazz - the class to resolve paths relative to
      Returns:
      this instance for chaining
    • tags

      public Karate tags(String... tagExpressions)
      Set tag filter expressions.

      Examples: "@smoke", "~@slow", "@smoke,@fast"

      Parameters:
      tagExpressions - tag expressions
      Returns:
      this instance for chaining
    • karateEnv

      public Karate karateEnv(String env)
      Set the karate environment (karate.env).
      Parameters:
      env - environment name
      Returns:
      this instance for chaining
    • threads

      public Karate threads(int count)
      Set the number of threads for parallel execution.
      Parameters:
      count - thread count (1 for sequential)
      Returns:
      this instance for chaining
    • hierarchical

      public Karate hierarchical(boolean enabled)
      Enable or disable hierarchical test structure.

      When enabled (default), features appear as containers with scenarios as children. When disabled, all scenarios appear at the root level.

      Parameters:
      enabled - true for hierarchical structure
      Returns:
      this instance for chaining
    • timeoutMinutes

      public Karate timeoutMinutes(long minutes)
      Set the timeout for waiting for test events.
      Parameters:
      minutes - timeout in minutes
      Returns:
      this instance for chaining
    • systemProperty

      public Karate systemProperty(String key, String value)
      Set a system property that will be available via karate.properties in scripts.
      Parameters:
      key - property key
      value - property value
      Returns:
      this instance for chaining
    • configDir

      public Karate configDir(String dir)
      Set the directory containing karate-config.js.
      Parameters:
      dir - config directory path
      Returns:
      this instance for chaining
    • outputDir

      public Karate outputDir(String dir)
      Set the output directory for reports.
      Parameters:
      dir - output directory path
      Returns:
      this instance for chaining
    • outputHtmlReport

      public Karate outputHtmlReport(boolean enabled)
      Enable or disable HTML report generation.
      Parameters:
      enabled - true to generate HTML reports
      Returns:
      this instance for chaining
    • outputJunitXml

      public Karate outputJunitXml(boolean enabled)
      Enable or disable JUnit XML report generation.
      Parameters:
      enabled - true to generate JUnit XML reports
      Returns:
      this instance for chaining
    • outputCucumberJson

      public Karate outputCucumberJson(boolean enabled)
      Enable or disable Cucumber JSON report generation.
      Parameters:
      enabled - true to generate Cucumber JSON reports
      Returns:
      this instance for chaining
    • scenarioName

      public Karate scenarioName(String name)
      Filter by scenario name (regex supported).
      Parameters:
      name - scenario name pattern
      Returns:
      this instance for chaining
    • dryRun

      public Karate dryRun(boolean enabled)
      Enable dry run mode (parse without executing).
      Parameters:
      enabled - true for dry run
      Returns:
      this instance for chaining
    • httpClientFactory

      public Karate httpClientFactory(io.karatelabs.http.HttpClientFactory factory)
      Set a custom HTTP client factory.
      Parameters:
      factory - the HTTP client factory
      Returns:
      this instance for chaining
    • stream

      public Stream<org.junit.jupiter.api.DynamicNode> stream()
      Returns a stream of dynamic test nodes for use with @TestFactory.

      This method starts Karate execution asynchronously and returns a stream that yields tests as scenarios complete. The stream blocks while waiting for the next scenario to finish.

      Returns:
      a stream of dynamic test nodes
    • iterator

      public Iterator<org.junit.jupiter.api.DynamicNode> iterator()
      Implements Iterable for use with the Karate.Test annotation.

      This allows the Karate instance to be returned directly from a method annotated with @Karate.Test, and JUnit will iterate over the tests.

      Specified by:
      iterator in interface Iterable<org.junit.jupiter.api.DynamicNode>
    • toString

      public String toString()
      Overrides:
      toString in class Object