karatelabs/karate · error · RuntimeException

def requires '=' assignment

Error message

def requires '=' assignment: <text>

What it means

The 'def' keyword in Karate assigns a variable from an expression and therefore requires an '=' assignment operator, e.g. 'def foo = call read(...)'. If StepUtils.findAssignmentOperator finds no '=' in the step text, Karate throws this RuntimeException naming the offending text.

Solutions

  1. Add '=' after the variable name: 'def <name> = <expression>'
  2. If the step is meant to only invoke something without assigning, drop 'def' and use 'call' or 'eval' directly
  3. Verify no comment or line-wrap swallowed the '=' in multi-line steps

Example fix

// before (feature)
def result call read('helper.feature')
// after
def result = call read('helper.feature')
Defensive patterns

Strategy: validation

Validate before calling

// ensure def lines carry an assignment operator before running
static boolean isWellFormedDef(String stepText) {
    String t = stepText.trim();
    if (!t.startsWith("def ")) return true;
    int i = t.indexOf('=', 4);
    return i > 4 && !t.substring(4, i).trim().contains(" ");
}

Try / catch

try { executor.execute(step); } catch (RuntimeException e) { if (e.getMessage().startsWith("def requires '=' assignment")) { throw new IllegalStateException("add '=' to def step: " + e.getMessage()); } throw e; }

Prevention

When it happens

Trigger: Writing a def step without '=', such as 'def myVar call read("other.feature")' or 'def result someFunction()' — the '=' between the variable name and the expression is missing.

Common situations: Translating from other DSLs where assignment is implicit; forgetting '=' after a rename refactor; multi-line def steps where the '=' got lost; confusing 'def' (assignment) with 'call' (invoke-only) steps.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/StepExecutor.java:356

            result.setCallResults(stepCallResults);
        }
        // Wipe AFTER attaching so the next step starts with a clean buffer.
        stepCallResults = null;
    }

    // ========== Expression Execution ==========

    private void executeExpression(Step step) {
        runtime.eval(step.getText(), step);
    }

    // ========== Variable Assignment ==========

    private void executeDef(Step step) {
        String text = step.getText();
        int eqIndex = StepUtils.findAssignmentOperator(text);
        if (eqIndex < 0) {
            throw new RuntimeException("def requires '=' assignment: " + text);
        }
        String name = text.substring(0, eqIndex).trim();
        validateVariableName(name);
        String expr = text.substring(eqIndex + 1).trim();
        String docString = step.getDocString();

        // Handle docstring if expression is empty (docstring IS the RHS expression).
        // Null it afterwards so it isn't ALSO treated as a separate call argument below.
        if (expr.isEmpty() && docString != null) {
            expr = docString;
            docString = null;
        }

        // Check if RHS is a special karate expression (not standard JS)
        if (expr.startsWith("call ")) {
            String callExpr = appendDocStringCallArg(expr.substring(5).trim(), docString);
            executeCallWithResult(callExpr, name);
        } else if (expr.startsWith("callonce ")) {

View on GitHub (pinned to a22eb90246)