karatelabs/karate · warning

[WARN] Failed to evaluate scenario name

Error message

[WARN] Failed to evaluate scenario name '{}': {}

What it means

Karate logs this warning when a dynamic scenario name expression (from `Scenario:` or a `name` config) fails to evaluate via the Karate engine. The scenario keeps its original (unevaluated) name and execution continues; the warning is also appended to the last step's log for report visibility. It is non-fatal by design.

Solutions

  1. Check the exception message after the colon to see which variable/expression failed
  2. Ensure all variables referenced in the scenario name are defined before name evaluation (e.g. via Background or bootstrap fixtures)
  3. Simplify the name expression to plain text plus safe concatenation
  4. Wrap risky lookups with a default, e.g. ` #(myVar || 'unknown') ` pattern

Example fix

// before
Scenario: Test user #(user.name.first)
// after
Scenario: Test user #(user && user.name && user.name.first || 'unknown')
Defensive patterns

Strategy: validation

Validate before calling

// Before the scenario runs, ensure name-expression variables exist:
// in Background or karate-config.js
if (!karate.get('user')) { karate.set('user', { name: { first: 'unknown' } }); }

Type guard

function safeStr(v) { return (v === null || v === undefined) ? 'unknown' : String(v); }

Prevention

When it happens

Trigger: A scenario name contains an embedded expression (e.g. `<name>` placeholder interpolation or JS evaluation) that references an undefined variable, throws inside the expression, or is syntactically invalid JS.

Common situations: Referencing a variable not yet initialized at scenario-name evaluation time; typos in variable names inside the scenario title; JSON-path expressions over data that is null in the target environment.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/e13c678cc54261be. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/ScenarioRuntime.java:1450

            return;
        }
        String trimmed = name.trim();
        boolean wrappedByBackTick = trimmed.length() > 1
                && trimmed.charAt(0) == '`'
                && trimmed.charAt(trimmed.length() - 1) == '`';
        boolean hasPlaceholder = JS_NAME_PLACEHOLDER.matcher(trimmed).find();
        if (!wrappedByBackTick && !hasPlaceholder) {
            return;
        }
        String eval = wrappedByBackTick ? trimmed : "`" + trimmed + "`";
        try {
            Object evaluated = karate.engine.eval(eval);
            if (evaluated != null) {
                scenario.setName(evaluated.toString());
            }
        } catch (Exception e) {
            String warning = "[WARN] Failed to evaluate scenario name '" + trimmed + "': " + e.getMessage();
            logger.warn(warning);
            // Append to last step's log for report visibility
            List<StepResult> steps = result.getStepResults();
            if (!steps.isEmpty()) {
                StepResult lastStep = steps.get(steps.size() - 1);
                lastStep.appendLog(warning);
            }
        }
    }

    private static final java.util.regex.Pattern JS_NAME_PLACEHOLDER =
            java.util.regex.Pattern.compile("\\$\\{.*?}");

    // ========== Execution Context ==========

    public Object eval(String expression) {
        return karate.engine.eval(expression);
    }

View on GitHub (pinned to a22eb90246)