karatelabs/karate · error · AssertionError
assert failed:
Error message
assert failed:
What it means
Karate's 'assert' step evaluates its text as an expression; if the resulting boolean is false, an AssertionError labeled 'assert failed: <expression>' is thrown. This is an intentional assertion failure, not a bug — the JS expression evaluated to false.
Solutions
- Print the values involved (print responseStatus, myVar) to see actual vs expected
- Fix the expectation if the product behavior is correct, or fix the underlying bug/fixture if not
- Watch for type mismatches — compare with explicit conversions when values come from JSON strings
- Ensure referenced variables are defined earlier (undefined comparisons often evaluate false)
Example fix
// before: fails when status is 201 assert responseStatus == 200 // after: accept both success codes assert responseStatus == 200 || responseStatus == 201
Defensive patterns
Strategy: validation
Validate before calling
// log operands before asserting on them
print('responseStatus:', responseStatus, 'expected:', 200);
// assert responseStatus == 200 Try / catch
// AssertionError is intentional; let it propagate and read the labeled expression
try { executor.execute(step); } catch (AssertionError e) { report.stepFailed(step, e.getMessage()); throw e; } Prevention
- print the compared values before asserting
- Beware string vs number comparisons on JSON values
- Ensure asserted variables are defined earlier in the scenario
- Match actual behavior against specs before 'fixing' the assert
When it happens
Trigger: An 'assert' step in a feature whose expression returns false, e.g. 'assert responseStatus == 200' after a non-200 response, or 'assert myVar.length == 3' with a different length.
Common situations: API regression caught by the test (wrong status code, wrong computed value); type coercion surprises making the expression false (string '5' vs number 5); evaluating against undefined variables which yield false-y comparisons.
Related errors
- assert expression must return boolean:
- append() needs at least two arguments
- appendTo() needs at least two arguments: list and item(s)
- call with tag selector requires a feature context
- called feature failed
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/aab76cebc928d7e5.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/core/StepExecutor.java:1902
return value;
}
/**
* True for an expression like {@code ({ a: 1 })} or {@code ([1, 2])} — a JSON literal
* the user forced through JS evaluation by wrapping it in round brackets.
*/
private static boolean isParenWrappedJson(String expr) {
if (expr.length() < 3 || expr.charAt(0) != '(' || expr.charAt(expr.length() - 1) != ')') {
return false;
}
return StringUtils.looksLikeJson(expr.substring(1, expr.length() - 1).trim());
}
private void executeAssert(Step step) {
Object result = runtime.eval(step.getText());
if (result instanceof Boolean b) {
if (!b) {
throw new AssertionError(withCommentLabel(step, "assert failed: " + step.getText()));
}
} else {
throw new RuntimeException("assert expression must return boolean: " + step.getText());
}
}
private static final LogContext.LogWriter SCENARIO_LOG = LogContext.with(LogContext.SCENARIO_LOGGER);
private void executePrint(Step step) {
// Wrap in array to handle comma-separated expressions like: print 'foo', 'bar'
// Without wrapping, JS comma operator would return only the last value
Object value = runtime.eval("[" + step.getText() + "]");
if (value instanceof List<?> list) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
if (i > 0) {
sb.append(' ');
}View on GitHub (pinned to a22eb90246)