Testing / Karate Framework Interview questions
How do you configure environment-specific settings using karate.env?
karate.env is a system property that karate-config.js reads to decide which environment's configuration values (base URLs, credentials, feature flags) to return, letting the same test suite target different environments without editing any feature files.
// karate-config.js function fn() { var env = karate.env || 'dev'; karate.log('running with env:', env); var config = { dev: { baseUrl: 'https://dev-api.example.com' }, staging: { baseUrl: 'https://staging-api.example.com' }, prod: { baseUrl: 'https://api.example.com' } }[env]; if (!config) { karate.log('unknown env, defaulting to dev config'); config = { baseUrl: 'https://dev-api.example.com' }; } return config; }
# running against staging mvn test -Dkarate.env=staging
Because the environment is passed in externally (as a system property, typically from the CI pipeline's build configuration) rather than hard-coded into the test files themselves, the exact same suite can validate dev, staging, and production without any code changes, just a different value passed at execution time.
More Related questions...