Testing COBOL programs
Test data files
COBOL programs read files, so tests are mostly file tests. Keep a fixtures directory with one input file and one expected output file per case, named so the case is obvious:
tests/payroll-01-input.dat tests/payroll-01-expected.txt
A test harness
A shell loop runs the program on each input and compares the output. ACCEPT FROM SYSIN lets a program read keyboard-style input from a redirected file, which keeps tests simple:
ACCEPT WS-INPUT FROM SYSIN.
Checking results
Compare byte for byte with cmp, and fail the run on any difference. For arithmetic, a tolerance line in the expected file beats exact formatting when rounding modes differ:
for f in tests/*-input.dat; do ./payroll < "$f" > out.tmp cmp out.tmp "${f%-input.dat}-expected.txt" || exit 1 done
The code on this page was run with GnuCOBOL 3.2.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev