Testing

Contents
Hand assert subroutine
Table cases
Run with a script
Red green habit

Hand assert subroutine

A test module with one check subroutine is enough to start. It prints PASS for good cases and stops with FAIL for bad ones. Keep the module in its own file and reuse it in every test program.

module tiny_test implicit none contains subroutine check(ok, name) implicit none logical, intent(in) :: ok character(len=*), intent(in) :: name if (ok) then write(*, "(a)") "PASS: " // name else write(*, "(a)") "FAIL: " // name error stop "test failed" end if end subroutine check end module tiny_test

Table cases

Table driven tests store inputs and answers in arrays and loop over them. Adding a case means adding one array element, not new code. Put the function under test in the contains section while it is small.

program test_twice use tiny_test implicit none integer :: want(4) = [2, 4, 6, 8] integer :: i do i = 1, size(want) call check(twice(i) == want(i), "twice") end do contains integer function twice(k) implicit none integer, intent(in) :: k twice = 2 * k end function twice end program test_twice

Run with a script

Compile the module with the test program and run the result. Two commands give a full test run with no tools to install. Save them in a small shell script and rerun after every change.

gfortran -Wall -Wextra tiny_test.f90 test_twice.f90 -o test_twice ./test_twice

sh run_tests.sh

PASS: twice PASS: twice PASS: twice PASS: twice

Red green habit

Work in a red green habit: watch a new test fail, make it pass, then clean up. A failing run proves the test can catch the bug. Never skip the red step or a passing test may prove nothing. The error stop line ends the run with a nonzero code, so scripts notice the failure.

FAIL: twice ERROR STOP test failed

Next: Build with gfortran and make

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: