Check Unit Testing Framework for C
repository·master·Indexed 22 days ago
https://github.com/libcheck/checkCheck is a unit testing framework for C that provides isolation by running tests in a separate address space, allowing it to catch segmentation faults and other signals without crashing the test runner. It includes checkmk, an Awk script that automates the creation of C source files for tests by translating a custom concise format into C programs. The framework integrates with IDEs and provides utilities like check.mk to improve make check behavior with parallelism and incremental testing.
What's inside Check
- Check is a unit testing framework for C designed with a simple interface. A key feature is that tests are run in a separate address space, which allows the framework to catch both assertion failures and critical code errors like segmentation faults or other signals without crashing the test runner itself. The output is compatible with source code editors and IDEs.
Install Check using CMake
masterTo install Check using CMake, create a build directory, configure the project, and build it using
make. You can run the tests usingmake testwith theCTEST_OUTPUT_ON_FAILURE=1environment variable to ensure failure details are visible.$ mkdir build $ cd build $ cmake ../ $ make $ CTEST_OUTPUT_ON_FAILURE=1 make testInstall Check using autoconf
masterTo install Check from source using the autoconf build system, run the following commands in the repository directory. Note that
autoreconf --installhandles the setup of necessary tools like autoconf and automake. By default, Check installs to/usr/local/lib.$ autoreconf --install $ ./configure $ make $ make check $ make install $ sudo ldconfigWhat is checkmk?
mastercheckmk is a tool that translates concise versions of test suites (written in a custom format) into C programs. These generated C programs are designed to be used with the Check unit test framework.Use checkmk to generate C unit tests
mastercheckmkis an Awk script that automates the creation of C source files for the Check unit testing framework. It eliminates the boilerplate required to instantiateSRunner,Suite, andTCaseobjects and manage their relationships with test functions.Users provide an input file using a C-preprocessor-like syntax to declare tests, suites, and test cases.
checkmkthen generates a complete C file with amain()function that runs the tests. The generated code is printed to standard output.checkmk input_file.ts > output_file.cInject custom code into main() using #main-pre and #main-post
masterYou can insert custom C code into the generated
main()function to configure the test runner (e.g., setting timeouts, fixtures, or logging).#main-pre: Inserts code immediately after local variable declarations but before any test relationships are established. This is the ideal place to set up test fixtures or tweak settings usingcheckmkidentifiers. Note: This can only be used once, and no#suite,#tcase, or#testdirectives may follow it.#main-post: Inserts code at the very end ofmain(), after tests have run. This is useful for cleanup or custom exit statuses. Note: If you use#main-post,checkmkwill not provide areturnstatement; you must provide your own.
#main-pre // Your setup code here #main-post // Your cleanup or exit code here return 0;Convert a test suite to C using checkmk
masterTo use
checkmk, you pass a test suite file (e.g., with a.tsextension) to the command and redirect the output to a.cfile. The resulting C file can then be compiled using a standard C compiler likeccorgcc, linking against thechecklibrary.# 1. Translate the test suite to a C file checkmk basic_complete.ts > basic_complete.c # 2. Compile the C file, linking with the check library (-lcheck) cc -o basic basic_complete.c -lcheck # 3. Run the generated test program ./basicIntegrate the Autotools example into another project
masterIf you want to use the Autotools-based structure from the money example in your own project, you should start with the following file hierarchy:
example |--- configure.ac |--- Makefile.am |--- src | |--- Makefile.am |--- tests |--- Makefile.amInstall the Check Eclipse template
masterTo use the Check framework's Eclipse template for creating unit tests, you must first ensure that the
checkframework is installed on your system and the library is available in your system path.Follow these steps to import the template into Eclipse (tested with Eclipse Helios CDT):
- Open the New Source File dialog.
- Click Configure....
- Click Import....
- Select the template XML file (e.g.,
check_template_eclipse_helios.xml).
Once imported, the template will appear under the C Source File tree as
C source template with check unit testingin the Preferences dialog. You can then select this template from the Template combo box when creating a new source file.1. New Source File -> Configure... -> Import... -> select check_template_eclipse_helios.xmlIntegrate the CMake example into another project
masterIf you want to use the CMake-based structure from the money example in your own project, you should start with the following file hierarchy:
example |--- CMakeFiles.txt |--- cmake | |--- config.h.in | |--- FindCheck.cmake |--- src | |--- CMakeFiles.txt |--- tests |--- CMakeFiles.txtBuild the example using CMake
masterTo build the money example using CMake, ensure you have the following programs installed:
- CMake 2.8
- Check 0.9.9
- pkg-config 0.26 (optional)
Unix-compatible shells
Use standard
cmakeandmakecommands.MSVC (Microsoft Visual C++)
Use
NMake Makefilesas the generator.Manual Configuration for MSVC
If
pkg-configis not installed and you are using MSVC, you must manually specify the Check installation location intests/CMakeLists.txtby setting theCHECK_INSTALL_DIRvariable. A commented-out example is provided in that file.Note: Avoid running
make installornmake installunless you want to install the money example onto your system.# For Unix-compatible shells $ cmake . $ make $ make test # For MSVC $ cmake -G "NMake Makefiles" . $ nmake $ nmake testBuild the example using Autotools
masterTo build the money example using the Autotools build system, ensure you have the following programs installed:
- Autoconf 2.59
- Automake 1.9.6
- Libtool 1.5.22
- Check 0.9.9
(Note: Older versions may also work.)
Follow these steps to configure, build, and run the tests. Avoid running
make installunless you specifically want to install the money example onto your system.$ autoreconf --install $ ./configure $ make $ make check