docs / Miri for Agents / Tests — miri test

Tests — miri test

Mark a function with @test and miri test finds and runs it. Each test runs in its own process, so a crash fails one test instead of the whole run. Here is a file with a deliberately wrong expectation in its second test:

scores.mi
// A function and two tests for it; the second test expects the wrong total.

use system.collections.list
use system.testing.{assert_eq}

fn total_of_passing(scores [int]) int
    var total = 0
    for s in scores
        if s >= 5: total = total + s
    total

@test fn counts_scores_from_five_up()
    assert_eq(total_of_passing(List([7, 5, 4])), 12)

@test fn empty_list_totals_zero()
    assert_eq(total_of_passing(List<int>()), 1)
$ miri test scores.mi
running 2 tests
test scores.mi::counts_scores_from_five_up ... ok
test scores.mi::empty_list_totals_zero ... FAILED

failures:

---- scores.mi::empty_list_totals_zero ----
Runtime error: assertion failed at scores.mi:16:5: expected 1, got 0

test result: FAILED. 1 passed; 1 failed; 0 ignored
exit status 1

With --format json, each failure carries the code, the line and the expected and actual values, ready for the agent to compare:

$ miri test scores.mi --format json
{
  "schemaVersion": 1,
  "ok": false,
  "command": "test",
  "diagnostics": [],
  "exitCode": 1,
  "durationMs": 422,
  "tests": {
    "total": 2,
    "passed": 1,
    "failed": 1,
    "ignored": 0,
    "results": [
      {
        "path": "scores.mi",
        "name": "counts_scores_from_five_up",
        "outcome": "passed"
      },
      {
        "path": "scores.mi",
        "name": "empty_list_totals_zero",
        "outcome": "failed",
        "detail": "Runtime error: assertion failed at scores.mi:16:5: expected 1, got 0\n",
        "code": "MER_RT_005",
        "line": 16,
        "column": 5,
        "expected": "1",
        "actual": "0"
      }
    ],
    "rejectedFiles": [],
    "filesRead": 1
  }
}
exit status 1

The exit status says what happened: 0 all passed, 1 a test failed, 2 no test ran at all (a file did not compile, or there were no tests). An agent can trust a 0: a run that found nothing to test is never reported as a pass. The miri-testing pack covers assertions and test attributes.