Skip to contents

Run all active unit tests defined on a stock-and-flow model. Use unit_test() to define tests; use unit_tests() to display them.

Usage

# S3 method for class 'stockflow'
verify(object, test = NULL, ...)

Arguments

object

An stockflow object.

test

Integer vector of test number(s) to run (numbers-based, as shown by unit_tests()). Defaults to NULL (run all tests).

...

Additional arguments passed to sim_settings() (e.g., seed, dt).

Value

An object of class verify_stockflow, returned invisibly. Use as.data.frame() to extract results as a data frame and plot() to visualize the simulations used. The object contains:

results

List of test result entries, one per test (including inactive tests, which appear with status = "skip"). Each entry has label, expr_str, conditions, status, error_type, message, and outcome.

object

The stockflow model the tests were run against.

sims

Nested list of simulate_stockflow objects used internally by plot.verify_stockflow(). Always present (never NULL).

j

Named integer vector mapping each test label to its condition index. Used internally by plot.verify_stockflow().

n

Number of simulations run per condition.

n_conditions

Number of unique simulation conditions.

test_indices

Integer vector of the original 1-based test numbers that were run (as shown by unit_tests()). Equal to seq_along(results) when test = NULL (all tests run).

Details

Calling verify() on a stockflow model will first simulate the model, then run all tests — including those that require re-simulation under alternative conditions. Simulations are always retained in the returned object so that plot.verify_stockflow() works without any extra arguments.

For repeated-run robustness testing use ensemble() instead.

Examples

sfm <- stockflow("SIR") |>
  unit_test(expr = all(susceptible >= 0)) |>
  unit_test(
    label = "recovered increases over time",
    expr = all(diff(recovered) >= 0)
  )

verify(sfm)
#> 
#> ── Stock-and-Flow Unit Test Results ────────────────────────────────────────────
#> 2/2 tests passed.
#>  1. susceptible is at least 0 (for all values)
#>  2. recovered increases over time