Skip to main content
Version: 🚧 Alpha 🚧

Writing tests in GAML

GAML tests group assertions and report which checks pass or fail. Use the test statement for the checks, assert for expected conditions, and an experiment declared with mode: test to display a test report. The legacy experiment facet type: is accepted with a deprecation warning; use mode: in new models.

A complete test model​

This model checks a few geometry operations. Each test has a name and can contain multiple assertions:

model GeometryTests

experiment GeometryTests mode: test autorun: true {
geometry base <- square(10) at_location {5, 5};
geometry shifted <- square(10) at_location {10, 5};
geometry distant <- square(10) at_location {50, 50};

test "Area and location" {
assert base.area = 100.0;
assert base.location = {5, 5};
}

test "Intersection" {
assert base intersects shifted;
assert not (base intersects distant);
}
}

When a test runs, GAMA evaluates each assertion in it. A failed assertion marks the test as failed; the remaining assertions in that test are still evaluated. Tests run as part of the model, but the test report is provided by an experiment declared with mode: test.

Assertions​

An assert checks that its boolean expression is true. A false assertion inside a test is reported as a failure. Outside a test, a false assertion raises an error during model execution.

model AssertionExample

global {
test "Basic checks" {
assert 1 + 1 = 2;
assert not (5 > 6);
}
}

experiment AssertionExample mode: test autorun: true {}

An assertion can also have a label to make a failure easier to identify, or the warning: true facet when a failed check should be reported as a warning. See the assert reference.

Prepare state with setup​

Use setup when several tests share mutable state that must be reset before each test. The setup block runs before every test in its containing model, species, or experiment.

model SetupExample

experiment SetupExample mode: test autorun: true {
geometry subject;

setup {
subject <- square(10) at_location {5, 5};
}

test "Initial geometry" {
assert subject.area = 100.0;
}

test "Translated copy" {
geometry moved <- subject translated_by {2, 0};
assert moved.location = {7, 5};
assert subject.location = {5, 5};
}
}

Each test starts with a newly initialized subject, so one test cannot affect another through this variable. See the setup reference and test reference for statement details.

Organize tests​

Tests can be declared in the model, a species, or an experiment. Put a test near the state or behavior it checks. An experiment-level test is useful when the test setup and assertions should be separate from the model's regular experiment interface.

The mode: test facet is not a replacement for assertions: it selects the experiment that reports test results. See experiment declarations and the GAMA library's unit test examples.