Write and check a model › Write test cases

Write and check a model

Test cases in depth

Extension — not covered by the 1.0 stability promise (testing; the core)

The core gives you test plans and cases written as timelines: at 0ms do …, at 10ms expect …, within 50ms expect …. The testing extension adds what larger test plans need: a table of cases that differ only in their values, code that runs before each case, the system a plan runs on, and the objective and preconditions a test document lists. It also names the old step blocks, so that archec fmt can rewrite them.

archec check --core lists every use of the extension in a model.

A worked example: a fan controller#

The fan runs slow above its limit and fast ten degrees above it. The bench system holds one fan.

project Fan {
  edition "1.0"
}

message TEMP {
  celsius: float32
}
message RESET {}

component Fan "Fan controller" {
  in sensor: can     receives TEMP, RESET
  var limit: float32 = 40.0

  states {
    state Off
    state Low
    state High
    Off -> Low on TEMP [celsius > limit]
    Low -> High on TEMP [celsius > limit + 10.0]
    * -> Off on RESET
  }
}

system Bench {
  part fan: Fan
}

system Field {
  part fan: Fan
}
Diagram: Bench
What archec draws from this code: Bench
Try it

The test plan runs on the bench, lowers the limit to 30 °C before every case, and writes three readings as one table:

test-plan TP "Fan speeds" {
  setup fan.limit = 30.0
  model Bench

  case TC-0 "Starts off" {
    objective "The fan is off before any reading"
    preconditions "The bench is at room temperature"
    at 0ms expect fan.state == Off
  }

  case TC-S "A higher limit of its own" {
    setup ```python
      fan.limit = 60.0
    ```
    at 0ms do send fan.sensor TEMP { celsius: 50.0 }
    at 1ms expect fan.state == Off
  }

  cases "Reading {t} °C" {
    at 0ms do send fan.sensor TEMP { celsius: {t} }
    at 1ms do send fan.sensor TEMP { celsius: {t2} }
    at 2ms expect fan.state == {s}
    rows {
      id    | t    | t2   | s
      TC-1  | 20.0 | -    | Off
      TC-2  | 35.0 | -    | Low
      TC-3  | 35.0 | 45.0 | High
    }
  }
}
archec test fan.arche

All five cases pass, in Arche's simulator and with --sim web:

PASS  TP/TC-0 Starts off
PASS  TP/TC-S A higher limit of its own
PASS  TP/TC-1 Reading 20.0 °C
PASS  TP/TC-2 Reading 35.0 °C
PASS  TP/TC-3 Reading 35.0 °C

Tables of cases#

cases "label" { … rows { … } } is one body and a line per case. The first line of rows names the columns: id first, then one per value. Each row after it is an ordinary case, with its own id in every report.

  • {column} anywhere in the label or the body is that row's cell: in a value, a state's name, a text, code.
  • A cell - leaves out every line that uses its column. Above, TC-1 and TC-2 send one reading, and TC-3 sends two.
  • A cell that holds a | is written in quotes.

A row runs as the case written out would. TC-3 is this case, in the same plan:

test-plan TP "Fan speeds" {
  setup fan.limit = 30.0
  model Bench

  case TC-3 "Reading 35.0 °C" {
    at 0ms do send fan.sensor TEMP { celsius: 35.0 }
    at 1ms do send fan.sensor TEMP { celsius: 45.0 }
    at 2ms expect fan.state == High
  }
}

Code before each case: setup#

A plan's setup runs before every case of the plan; a case's setup runs after it, before the case's first moment. It is the observer code of Arche's test runner: a Python line after setup, a ```python block, or an action language block (```arche), which archec translates. It sets what the case starts from, here a part's variable.

Both simulators run it. The browser simulator reads each line as it reads a test line's do, so a line it cannot read makes the case inconclusive, never silently skipped. To start from a named set of values, a case's keyframe is often clearer (the spec).

Which system a plan runs on: model#

model Bench names the system the plan's cases run on. Without it, a plan runs on its simulation's model, else on its deployment's system. A model with several systems, as here, needs it to pick the right one.

What a test document says: objective and preconditions#

objective says what a case shows, and preconditions the state it needs first. They do not change the run. They are written to the Arche project's test plans and to the LaTeX specification (archec doc), where a reviewer reads them.

The old step blocks#

Cases used to be written as steps, each with a do, a wait and its expects:

test-plan TP "Old form" {
  model Bench

  case TC-1 "Warm" {
    step "Warm reading" {
      do send(fan.sensor, TEMP(celsius=45.0))
      wait 10ms
      expect fan.state == "Low"
    }
  }
}

That form is not read in any edition: it is an error (old-form). archec fmt writes it as a timeline, exactly, with each step's name kept:

test-plan TP "Old form" {
  model Bench

  case TC-1 "Warm" {
    at 0ms "Warm reading" do send(fan.sensor, TEMP(celsius=45.0))
    at 10ms expect fan.state == "Low"
  }
}

A step's fault becomes a moment's (at 10ms fault freeze fan), which only Arche's simulator runs (testing with faults). The hint arche-test-line then offers to write the Python lines in the action language. Where the rewrite cannot be exact, the message gives the lines as near as they go, and what differs.

Limits#

  • setup, line by line. The browser simulator runs each line of a setup alone, as a test line (fan.limit = 60.0). A line it cannot read alone, such as an if with its body, makes the case inconclusive there: run it in Arche's simulator.
  • A table's cells are text. A cell is put in the body as it is written, and the case is checked after that. There is no type per column, and no range or product of values to generate rows from.
  • objective and preconditions are not in the HTML or Markdown specification, only in the LaTeX one and the Arche project.
  • Faults run in Arche's simulator only. A case with a fault moment stays there, and the browser simulator reports it as a problem.