Our recent interviews with NetLogo users identified tools for testing code as a development need. And recent literature on good practices in agent-based modeling identifies the lack of code testing, or even a culture of testing, as a major concern–a sign that agent-based modeling is not yet a mature approach to science. Building tools for testing code into NetLogo would make it easier to test code, and would promote a code-testing culture by reminding users that code is not finished until it’s tested.
Here I describe an idea I have for a code testing tool, and I look forward to seeing other ideas.
I often write custom code to test a particular submodel by executing the submodel over wide ranges of values for several different inputs. For example, I might test a fish growth submodel using a test procedure that writes to file the growth rate calculated by the submodel over wide ranges of food intake, swimming speed, temperature, and fish size. Then I analyze that output in Excel (etc.) to search for errors.
I can envision a tool somewhat like BehaviorSpace that would produce this kind of test output. It would let users:
Create local variables that identify specific turtles and/or patches, for example:
let a-fish one-of turtles
let fishs-patch [patch-here] of a-fish
Create other local variables that are used as input to the code being tested, and specify ranges of values of those local variables (or observer variables). The BehaviorSpace syntax for specifying ranges of values could be used. For example:
let fish-lengths [5 5 25]
let patch-foods [0 1 20]
let temperatures [0 2 30]
Provide a block of code that updates the submodel for each combination of input variable values:
ask a-fish [set length fish-lengths]
ask fishs-patch [set food patch-foods]
set temperature temperatures ; temperature is an observer variable
ask a-fish [update-food-intake] ; execute a procedure affecting growth
Specify the outputs to be reported for each combination of input values, such as:
[length] of a-fish
[food] of fishs-patch
temperature
[growth] of a-fish ; A turtle reporter that calculates growth
Then of course the user could execute the tester so it creates an output file with all the results. The output file could also produce metadata documenting exactly what code was executed how.
I have now drafted two concepts for code testers. One would be BehaviorSpace-like and test a piece of code over wide ranges of selected variables that affect it. The second would be an extension (perhaps) that records the values of selected variables at the start and end of a selected procedure. The NetLogo development team has these concept descriptions.
The absence of a testing tool makes it challenging not only to validate NetLogo code but also to publish it. As an illustration, here’s an example from an open review:
One workaround is to use the R package logolink to programmatically run NetLogo experiments from R, combined with testthat to define expectations and check results. While this isn’t a built-in testing suite for NetLogo, it can serve a similar purpose.
This is good to know about. We’ll keep it in mind for the future as we think more about potential tools for testing. Is this something you would be interested in contributing to?
There are several ways to use LogoActions. I’m using it for all the cases below.
One is the one you mentioned: running and integrating automated tests for BehaviorSpace experiments.
Another is generating reproducible reports that depend on NetLogo simulations. That is where Quarto and logolink come in.
You can also create unit tests by defining multiple experiments inside those reports, but I do not think that is the ideal setup. NetLogo still needs a proper test framework like testthat, although this moves things a bit in that direction.
A bigger use case is automating large experiment runs on large runners. If someone has a GitHub Pro account (free for students and academics), they can already run heavier experiments directly in their repositories and just collect the results.
About the broken links: I merged netlogo-check intoLogoActions, so the old links no longer work. I added a note in the post informing this. The netlogo-check workflow is shown in the section Running Experiments with Quarto and logolink.
I just released the check-netlogo action in the LogoActions project, which solves most of the issues we’ve been discussing.
Together with setup-netlogo, it automatically checks whether all BehaviorSpace experiments in a repository’s NetLogo models run without errors. This lets you run parameter sweeps on every commit to the main branch and catch problems introduced by changes to models or experiments.
Below is a basic workflow configuration. To use it, create a file named check-netlogo.yaml with the content below and place it in .github/workflows at the root of your repository.
So glad to see this! Testing is one of those things that usually falls by the wayside in ABM, but these tools make it so much more approachable. The GitHub Actions integration is exactly what the community needs to build a proper 'code-testing culture.
For those that may be struggling with this subject, I suggest checking the solutions I presented to JOSS reviewers to verify the functionality of the LogoClim model: