Test-drive: the simple testing framework

I just released the first official version of the test-drive project, which offers a procedural approach for creating and organizing unit tests, without requiring external tools and only using standard Fortran. I developed this project originally for TOML Fortran and the Fortran package manager (fpm) as a light-weight and practical option to run our unit tests.

It turned out good enough to spin off a separate testing framework and we are now in the progress of adopting it for testing stdlib: Add testing module to allow better structuring of test suites by awvwgk · Pull Request #494 · fortran-lang/stdlib · GitHub.

Therefore, we decided to maintain it at the Fortran-lang GitHub organization from now on:

Support for meson, cmake and fpm is available with test-drive and I already updated the cmake template to automatically make test-drive available together with stdlib. Since it just a single file you could also just drop it in your project without any build system integration required.

How does it work?

A test defined for this framework will have access to a derived type for error handling and a overloaded procedure called check to create the actual test conditions.

subroutine test_something(error)
  type(error_type), allocatable, intent(out) :: error
  ! ... do calculation
  call check(error, actual, expected)
end subroutine test_something

The error_type is not really special or fancy, because it contains basically only an error message when allocated, the only special thing is that it uses a final procedure to enforce no test condition can fail unnoticed.

There is some boilerplate to collect and organize the unit tests in test suites and allow selection of individual tests or suites via command line arguments as well, which is mainly based on small derived types with procedure pointers. I found Fortran’s way of handling procedure pointers provides a very powerful and expressive way to build complex but well-defined workflows without needing object-oriented programming patterns.

Feedback is of course very welcome. Feel free to leave a comment here, open an issue at the repository or send a patch.