How To Automatically Run Unit Tests Using CMake

Contents

  1. Overview

Overview

Unit tests are a great way to make sure your code is working, drive development (TDD, a.k.a Test Driven Development) and prevent regression. However, it is very easy to forget to run your unit tests regularly. CI (continuous integration) tools may help here, running unit tests on every commit that is pushed to the server. But what about local commits, and what about all the code builds that may occur before a commit is made and pushed?

This is where the automatic (or semi-automatic) running of unit tests from CMake can come in helpful.

One way to achieve this is to add a custom target in conjunction with a custom command. The custom target provides a way of running the unit tests through make (e.g. make run_unit_tests), while the custom command provides the functionality needed to actually call the unit test executable.

The above CMake commands result in the ability to run the unit tests by calling: