Check Unit Testing Framework for C

repository·master·Indexed 22 days ago

https://github.com/libcheck/check

Check 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.

Tokens
2.9K
Snippets
14
Records
19
Agent score
78%

What's inside Check

  1. What is Check?

    master
    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.
  2. Install Check using CMake

    master

    To install Check using CMake, create a build directory, configure the project, and build it using make. You can run the tests using make test with the CTEST_OUTPUT_ON_FAILURE=1 environment variable to ensure failure details are visible.

    $ mkdir build
    $ cd build
    $ cmake ../
    $ make
    $ CTEST_OUTPUT_ON_FAILURE=1 make test
  3. Install Check using autoconf

    master

    To install Check from source using the autoconf build system, run the following commands in the repository directory. Note that autoreconf --install handles 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 ldconfig
  4. Use checkmk to generate C unit tests

    master

    checkmk is an Awk script that automates the creation of C source files for the Check unit testing framework. It eliminates the boilerplate required to instantiate SRunner, Suite, and TCase objects 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. checkmk then generates a complete C file with a main() function that runs the tests. The generated code is printed to standard output.

    checkmk input_file.ts > output_file.c
  5. Inject custom code into main() using #main-pre and #main-post

    master

    You 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 using checkmk identifiers. Note: This can only be used once, and no #suite, #tcase, or #test directives may follow it.
    • #main-post: Inserts code at the very end of main(), after tests have run. This is useful for cleanup or custom exit statuses. Note: If you use #main-post, checkmk will not provide a return statement; you must provide your own.
    #main-pre
    // Your setup code here
    
    #main-post
    // Your cleanup or exit code here
    return 0;
  6. Convert a test suite to C using checkmk

    master

    To use checkmk, you pass a test suite file (e.g., with a .ts extension) to the command and redirect the output to a .c file. The resulting C file can then be compiled using a standard C compiler like cc or gcc, linking against the check library.

    # 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
    ./basic
  7. Integrate the Autotools example into another project

    master

    If 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.am
  8. Install the Check Eclipse template

    master

    To use the Check framework's Eclipse template for creating unit tests, you must first ensure that the check framework 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):

    1. Open the New Source File dialog.
    2. Click Configure....
    3. Click Import....
    4. 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 testing in 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.xml
  9. Integrate the CMake example into another project

    master

    If 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.txt
  10. Build the example using CMake

    master

    To 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 cmake and make commands.

    MSVC (Microsoft Visual C++)

    Use NMake Makefiles as the generator.

    Manual Configuration for MSVC

    If pkg-config is not installed and you are using MSVC, you must manually specify the Check installation location in tests/CMakeLists.txt by setting the CHECK_INSTALL_DIR variable. A commented-out example is provided in that file.

    Note: Avoid running make install or nmake install unless 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 test
  11. Build the example using Autotools

    master

    To 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 install unless you specifically want to install the money example onto your system.

    $ autoreconf --install
    $ ./configure
    $ make
    $ make check