RUNNING TESTS
JUnit Integration
Overview
Karate supports JUnit 5 with advantages including multiple test methods per class, a single import, and a fluent API for expressing test configurations. Classes and methods don't need to be public, making tests concise.
Basic JUnit 5 Setup
Simple Test Class
package karate;
import com.intuit.karate.junit5.Karate;
class SampleTest {
@Karate.Test
Karate testSample() {
return Karate.run("sample").relativeTo(getClass());
}
@Karate.Test
Karate testTags() {
return Karate.run("tags").tags("@second").relativeTo(getClass());
}
@Karate.Test
Karate testSystemProperty() {
return Karate.run("classpath:karate/tags.feature")
.tags("@second")
.karateEnv("e2e")
.systemProperty("foo", "bar");
}
}
Maven Surefire Plugin Configuration
To run JUnit 5 tests from command-line, ensure the latest maven-surefire-plugin
is in your pom.xml
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
Running Single Test Methods
Run a specific test method:
mvn test -Dtest=SampleTest#testTags
Feature File Discovery
Karate traverses sub-directories automatically to find *.feature
files. If your JUnit class is in the com.mycompany
package, feature files in com.mycompany.foo
and com.mycompany.bar
will also be executed.
This behavior supports flat directory structures as recommended in naming conventions.
HTML Reports
After each feature execution, an HTML report is generated in the target/karate-reports
folder:
html report: (paste into browser to view)
-----------------------------------------
file:///projects/myproject/target/karate-reports/mypackage.myfeature.html
This report includes:
- All requests and responses inline with steps
- Error messages and
print
statement output - Useful for troubleshooting and debugging
- Refreshes automatically when tests are re-run
Dry Run Mode
Generate a preview report showing which features will run without actual execution:
// In your Runner.Builder configuration
.dryRun()
Or via command-line using karate.options
:
-Dkarate.options="--dryrun"
Dry Run Benefits
- Review tag "coverage" of test execution
- Generate feature "coverage" reports with rich tag sets
- Validate test selection before actual runs
- All steps shown (including comments)
- Everything appears as "passed" with no actual execution
Advanced Configuration
Builder methods from Runner.Builder
are available for additional configuration:
reportDir()
- Custom report directorytags()
- Tag-based test filteringkarateEnv()
- Environment configurationsystemProperty()
- System property injection
IDE Integration
Right-click and run individual test methods directly in your IDE during development. This provides quick feedback for single feature testing without full test suite execution.
Next Steps
- Learn about Command Line Execution for CI/CD integration
- Explore Parallel Execution for faster test runs
- Understand Test Tags for flexible test organization