PicoTest
A minimalist unit testing framework for C programs
|
PicoTest is a single-file unit testing framework for C programs that follows the xUnit principles:
https://en.wikipedia.org/wiki/XUnit
PicoTest is released under the terms of the 3-Clause BSD License:
https://opensource.org/licenses/BSD-3-Clause
PicoTest is a single-file package, everything is defined in the header file picotest.h. So you can either install this file in a standard location, or copy it within your project source tree.
Simply include the file in your source file, then start writing your tests!
The complete documentation is available here:
https://fredericbonnet.github.io/picotest
The documentation site was built using these great tools:
To rebuild the documentation you'll need the following tools:
If you want to serve the documentation locally you can use the provided script:
The examples subdirectory contains several example source files that demonstrate the various features of PicoTest. These examples are also integrated in the documentation.
Building the examples requires the CMake build tool along with your favorite toolchain:
PicoTest is self-tested and comes with a complete test suite that covers all aspects of the framework. This guarantees that PicoTest is production-ready.
The test suite itself is a good real-world example of PicoTest in action.
Building and running the test suite requires CMake (see next section for more information). To build the suite:
The build process makes use of the test discovery mechanism described in the next section.
To run the suite using CTest:
You can also run the suite executable test_picotest directly from the CMake output directory.
PicoTest provides a CMake module definition for easier integration with other CMake projects. In particular, it comes with an auto-discovery script for CTest that makes use of the test traversal features of PicoTest.
The file FindPicoTest.cmake is the module definition. Add this file to your CMAKE_MODULE_PATH, and add the following line to your CMake project:
You can then add the PicoTest dependency to your targets, e.g.:
If you also use CTest for test automation with enable_testing(), you can also call the provided picotest_discover_tests macro to integrate PicoTest test cases in your build process automatically. For example, assuming that the my_test_runner executable:
Then the following lines will:
PicoTest was developed on Windows 10 and Linux Ubuntu 16.04 using the following tools:
It should work with any reasonably modern C compiler that supports variadic macros.
The assertion mechanism relies on setjmp() / longjmp(). While these functions are discouraged for production code, their usage is acceptable in the context of unit testing: in our case, longjmp() is only called when an assertion fails, a situation where the actual process state is no longer reliable anyway. Moreover, they constitute the only standard exception handling mechanism for plain C code.