Class Karate
- All Implemented Interfaces:
Iterable<org.junit.jupiter.api.DynamicNode>
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());
}
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic @interfaceMarks a method as a Karate test factory. -
Method Summary
Modifier and TypeMethodDescriptionSet the directory containing karate-config.js.dryRun(boolean enabled) Enable dry run mode (parse without executing).hierarchical(boolean enabled) Enable or disable hierarchical test structure.httpClientFactory(io.karatelabs.http.HttpClientFactory factory) Set a custom HTTP client factory.Iterator<org.junit.jupiter.api.DynamicNode> iterator()ImplementsIterablefor use with theKarate.Testannotation.Set the karate environment (karate.env).outputCucumberJson(boolean enabled) Enable or disable Cucumber JSON report generation.Set the output directory for reports.outputHtmlReport(boolean enabled) Enable or disable HTML report generation.outputJunitXml(boolean enabled) Enable or disable JUnit XML report generation.relativeTo(Class<?> clazz) Set paths relative to the given class.static KarateCreates a new Karate instance configured to run the specified paths.scenarioName(String name) Filter by scenario name (regex supported).Stream<org.junit.jupiter.api.DynamicNode> stream()Returns a stream of dynamic test nodes for use with@TestFactory.systemProperty(String key, String value) Set a system property that will be available via karate.properties in scripts.Set tag filter expressions.threads(int count) Set the number of threads for parallel execution.timeoutMinutes(long minutes) Set the timeout for waiting for test events.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
run
Creates a new Karate instance configured to run the specified paths.- Parameters:
paths- feature file or directory paths- Returns:
- a new Karate instance
-
relativeTo
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
Set tag filter expressions.Examples: "@smoke", "~@slow", "@smoke,@fast"
- Parameters:
tagExpressions- tag expressions- Returns:
- this instance for chaining
-
karateEnv
Set the karate environment (karate.env).- Parameters:
env- environment name- Returns:
- this instance for chaining
-
threads
Set the number of threads for parallel execution.- Parameters:
count- thread count (1 for sequential)- Returns:
- this instance for chaining
-
hierarchical
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
Set the timeout for waiting for test events.- Parameters:
minutes- timeout in minutes- Returns:
- this instance for chaining
-
systemProperty
Set a system property that will be available via karate.properties in scripts.- Parameters:
key- property keyvalue- property value- Returns:
- this instance for chaining
-
configDir
Set the directory containing karate-config.js.- Parameters:
dir- config directory path- Returns:
- this instance for chaining
-
outputDir
Set the output directory for reports.- Parameters:
dir- output directory path- Returns:
- this instance for chaining
-
outputHtmlReport
Enable or disable HTML report generation.- Parameters:
enabled- true to generate HTML reports- Returns:
- this instance for chaining
-
outputJunitXml
Enable or disable JUnit XML report generation.- Parameters:
enabled- true to generate JUnit XML reports- Returns:
- this instance for chaining
-
outputCucumberJson
Enable or disable Cucumber JSON report generation.- Parameters:
enabled- true to generate Cucumber JSON reports- Returns:
- this instance for chaining
-
scenarioName
Filter by scenario name (regex supported).- Parameters:
name- scenario name pattern- Returns:
- this instance for chaining
-
dryRun
Enable dry run mode (parse without executing).- Parameters:
enabled- true for dry run- Returns:
- this instance for chaining
-
httpClientFactory
Set a custom HTTP client factory.- Parameters:
factory- the HTTP client factory- Returns:
- this instance for chaining
-
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
ImplementsIterablefor use with theKarate.Testannotation.This allows the Karate instance to be returned directly from a method annotated with
@Karate.Test, and JUnit will iterate over the tests. -
toString
-