Skip to content

Introduction

Elwardi edited this page Oct 12, 2025 · 3 revisions

So, you're looking for a unit testing framework to help you discover bugs and keep your (OpenFOAM-related) code's quality high enough? Well, that is hard to achieve, and this is my attempt at it! Enjoy!

Motivation

Writing OpenFOAM code and hoping for the best is a very common practice in the OpenFOAM community. Well, testing code on specific OpenFOAM cases is more common, but it's basically the same thing.

The need for an easy-to use "harness suite" that doesn't make you waste 80% of your time on setting up tests has led me to put this repository together, with the following in mind:

  • The repo can be used as-is with all main OpenFOAM forks
  • Users only have to
    • Write tests for their libraries; and of course ship them with custom Make directories for complete compile-time freedom.
    • Provide their OpenFOAM test cases.
  • Tests should be fast. If you write small-enough tests, you should be able to get feedback on test fails without leaving your editor.

Prerequisites

I'm assuming you're using this framework on a Linux machine, it can be a real one, a VM or a container, but there is no support for running this on Windows whatsoever.

To compile and run tests with this framework, you need:

  • A sourced OpenFOAM installation.
  • What's required to build Catch2 v3.x library (mainly a recent cmake with common build essentials).

Note: HTML report generation via xsltproc is deprecated in v2.0.0. Use --report for JSON output instead.

Design principles

  • Tests are created and ran with Catch2 v3.x
  • No mocking library is shipped with the framework by default as I don't think mocking is a good idea in general
    • But you always can grab and use fakeIt for example.
  • Each library you want to test has its own test driver
    • Convention: The name of the test binary is always testDriver and is compiled inside your tests directory for that library.
    • Each class's tests suite should be written in a separate .C file
  • The same unit-test case can check for results both in serial and in parallel.
  • In the presence of a mesh object, all tests that interact with it are "integration tests" at best.
  • Tagging the test case is used to distinguish between serial/parallel tests, and to specify which OpenFOAM case to use to run the tests.
  • Supported Catch2 reporters:
    • stdout for output at the console (Best for local and CI testing)
    • compact for status overview in a single line
    • xml and junit for Data Collectors

Clone this wiki locally