Unit testing in Fortran workshop

Instructor companion slides

Connor Aird

University College London

2026-11-10

Setup

Example exercises

  1. Open the exercises repo: github.com/carpentries-incubator/fortran-unit-testing/tree/main/exercises
  2. Setup GitHub Codespaces.
  3. If this does not work:
    • Run the container image locally via vscode
    • Setup local environment with:
      • CMake
      • pFUnit
      • gfortran

What is a unit test?

“If the parts don’t work by themselves, they probably won’t work well together”

(Thomas and Hunt, 2019, The pragmatic programmer, Topic 51).

Definition of a unit test

Unit tests are:

  • Isolated
  • Minimal
  • Fast
  • tests which verify the correctness of a single unit of code.

Benefits of unit tests:

  • Confidence that your code does what you expect.
  • Easier to make changes to you code.
  • Catch bugs before they are introduced.

Temperature conversions library

Throughout this workshop you will be writing tests for the temperature conversion library provided in the exercises.

Our first unit test

Testing the temperature

First, how can we test this library using standard Fortran?

What makes this implementation good? What makes it bad?

Good:

  • The tests are partially parameterise
  • Good failure logs

Bad:

  • Unable to run a single test in isolation.
  • Extra maintenance of test runner logic.
  • Do not benefit from community development efforts

Our first pFUnit test

Why are we using pFUnit?

Supports testing MPI parallelised code, including parameterising tests by number of MPI ranks.

A few other reasons:

  • The most feature rich framework.
  • The most widely used framework.
  • Being maintained.
  • Dedicated CMake and Make integrations.

Standard Fortran to pFUnit

Live coding example

Starting with the standard Fortran test we saw earlier, follow along writing tests for the temperature conversion library.

Building with CMake

Live code example

Now that we have written a test using pFUnit, let’s build it using CMake. Follow along to build your newly written pFUnit test of the temperature conversion library

Parameterising with pFUnit

Why bother parameterising?

  • No repeated test logic.
  • pFUnit handles reporting failed tests.
  • Ensures the full test suite runs.

Basic to parameterised pFUnit

What problems are there with the test we’ve just written? What happens in the case when one test scenario fails?

Live coding example

Starting with the pFUnit tests we have just written, follow along parameterising your tests for the temperature conversion library.

Further reading

Learn how to test MPI parallelised Fortran by following the workshop notes in your own time.

Scan to view the slides

QR code linking to this talk's web page.