RUNNING TESTS
Command Line
Execute Karate tests directly from the command line for rapid development cycles and CI automation. Target specific tests, environments, and scenarios with simple commands.
Maven Execution
Run tests using Maven's familiar command patterns with powerful filtering options.
# Run all tests (follows *Test.java naming convention)
mvn test
# Run specific JUnit test class
mvn test -Dtest=UserTest
# Run specific test method
mvn test -Dtest=UserTest#smokeTests
# Run tests matching pattern
mvn test -Dtest="*IntegrationTest"
Environment Configuration
Switch environments and pass system properties for different test contexts:
# Set Karate environment to QA
mvn test -Dkarate.env=qa
# Combine test selection with environment and custom properties
mvn test -Dtest=ApiTest -Dkarate.env=staging -Dapi.timeout=30000
Gradle Execution
Configure Gradle to pass Karate options to the test runtime:
test {
// Pass karate options to runtime
systemProperty "karate.options", System.properties.getProperty("karate.options")
systemProperty "karate.env", System.properties.getProperty("karate.env")
// Ensure tests always run
outputs.upToDateWhen { false }
}
# Run all tests
./gradlew test
# Run specific test class
./gradlew test --tests "*UserTest"
# Alternative Gradle syntax for single test
./gradlew test -Dtest.single=UserTest
# Run with QA environment
./gradlew test -Dkarate.env=qa
Using karate.options
The karate.options
system property provides precise control over test execution:
Feature and Scenario Selection
# Run specific feature file
mvn test -Dtest=Runner -Dkarate.options="classpath:features/users.feature"
# Run specific scenario by line number
mvn test -Dtest=Runner -Dkarate.options="features/users.feature:25"
# Run multiple features
mvn test -Dtest=Runner -Dkarate.options="features/users.feature features/products.feature"
Tag-Based Filtering
# Run tests tagged with @smoke
mvn test -Dtest=Runner -Dkarate.options="--tags @smoke"
# Exclude tests tagged with @slow
mvn test -Dtest=Runner -Dkarate.options="--tags ~@slow"
# Multiple tags with equals syntax
mvn test -Dkarate.options="-t=@dev -t=@api" -Dtest=Runner
# Run tests with either tag (OR operation)
mvn test -Dtest=Runner -Dkarate.options="--tags @regression,@api"
Development Mode Options
# Validate test syntax without execution
mvn test -Dtest=Runner -Dkarate.options="--dryrun --tags @smoke"
# Run all features in a directory
mvn test -Dtest=Runner -Dkarate.options="classpath:features/debug"
Test Suites Organization
Traditional JUnit Test Suites
Create runner classes to organize related feature files:
src/test/java/
├── animals/
│ ├── cats/cats.feature
│ ├── dogs/dogs.feature
│ └── AnimalsTest.java # Runs all features in animals/
└── ApiTestSuite.java # Runs all project features
// AnimalsTest.java - Traditional JUnit runner
import com.intuit.karate.junit5.Karate;
class AnimalsTest {
@Karate.Test
Karate testAnimals() {
return Karate.run("classpath:animals").relativeTo(getClass());
}
@Karate.Test
Karate testSmokeOnly() {
return Karate.run("classpath:animals")
.tags("@smoke")
.relativeTo(getClass());
}
}
Running Test Suites
# Run all tests in the AnimalsTest suite
mvn test -Dtest=AnimalsTest
# Run suite excluding @skipme tagged tests
mvn test "-Dkarate.options=--tags ~@skipme" -Dtest=AnimalsTest
Maven Surefire Configuration
Lock test execution to specific suites:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M9</version>
<configuration>
<includes>
<include>animals/AnimalsTest.java</include>
</includes>
<systemProperties>
<karate.options>--tags @smoke</karate.options>
</systemProperties>
</configuration>
</plugin>
Traditional JUnit test suites run sequentially. For parallel execution, use the Parallel Runner.
Environment Switching
Pass the karate.env
system property to switch between environments:
# Run tests against development environment
mvn test -Dkarate.env=dev
# Run Gradle tests against staging
./gradlew test -Dkarate.env=staging
# Alternative Maven syntax for environment
mvn test -DargLine="-Dkarate.env=production"
The karate.env
value is available in your karate-config.js
for environment-specific configuration.
Quick Development Patterns
Rapid Test Iteration
# Run single test method for debugging
mvn test -Dtest=ApiTest#debugScenario
# Run problematic scenario by line number
mvn test -Dtest=Runner -Dkarate.options="features/problem.feature:15"
# Run work-in-progress tests locally
mvn test -Dtest=Runner -Dkarate.options="--tags @wip" -Dkarate.env=local
Multi-Environment Testing
# Run smoke tests against QA environment
mvn test -Dtest=SmokeTest -Dkarate.env=qa -Dkarate.options="--tags @smoke"
# Run critical tests against staging
mvn test -Dtest=Runner -Dkarate.env=staging -Dkarate.options="--tags @critical"
Common Gotchas
- Test Discovery: Maven only runs classes following
*Test.java
naming convention by default - karate.options Ignored: Ensure Gradle passes system properties to test runtime
- @ignore Tag: Tests tagged with
@ignore
are automatically skipped - Path Syntax: Feature paths should use
classpath:
prefix for consistency
Next Steps
Master command line execution, then explore:
- Parallel Execution - Scale tests across multiple threads
- Tags - Advanced test organization and filtering
- Test Reports - Generate and analyze test results