Testing
SWAT+ has a scenario regression test: it runs the model on a reference dataset and compares chosen output files with copies kept in the repository. Differences outside a tolerance point to a change in results, which is then either a bug or an intended change that needs new reference outputs.
Test script¶
test/spcheck.py (Python 3, standard library only) has three commands:
| Command | Use |
|---|---|
ctest <executable> <reference folder> <work folder> |
copy the reference folder into the work folder, run the executable there, and compare the files listed in the folder's .testfiles.txt with the reference copies; exit with code 1 if any value differs |
run <executable> <scenario> |
run build/<executable> on data/<scenario> in a new numbered folder under runs/<scenario>/ |
compare <scenario> <a> <b> |
compare two numbered runs of a scenario |
Options --abserr and --relerr set the tolerances (defaults 1e-8 and 0.05), and --nlines and --nerrorlines limit how much of each file is checked and reported.
Two numbers are equal when
Only real numbers are compared, value by value on matching lines; integers and text are skipped, as are the title lines with the build date (lines containing MODULAR).
Running the test at 62.0.1¶
CMakeLists.txt registers one test, Ames_sub1, with tolerances of 1e-8 absolute and 1 % relative, and reference data in data/Ames_sub1. At 62.0.1 the repository has no data/ folder; the reference datasets are in refdata/. ctest therefore fails at once with data dir not found.
To run the comparison, call the script directly with the refdata folder:
cmake -B build -D CMAKE_BUILD_TYPE=Release
cmake --build build -j 8
python3 test/spcheck.py ctest build/swatplus-*-Rel refdata/Ames_sub1 build/test-runs --abserr 1e-8 --relerr 0.01
Notes on the reference dataset:
- A full
Ames_sub1run writes about 9 GB, mostly daily carbon output, and takes a few minutes. Make sure the work folder has the space. .testfiles.txtlists 42 files. With the shippedprint.prtthe model rewrites fewer than half of them; the rest (allbasin_*files,hydin_aa.txt,ru_aa.txtand others) are copied from the reference folder and compared with themselves. The test therefore checks the HRU water and nutrient balances, plant and yield output and management output, and not channel, aquifer or basin output.
Adding a scenario¶
- Put a complete input set in a new folder under
refdata/(ordata/). - Run the model and check the outputs you want to fix as the reference.
- Commit the inputs and those outputs.
- Add a
.testfiles.txtnaming the output files to compare, one per line (#starts a comment). - Add an
add_testline toCMakeLists.txtpointing at the folder.
Continuous integration¶
.github/workflows/test.yml builds SWAT+ with Intel ifx on Ubuntu and runs ctest on pushes to branches whose name ends in main. With the data/ path above, the test step fails. The release workflow (build.yml) builds the release executables for each tag and does not run the tests.
Sources
test/spcheck.py:ctest_all,cmp_fileand therun/comparecommands.CMakeLists.txt:set(ref_dir "${PROJECT_SOURCE_DIR}/data"), tolerancesaerr = 1e-8,rerr = 0.01, andadd_test(Ames_sub1 ...). TheIthaca_sub6test is commented out.refdata/Ames_sub1/.testfiles.txt..github/workflows/test.ymland.github/workflows/build.yml.- The 9 GB output size and the list of files rewritten are from a run of the 62.0.1
gnu-lin_x86_64release executable on 20 October 2026.