Building an Embedded TDD Environment for 2025: MSYS2 + CppUTest

1. Background: Classics and Obsolescence (The Gap)

Recently, I have been studying James Grenning’s classic work “Test-Driven Development for Embedded C”. The TDD (Test-Driven Development) philosophy and the Dual-Targeting strategy presented in the book are very robust and serve as a good remedy for solving the coupling of embedded business logic.

However, the accompanying source code in the book is quite outdated (over a decade old), and opening it directly in modern CLion or Visual Studio results in numerous errors. The original Makefile is also incompatible with the current version of GCC. To avoid being deterred by environmental issues, I have developed a build solution for 2025 based on MSYS2 and a community-maintained new repository.

Core Objective: To achieve a native Linux-like build experience on Windows and successfully run the <span>LedDriver</span> test cases from the book.

2. Toolchain Preparation (Prerequisites)

We abandon the cumbersome IDE configurations and return to the purest command-line toolchain.

1. Source Code Repository (Modern Fork)

Do not use the original link from the book; instead, I recommend using this version that has been fixed for modern compilers:

  • GitHub Repo: Mozilla88/tddcode [https://github.com/Mozilla88/tddcode]
  • Operation: git clone to local (e.g., D:\Dev_Workspace\Lab\tddcode).

2. MSYS2 Environment (The Environment)

The most recommended Unix-like environment for Windows.

  • Download: MSYS2 official website [https://www.msys2.org/]
  • Installation: Just keep clicking next.
  • Key Point: MSYS2 has many shell environments (Clang64, Mingw64, etc.), we choose MSYS2 UCRT64 (Universal C Runtime) for the best compatibility.

3. Detailed Setup Steps (Step-by-Step)

All commands below are executed in the MSYS2 UCRT64 terminal window.

Step 1: Install Compiler (GCC)

Open the UCRT64 terminal and execute the following command to install the GCC toolchain:

  • 1
pacman -S mingw-w64-ucrt-x86_64-gcc

Step 2: Install Build Tools (Make) — [Common Pitfall]

Simply installing GCC is not enough; the build in the book depends on <span>make</span>. Many tutorials miss this step, leading to the error <span>bash: make: command not found</span>.

Bash

  • 1
pacman -S make

(Alternatively, install <span>pacman -S base-devel</span> for a more complete toolset)

Step 3: Compile CppUTest Framework

We need to compile the latest CppUTest from source to ensure compatibility with the local GCC version.

  1. Download: Get the latest release version from the CppUTest official website [https://cpputest.github.io] or GitHub.
  2. Path: Assume it is extracted to D:\Dev_Workspace\Lab\cpputest-3.8.
  3. Compile: Switch to the path in the terminal and build (note the MSYS2 path format):

Bash

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
# Note: Windows D:\ corresponds to MSYS2 /d/cd /d/Dev_Workspace/Lab/cpputest-3.8
# Configure and check
./configure
make check
make tdd

If you see <span>OK</span> at the end, it means the test framework has compiled successfully.

Step 4: Configure Environment Variables (The Bridge)

The Makefile in the book depends on the <span>CPPUTEST_HOME</span> variable to locate the test framework. We need to set it.

Permanent Solution: Write the environment variable into the <span>.bashrc</span> configuration file:

Bash

  • 1
  • 2
  • 3
  • 4
  • 5
# Be sure to replace with your actual path, using Linux-style slashes /d/...echo 'export CPPUTEST_HOME=/d/Dev_Workspace/Lab/cpputest-3.8' >> ~/.bashrc
# Refresh the configuration immediately
source ~/.bashrc

4. Witness the Miracle (The Moment of Truth)

Everything is ready, return to the directory of the cloned book source code.

  1. Change directory: Bashcd /d/Dev_Workspace/Lab/tddcode
  • 1
  • Run tests: The code in the book supports Unity and CppUTest; here we use the more powerful CppUTest: Bashmake -i -f MakefileCppUTest.mk
    • 1

    🎉 Successful Output

    If you see output similar to the following, congratulations, your TDD environment is set up!

    Plaintext

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    compiling LedDriverTest.cpp...compiling AllTests.cpp
    Linking BookCode_CppUTest_tests
    Running BookCode_CppUTest_tests...............................................................................................OK (95 tests, 95 ran, 484 checks, 0 ignored, 0 filtered out, 3 ms)

    5. Common Command Quick Reference (Cheat Sheet)

    To facilitate future practice, here are some frequently used commands:

    • Run CppUTest tests: make -i -f MakefileCppUTest.mk
    • Clean build: make -i -f MakefileCppUTest.mk clean
    • Run Unity tests (from earlier chapters): make -i -f MakefileUnity.mk

    Engineer’s Note: Although modern IDEs (like CLion) also have GoogleTest plugins, manually setting up this environment allows you to better understand the underlying principles of “Host-based Testing”: how code is compiled and linked to mock libraries in PC’s GCC, separate from hardware. This is the starting point of white-box thinking.

    Leave a Comment