Karate cache persistence across scenarios and its impact on test reliability
0 reputation · 15 Feb 2020, 21:42 UTC
0 reputation · 15 Feb 2020, 21:42 UTC
When Karate’s built‑in response cache is enabled with karate.configure('cache', true), identical requests return the stored response without contacting the server. The goal is to use this cache to speed up test execution while guaranteeing that each scenario sees the current state of the backend, even when the service modifies data during a suite run.
Because the cache is not automatically cleared between scenarios or features, there is uncertainty about whether stale responses can leak from one scenario to the next, potentially masking backend changes. The documentation does not define a cache‑invalidation policy, and behavior may vary between releases such as 0.9.x and 1.4.x, making it unclear what manual steps are required to isolate the cache per scenario without sacrificing the performance benefit.
Does Karate automatically clear the response cache between scenarios? If not, what is the recommended way to reset the cache for each scenario while keeping caching enabled? How does cache persistence across scenario boundaries differ between Karate 0.9.x and 1.4.x?
Karate does not automatically clear the response cache between scenarios. The cache lives for the entire JVM process, so a response stored in one scenario will be returned in later scenarios unless you explicitly reset it.
cacheHeaders), and request body.karate.configure('cache', true) makes it global to the test run.There is no programmatic clear() API. The only reliable way to flush the cache is to toggle the setting off and back on:
karate.configure('cache', false);
karate.configure('cache', true);
Place these two lines in a Background section (or a callonce feature) so they execute before every scenario.
A direct clear method was added. The exact name may vary by minor release (karate.cache.clear() or karate.getCache().clear()). Use the one that exists in your version:
// Preferred in 1.4.x
karate.cache.clear();
Again, call this in a Background step or a @BeforeScenario hook to isolate each scenario while keeping caching enabled for repeated calls within the same scenario (e.g., Scenario Outline iterations).
mvn dependency:tree or Gradle equivalent).Background.karate.cache.clear() (or karate.getCache().clear()) to Background or a @BeforeScenario hook.karate.configure('cacheHeaders', ['Authorization']);
-Dkarate.log.level=DEBUG – you will see “cache hit” / “cache miss” lines.karate.parallel()) shares the same cache across threads. Resetting in one thread clears it for all threads, which can cause race conditions. For parallel suites consider disabling the cache entirely or accepting the shared‑cache behavior.karate.cache.clear() API may be absent in early 1.x releases (e.g., 1.0.0‑1.2.0). Verify the method name against the CHANGELOG for your exact minor version.Missing diagnostic detail: Are you running scenarios in parallel with karate.parallel()? If yes, the recommendation changes to disabling the cache for the parallel run.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.