libcester
|
A robust header only unit testing framework for C programming language. Support function mocking, memory leak detection, crash report.
cester is a header only automated testing framework for the C programming language, it requires no dependency and can be downloaded and used in a project immediately. Works on various platorms including embedded systems and compatible with various compilers. Allows shared instance TestInstance
object in which each test cases can use to share data and access the command line arguments.
The test results can be outputed as various format JunitXML, Test Anything Protocol, Test Anything Protocol Version 13 and text. Visit https://exoticlibraries.github.io/libcester/docs/ for documentation and tutorials.
The project is compliant with the original C language specification ISO/IEC 9899:1990 and the first POSIX specification IEEE Std 1003.1-1988 which ensures the project compatibility in various environments. It also makes use of features in the newer revisions ISO/IEC 9899:1999 and IEEE Std 1003.1-2001 whenever possible.
Even though the project is designed for C, but also works with C++ as it is compatible with C++98 Standard (ISO/IEC 14882:1998), C++03 Standard (ISO/IEC 14882:2003) and C++11 Standard (ISO/IEC 14882:2011).
The project can be used with any C or C++ compiler. There are optional macros and options that can be used to attain the desired output in the case of undesired results.
If you install the library file cester.h
using any of the commands below, it can be included in your test like <exotic/cester.h>
.
Install the library using powershell. It auto detect your insalled C and C++ compilers include directory and install libcester into the include folder. Execute the command in powershell as admin.
Use the remote installation script to install libcester with bash. If the command is executes with super user priviledge (sudo) it will be installed in the folder /usr/include else it will be installed in the folder /usr/local/include. Or you can specify where to install it using the option --installfolder=./
You can simply download the header file cester.h
from the repo into your project source folder and include it in your project. Download the file from here. The you can include it in your test relatively like #include "cester.h"
.
The documentation provides several examples, tutorials, and detailed guides for using the library. While reference provides a low-level overview of all the implemented APIs in the library.
Some of the documentation pages are listed below:
For some know unfixed issues see the page known_issues.
The macro CESTER_TEST is used to create a test case, the first parameter is the test case name the second parameter is the test instance object which is used to share data between the test cases and also has the command line arguments object and count, the last parameter is the body of the test case. The following example defines a test case that chack if NULL
is the same as ((void*)0)
cester will automatically detect and register the test cases if the macro __BASE_FILE__
is predefined by the compiler. If no test is detected see the FAQ below on ways to make cester aware of the test case.
The test above can be compiled and run like below. Do not forget to add the option -I. for gcc so it can find the __BASE_FILE__
.
Many predefined helper macros are present in cester, all cester macros begins with CESTER_ and cester_. The detail documentation of the macros is at Helper Macros.
cester supports mocking function. The mock feature currently works on GCC compiler because of the use of the --wrap
option which is not supported on MAC OSX and might not be available in other compilers. The two macros CESTER_MOCK_SIMPLE_FUNCTION and CESTER_MOCK_FUNCTION are used for function mocking.
The following test mocks a funtion that accept no parameter and return a value:
originals.c
test_mock.c
Compile the test file test_mock.c with the –wrap option e.g. in the example above the function multiply_by
was mocked so the option -Wl,–wrap=multiply_by is supplied during compilation.
More detailed explanation on mocking function can be seen at https://exoticlibraries.github.io/libcester/docs/mocking.html.
cester accepts various options to tune it functionalities. Any command line parameter that starts with –cester- is treated as cester option otherwise it is ignored. All the available options can be viewed here.
The following options performs important task:
CESTER_NO_SIGNAL
defined, cester will not be able to recover from critical crash therefore if a test case segfault the tests will terminate immediately.--cester-output=junitxml
.CESTER_NO_MEM_TEST
defined at the beginning of the source file.CESTER_NO_STREAM_CAPTURE
defined at the beginning of the source file.--cester-test=test_string,test_int
.CESTER_NO_PRINT_DETAIL
before including cester.h. All possible value can be viewed here. E.g. to print all trhe test executable information supply the flag --cester-info=all
.If no test was ran or your test cases were not detected, in most cases it because your compiler did not define the __BASE_FILE__
macro. If you are using the Visual studio IDE you should define the macro in Properties -> C/C++ -> Preprocessor -> Preprocessor Definition
as __BASE_FILE__="%(Filename)%(Extension)"
. Or you can add the macro at compile time as option to your compiler using the macro option. e.g. in gcc
Setting for Visual C compiler
You can also define the __BASE_FILE__
at the beginning of your test file with the absolute path to the test file. E.g for the test file test.c:
Alternatively the test cases should be manually registered in the main method, you will have to disable cester main function by defining the macro CESTER_NO_MAIN.
Visit this link for more detail on manual test registration.
The base file which is the file that contains the tests is included more than twice during the macro expansion process to define and register the test cases. The working principle is best explained by a thought experiment.
Imagine a source file including a header file. Then imagine the header file including the source file that included it. Now imagine doing that three times in a row within the same header file. Proceed to imagine redefining all of the identifiers each time. Finally imagine doing all of that with preprocessor directives. What you ended up with is CHEAT - Sampsa Kiiskinen
The project uses the same approach used by the cheat project. It makes a very tricky use of the C preprocessor directive to achieve test cases registration so the developer can concentrate on writing the test only while cester manages the test registration, execution and analysis.
See the pages at how it works for more explanation.
If you have any issue or you want to request a feature you can open a request here anytime and if you made some changes that should be added to the main project send in a pull request.
MIT License Copyright (c) 2020, Adewale Azeez