Improving C/C++ Unit Test Development Efficiency with GCC Plugins
The article proposes a GCC‑plugin‑based tool called TU that automatically extracts AST information from C/C++ source files to generate boundary unit tests, custom cases, and mocks without modifying business code, dramatically reducing the manual effort required for comprehensive test development.
C/C++ development efficiency, especially for unit testing, is often criticized for being cumbersome. Developers tend to avoid writing unit tests due to the high effort required. This article explores how to improve unit test development efficiency by leveraging GCC plugins to automate test case generation.
Motivation
The basic workflow of C/C++ unit testing involves manually writing test code, which is labor‑intensive, particularly when stubbing private methods. Existing open‑source frameworks (e.g., GoogleTest) still require substantial boilerplate, and commercial tools are limited.
To reduce this effort, the article proposes extracting function and class information from source files and automatically generating corresponding unit tests. The key challenge is obtaining accurate declarations, especially for private/protected members.
Approaches considered
1.1 Using regular expressions – feasible for simple signatures but fragile for complex C/C++ syntax. Example function definitions:
void test(int arg) {}
void test1(template<template<string>> arg,... ) {}
void test2(int(*func)(int ,float,...), template<template<string>> arg2) {}These regexes can extract function names and return types, but they cannot reliably validate C/C++ syntax.
1.2 Using flex/bison – building a full lexer/parser provides accurate parsing but requires substantial effort to maintain.
1.3 Leveraging the compiler‑generated Abstract Syntax Tree (AST) – GCC creates an intermediate representation (GENERIC, GIMPLE, RTL) and an AST (TREE) during compilation. By writing a GCC plugin, one can access this AST to retrieve function return types, names, parameter types, and other metadata.
Solution: TU (Translate Unit)
The proposed TU tool is based on method 3. It uses GCC plugins to collect AST information, header includes, and custom attributes (e.g., [[tu::case]] , [[tu::mock]] ). The collected data is then used to automatically generate boundary test cases without modifying business code.
Key plugin events used:
PLUGIN_INCLUDE_FILE – gathers included headers.
PLUGIN_OVERRIDE_GATE – obtains ordinary functions and classes.
PLUGIN_PRE_GENERICIZE – handles template instantiations.
PLUGIN_ATTRIBUTES – processes custom attributes such as tu::case and tu::mock .
Features Demonstrated
2.1 Zero‑modification boundary test generation – TU can produce exhaustive combinations of parameter boundary values, helping to uncover crashes even without explicit assertions.
2.2 Custom test cases via [[tu::case]] – developers can annotate a function to specify expected return values and argument sets, e.g.:
[[tu::case("NE","123","1","1000")]]2.3 Automatic mock generation via [[tu::mock]] – allows quick creation of mock functions for external dependencies such as Redis or databases.
Advantages
Simple integration – adding the plugin to the build script (e.g., CMake) enables automatic test generation.
Full‑permutation of boundary values reduces missed cases and repetitive work.
Supports user‑defined cases and mock methods, enhancing flexibility.
Conclusion and Outlook
The article compares three automatic test generation methods, details the TU solution built on GCC‑AST, and demonstrates its effectiveness. Future work includes IDE integration for one‑click test generation and further automation of the development workflow.
References:
GCC Plugins
Functions for C++ (GCC Internals)
vivo Internet Technology
Sharing practical vivo Internet technology insights and salon events, plus the latest industry news and hot conferences.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.