All macros in the library starts with CESTER_.
The macro for creating a test case. The macro takes care of initialization and declaration of the function and also takes care of the test case registration. The first parameter is the name of the test case, the second parameter is the TestInstance object to share data and command line arguments and the third is the test case body.
During the compile time code generation process the first time the macro is expanded it declare the test case function, in the second expansion it registered the test case function in the static array and in the final expansion the test case function is initialized.
For the test case below:
CESTER_TEST(test_case_name, test_instance,
cester_assert_nothing();
)
First Expansion
static void cester_test_test_case_name(TestInstance* test_instance);
Second Expansion
static TestCase cester_test_cases[] = {
{ CESTER_RESULT_UNKNOWN, __LINE__, CESTER_RESULT_SUCCESS, 0.000, 0.000, (char*) "",
(char*) "test_case_name", (cester_test_test_case_name), NULL, NULL, CESTER_NORMAL_TEST }
}
Final Expansion
static void cester_test_test_case_name(TestInstance* test_instance) {
cester_assert_nothing();
}
Therefore after the final expansion the source code will have transformed to this
static void cester_test_test_case_name(TestInstance* test_instance);
static TestCase cester_test_cases[] = {
{ CESTER_RESULT_UNKNOWN, __LINE__, CESTER_RESULT_SUCCESS, 0.000, 0.000, (char*) "",
(char*) #x, (cester_test_test_case_name), NULL, NULL, CESTER_NORMAL_TEST }
}
static void cester_test_test_case_name(TestInstance* test_instance) {
cester_assert_nothing();
}
Work the same as the CESTER_TEST macro but the test type is CESTER_NORMAL_TODO_TEST. Test case declared with this macro is not executed but it reported as todo test.
CESTER_TODO_TEST(a_test_to_implement, test_instance,
)
This macro is most useful for Test Driven Development (TDD). It create test cases for ops that has not been implemented yet.
Work the same as the CESTER_TEST macro but the test type is CESTER_NORMAL_SKIP_TEST. Test case declared with this macro is not executed but it reported as skipped test.
CESTER_SKIP_TEST(a_test_to_implement, test_instance,
)
There are various reason to creat a test with this macro, an example is running the test file in an environment where the resources needed by the test case is not available.
The function to setup the test resources before running the test case. It accepts the TestInstance object as the first parameter. The TestInstance parameter can be used to share data and object between the test cases.
CESTER_BEFORE_ALL(test_instance,
test_instance->arg = "beforeall";
)
CESTER_TEST(test_case, test_instance,
cester_assert_str_equal((char*)(test_instance->arg), "beforeall");
)
Note
If you have any of CESTER_BEFORE_ALL, CESTER_BEFORE_EACH, CESTER_AFTER_ALL and CESTER_AFTER_EACH. Your test cases will be executed on a single process to allow shared objects.
Use the macro to create a function that is invoked before each test case is executed. It accepts the TestInstance object as the first parameter, the current test name as the second parameter, the test case index as the third parameter.
CESTER_BEFORE_EACH(test_instance, test_name, index,
if (test_name == "test_xml") {
test_instance->arg = (void*) fopen("./result.xml", "r");
} else if (test_name == "test_json") {
test_instance->arg = (void*) fopen("./result.json", "r");
}
)
This macro can be used to setup resources according to the test case.
Note
If you have any of CESTER_BEFORE_ALL, CESTER_BEFORE_EACH, CESTER_AFTER_ALL and CESTER_AFTER_EACH. Your test cases will be executed on a single process to allow shared objects.
Use the macro to create a function that is invoked after each test case is executed. It accepts the TestInstance object as the first parameter, the current test name as the second parameter, the test case index as the third parameter.
CESTER_AFTER_EACH(test_instance, test_name, index,
if (test_name == "test_xml" || test_name == "test_json") {
fclose((FILE*)(test_instance->arg));
}
)
This macro can be used to tear down resources according to the test case.
Note
If you have any of CESTER_BEFORE_ALL, CESTER_BEFORE_EACH, CESTER_AFTER_ALL and CESTER_AFTER_EACH. Your test cases will be executed on a single process to allow shared objects.
The function to tear down the test resources after running the test case. It accepts the TestInstance object as the first parameter. This macro should be used to clean up shared object and close opened resources.
CESTER_AFTER_ALL(test_instance,
free(test_instance->arg);
)
Note
If you have any of CESTER_BEFORE_ALL, CESTER_BEFORE_EACH, CESTER_AFTER_ALL and CESTER_AFTER_EACH. Your test cases will be executed on a single process to allow shared objects.
This macro should be used to set the options accepted by cester. The function is executed before any of the setup functions and the test case.
CESTER_OPTIONS(
CESTER_NOCOLOR();
CESTER_NO_ISOLATION();
CESTER_OUTPUT_JUNITXML();
)
A multiline comment macro everything in the macro is printed out at the beginning of the test. Comma in the content will cause issue in the macro expansion when the code is compiled with C version less than C99. It should not begin or end in quote, escape characters is expanded when printed out
CESTER_COMMENT(
This is a multiline comment in \n
the source code this text is printed out \n
before any test cases is executed.
)
Always mark the test as success even if the test cases failed or an error occur while running the test.
CESTER_OPTIONS(
CESTER_REPORT_SUCCESS_REGARDLESS();
)
Always mark the test as failure even if the test cases passes and no error occur.
CESTER_OPTIONS(
CESTER_REPORT_FAILURE_REGARDLESS();
)
Manually register a test case. For a compiler that does not have the __BASE_FILE__ macro defined the test case won’t be registered at compile time, the test cases can be registered at runtime using this macro.
CESTER_TEST(test_case_name, test_instance,
cester_assert_nothing();
)
CESTER_BODY(
int main(int argc, char** argv) {
CESTER_REGISTER_TEST(test_case_name);
return CESTER_RUN_ALL_TESTS(argc, argv);
}
)
Manually register a test case as a skip test which cases the test case not to run but it will be reported in result and logged under skipped tests.
CESTER_SKIP_TEST(test_case_name, test_instance,
cester_assert_nothing();
)
CESTER_BODY(
int main(int argc, char** argv) {
CESTER_REGISTER_SKIP_TEST(test_case_name);
return CESTER_RUN_ALL_TESTS(argc, argv);
}
)
Manually register a test case that is yet to be implemented so it will be skipped but it will be reported in result and logged under todo tests.
CESTER_TODO_TEST(test_case_name, test_instance,
cester_assert_nothing();
)
CESTER_BODY(
int main(int argc, char** argv) {
CESTER_REGISTER_TODO_TEST(test_case_name);
return CESTER_RUN_ALL_TESTS(argc, argv);
}
)
Manually notify cester to execute the CESTER_BEFORE_ALL function before all the test case are run.
CESTER_BODY(
int main(int argc, char** argv) {
CESTER_REGISTER_BEFORE_ALL();
return CESTER_RUN_ALL_TESTS(argc, argv);
}
)
Warning
The test cases registered at compile time are ignored if at least one test case is registered at run time with this macro.
Manually notify cester to execute the CESTER_AFTER_ALL function after all the test case are run.
CESTER_BODY(
int main(int argc, char** argv) {
CESTER_REGISTER_AFTER_ALL();
return CESTER_RUN_ALL_TESTS(argc, argv);
}
)
Warning
The test cases registered at compile time are ignored if at least one test case is registered at run time with this macro.
Manually notify cester to execute the CESTER_BEFORE_EACH function before each of the test case are run.
CESTER_BODY(
int main(int argc, char** argv) {
CESTER_REGISTER_BEFORE_EACH();
return CESTER_RUN_ALL_TESTS(argc, argv);
}
)
Warning
The test cases registered at compile time are ignored if at least one test case is registered at run time with this macro.
Manually notify cester to execute the CESTER_AFTER_EACH function after each of the test case are run.
CESTER_BODY(
int main(int argc, char** argv) {
CESTER_REGISTER_AFTER_EACH();
return CESTER_RUN_ALL_TESTS(argc, argv);
}
)
Warning
The test cases registered at compile time are ignored if at least one test case is registered at run time with this macro.
Manually notify cester to execute the CESTER_OPTIONS function before running any test cases.
CESTER_BODY(
int main(int argc, char** argv) {
CESTER_REGISTER_OPTIONS();
return CESTER_RUN_ALL_TESTS(argc, argv);
}
)
Warning
The test cases registered at compile time are ignored if at least one test case is registered at run time with this macro.
Set the expected result of a test case. It accept the test case name as the first paramater, it accepts the expected result as the second parameter. The expected result mush be one of the enum types
CESTER_TEST(test_case_name, test_instance,
)
CESTER_OPTIONS(
CESTER_TEST_SHOULD(test_case_name, CESTER_RESULT_SUCCESS);
)
Change the expected result of a test case to Segfault. If the test segfault then it passes. If it does not segfault it is marked as failed. It accept the test case name as paramater.
CESTER_TEST(test_case_name, test_instance,
int *p = NULL;
*p=0xdead;
)
CESTER_OPTIONS(
CESTER_TEST_SHOULD_SEGFAULT(test_case_name);
)
Change the expected result of a test case to failure. If the test case passed then it marked as failure. If it failed then it consider as passed. It accept the test case name as paramater.
CESTER_TEST(test_case_name, test_instance,
cester_assert_not_equal(NULL, ((void*)0));
)
CESTER_OPTIONS(
CESTER_TEST_SHOULD_FAIL(test_case_name);
)
Change the expected test case result. If the test case is terminated by user or another program then it passes ortherwise it fails. It accept the test case name as paramater.
CESTER_TEST(test_case_name, test_instance,
while (1) {
}
)
CESTER_OPTIONS(
CESTER_TEST_SHOULD_BE_TERMINATED(test_case_name);
)
Change the expected test case result. If the test case does not leak any memory then the test case is marked as failure. It accept the test case name as paramater.
CESTER_TEST(test_case_name, test_instance,
char* str = (char*) malloc(sizeof(char) * 12);
)
CESTER_OPTIONS(
CESTER_TEST_SHOULD_LEAK_MEMORY(test_case_name);
)
Note
This macro is not available if the macro CESTER_NO_MEM_TEST is defined
Change the expected test case result. If the test case does not have any unreleased stream or no stream was captured the test case is marked as failure. It accept the test case name as paramater.
CESTER_TEST(test_stream_capture, test_instance,
CESTER_CAPTURE_STREAM(stdout);
)
CESTER_OPTIONS(
CESTER_TEST_SHOULD_NOT_RELEASE_STREAM(test_case_name);
)
Note
This macro is not available if the macro CESTER_NO_MEM_TEST is defined
Define the macro CESTER_NO_MOCK to prevent mocking functions in the test. Even if mocked function are defined. This is not a predefined macro it has to be defined in the test source before including cester.h.
Mock a function that simply return a value. The macro accepts three parameters, the function identifier and parameters, the return type and the value to return when the function is invoked. For example mocking a function that return an integer:
int multiply_by() {
return 2;
}
To function above returns 2, the macro CESTER_MOCK_SIMPLE_FUNCTION can mock this function to return 5 instead of two:
CESTER_MOCK_SIMPLE_FUNCTION(multiply_by(void), int, 5)
The macro above expands to the following code
//...
__attribute__((weak)) int multiply_by(); int __real_multiply_by();
//...
int __wrap_multiply_by() { return 5; }
This macro is similar to the CESTER_MOCK_SIMPLE_FUNCTION macro but instead of return a single value it accepts function body as the third argument. It accepts three parameters, the first is the function name and parameters, the second is the return type and the thrid is the function body. For example the function below does not return the correct power of value of an integer:
int power_of(int* num1, int num2) {
return *num1 * num2;
}
We then mock the function in the snippet below to properly calculate the power of an integer:
CESTER_MOCK_FUNCTION(power_of(int* num1, int num2), int,
int return_val = *num1;
while (num2-->1) {
return_val *= *num1;
}
return return_val;
)
The macro above expands to the following code
//...
__attribute__((weak)) int power_of(int* num1, int num2); int __real_power_of(int* num1, int num2);
//...
int __wrap_power_of(int* num1, int num2) {
int return_val = *num1;
while (num2-->1) {
return_val *= *num1;
}
return return_val;
}
Use this macro to prepare a file stream to be captured by libcester, if a stream is not captured all assertion will fail for that stream. This simply make the stream address to point to another stream that is accessible and can be freely modified by cester.
CESTER_TEST(test_stream_capture, test_instance,
CESTER_CAPTURE_STREAM(stdout);
//...
CESTER_RELEASE_STREAM(stdout);
)
If the stream that the output is written into (stdout by default) is captured if it not released at the end of the test case it will be forcefully released to libcester can continue writing the to the stream to see the test results.
Note
This macro is not available if the macro CESTER_NO_STREAM_CAPTURE is defined
Release a stream that has been captured by cester, this simply change the address of the stream to point to the original stream value, and the temporary stream used for testing is deleted from the file system. It very important to release a captured stream else stream might not give expected output if used in other test case, and if recaptured the original stream is lost forever in memory.
Always write the release statement at the point o capture and place the code in between. If you fail to release captured stream the test case will fail with CESTER_RESULT_UNRELEASED_STREAM error.
Note
This macro is not available if the macro CESTER_NO_STREAM_CAPTURE is defined
Remove all the existing data written to a stream so new data is not appended.
Note
This macro is not available if the macro CESTER_NO_STREAM_CAPTURE is defined
Change the folder used to hold the temporary files used for the streams captured. It accept the folder as the only parameter, the folder must exist on the file system and must be readable and writable.
The default location on windows is determined by the return value of getenv(“TEMP”) if not set the folder “C:/libcester_tmp/” is used. On other platforms the value of getenv(“TMPDIR”) is used and if not set the fallback folder is “/tmp/libcester_tmp/”.
Note
This macro is not available if the macro CESTER_NO_STREAM_CAPTURE is defined
Capture the stdout stream, alias for CESTER_CAPTURE_STREAM(stdout).
Note
This macro is not available if the macro CESTER_NO_STREAM_CAPTURE is defined
Release the stdout stream, alias for CESTER_RELEASE_STREAM(stdout).
Note
This macro is not available if the macro CESTER_NO_STREAM_CAPTURE is defined
Reset the stdout stream, alias for CESTER_RESET_STREAM(stdout).
Note
This macro is not available if the macro CESTER_NO_STREAM_CAPTURE is defined
Capture the stderr stream, alias for CESTER_CAPTURE_STREAM(stderr).
Note
This macro is not available if the macro CESTER_NO_STREAM_CAPTURE is defined
Release the stderr stream, alias for CESTER_RELEASE_STREAM(stderr).
Note
This macro is not available if the macro CESTER_NO_STREAM_CAPTURE is defined
Reset the stderr stream, alias for CESTER_RESET_STREAM(stderr).
Note
This macro is not available if the macro CESTER_NO_STREAM_CAPTURE is defined
Capture the stdin stream, alias for CESTER_CAPTURE_STREAM(stdin).
Note
This macro is not available if the macro CESTER_NO_STREAM_CAPTURE is defined
Release the stdin stream, alias for CESTER_RELEASE_STREAM(stdin).
Note
This macro is not available if the macro CESTER_NO_STREAM_CAPTURE is defined
Reset the stdin stream, alias for CESTER_RESET_STREAM(stdin).
Note
This macro is not available if the macro CESTER_NO_STREAM_CAPTURE is defined
Define this macro before including cester.h to use your own custom main function. This still requires you to put your main function in the CESTER_BODY macro. To run the tests invoke the macro CESTER_RUN_ALL_TESTS(argc, argv) with the two main function parameter.
Defining this macro will remove the printing test information feature during compilation step, the flag –cester-info=, function cester_str_size will not be available at runtime. This is not a predefined macro it has to be defined in the test source before including cester.h.
This macro wraps codes in test file to prevent generating the codes more than once. Any code block not in one of the test macros should be placed in this macro. Putting code in the file without wraping it with CESTER_BODY will cause issues during compilation.
The following code will cause compilation issues.
#include <exotic/cester.h>
int an_integer = 20;
The solution is to wrap the statement in CESTER_BODY
#include <exotic/cester.h>
CESTER_BODY(
int an_integer = 20;
)
Another good example is defining the entry point for the test instead of relying on cester main function. After defining the CESTER_NO_MAIN your main function must be wrapped inside CESTER_BODY macro to avoid error.
#define CESTER_NO_MAIN
#include <exotic/cester.h>
CESTER_BEFORE_ALL(test_instance,
cester_assert_equal(1, 1);
)
CESTER_BODY(
int main(int argc, char **argv) {
return CESTER_RUN_ALL_TESTS(argc, argv);
}
)
Any code written inside the CESTER_BODY macro will be dumb in the file global scope. This macro should not contain other CESTER_ macros.
Define the macro CESTER_NO_MEM_TEST to disable cester memory allocation test at compile time. The option –cester-nomemtest can be applied to the command line to stop mem test at runtime. This is not a predefined macro it has to be defined in the test source before including cester.h.
Define the macro CESTER_NO_STREAM_CAPTURE to disable cester stream capturing and assertion test at compile time. The option –cester-nostreamcapture can be applied to the command line to stop stream captuing at runtime. This is not a predefined macro it has to be defined in the test source before including cester.h.
Disable error handling with signal.h
Define the macro CESTER_NO_TIME to disable time keeping and floating point in your test. This comes handy in microcontrollers which does not have a proper time keeping library and has no floating point types support (but likely using compiler older than C90).
This macro was added in PR 14 by nicolasnoble.
Define the macro CESTER_EXCLUDE_WINDOWS_H to prevent the inclusion of the windows.h header file. Use this to get around conflicts in the windows API and your test.
Change the stream where the ouput is written to, the default value is the standard output stdout, the macro accepts one argument which is the stream to use. The example below shows how to write the test result to a file ‘result.txt’:
CESTER_BEFORE_ALL(test_instance,
FILE* output = fopen("./result.txt", "w+");
CESTER_CHANGE_STREAM(output);
)
The result of the test will be written into the file ‘result.txt’. The ouput stream changes at which point the macro is used, hence output written before changing the stream will remain and the new output will be written to the new stream.
CESTER_NO_COLOR()
Prevent displaying the output with colors. This option can prevent writing extra bytes into an output file in place of the color values. The option can also be set from the cli using –cester-nocolor.
CESTER_VERBOSE_LEVEL(3)
Change the verbose level of the output, the higher the velue the more the information printed into the terminal. 0 value means no output apart from the testcase’s and value 4 and above prints the full path to the test file. The option can also be set from the cli using –cester-verbose-level=3.
The verbose level ranges from 0 to 10, zero being the lowest and 10 the highest. With verbose level of zero no information is printed in the terminal, with 10 all possible information is printed in the terminal. 3 is the best value to print out meaningful details.
CESTER_PRINT_ERROR_ONLY()
Print only error in the terminal, ignore assertions, evaluation result.
CESTER_MINIMAL()
Print minimal info into the output stream. With this option set the expression evaluated will not be printed in the result output. The option can also be set from the cli using –cester-minimal.
This is equivaluent to setting the verbose level to the lowest value –cester-verbose-level=0. No information is printed into the terminal.
CESTER_VERBOSE()
Print as much info as possible into the output stream. With this option set both passed and failed expression evaluated will be printed in the result. The option can also be set from the cli using –cester-verbose.
This is equivaluent to setting the verbose level to the highest value –cester-verbose-level=10. All possible information is printed into the terminal with the full path of the test file.
CESTER_PRINT_VERSION()
Print cester version before running any test. The option can also be set from the cli using –cester-printversion.
CESTER_SINGLE_OUPUT_ONLY()
Display test for a single test case only, skip syntesis and summary. This option can also be set from the command line with –cester-singleoutput
CESTER_NO_ISOLATION()
Do not isolate the tests, run each of the test cases in a single process. The drawback is if a test case causes segfault or crash the entire test crashes and no summary is displayed. No isolation causes a crash one crash all scenerio.
This option can also be set from the command line with –cester-noisolation
CESTER_NO_MEMTEST()
Disable memory leak detection test. This option can also be set from the command line with –cester-nomemtest
CESTER_DO_MEMTEST()
Enable memory allocation. The combination of CESTER_NO_MEMTEST() and CESTER_DO_MEMTEST() is valid only in non isolated tests.
This togle combined with CESTER_NO_MEMTEST() can be used to selectively test memory allocation in a test e.g. Calling CESTER_NO_MEMTEST() before a test case will prevent memory test from the beginning of that function and calling CESTER_DO_MEMTEST() at the end of the test case will ensure memory allocation will be validated in all the other test case that follows.
CESTER_NO_STREAMCAPTURE()
Disable memory leak detection test. This option can also be set from the command line with –cester-nomemtest
CESTER_DO_STREAMCAPTURE()
Enable stream capture features. The combination of CESTER_NO_STREAMCAPTURE() and CESTER_DO_STREAMCAPTURE() is valid only in both isolated and non isolated tests.
This togle combined with CESTER_NO_STREAMCAPTURE() can be used to selectively test stream capture and content in a test e.g. Calling CESTER_NO_STREAMCAPTURE() before a test case will prevent stream capturing from the beginning of that function and calling CESTER_DO_STREAMCAPTURE() at the end of the test case will ensure stream capturing will be enabled in all the other test case that follows.
CESTER_OUTPUT_TEXT()
Change the output format to text. This option can also be set from the command line with –cester-format=text
CESTER_OUTPUT_JUNITXML()
Change the output format to junitxml. This option can also be set from the command line with –cester-format=junitxml
CESTER_OUTPUT_TAP()
Change the output format to TAP (Test Anything Protocol). This option can also be set from the command line with –cester-format=tap
CESTER_OUTPUT_TAPV13()
Change the output format to TAP (Test Anything Protocol) Version 13. This option can also be set from the command line with –cester-format=tapV13
CESTER_FORMAT_TESTNAME()
Format the test case name for output. E.g the test name modify_test_instance becomes modify test instance. This does not apply to junitxml as the test name remain the way it declared in the test source.
CESTER_DONT_FORMAT_TESTNAME()
Do not format the test case name, it remain the way it declared in the test source. This option can also be set from the command line with –cester-dontformatname
printf("Current Total Test %d", CESTER_TOTAL_TESTS_COUNT);
The total number of test that is registered at the time.
printf("Total Test Error %d", CESTER_TOTAL_TEST_ERRORS_COUNT);
The total number of errors that occur during the test. The errors is not tied to the test cases, the error is tied to cester fixtures, environment error and error that occur outside a test case.
Error that occur within a test case is reported for that test case
printf("Current Total Test Ran %d", CESTER_TOTAL_TESTS_RAN);
The total number of test that was executed. In cases where specific test cases is selected from the cli it value might not be equal the value of CESTER_TOTAL_TESTS_COUNT. If specific test cases is selected, if any of the selected test cases does not exist it will not count only the executed test cases counts.
printf("Failed Test %d", CESTER_TOTAL_FAILED_TESTS_COUNT);
The total number of test cases that fails from the total number of test cases at the time.
printf("Skipped %d tests\n", CESTER_TOTAL_TESTS_SKIPPED);
The number of test cases that was skipped at the time.
printf("%d test are passing \n", CESTER_TOTAL_PASSED_TESTS_COUNT);
The total number of test cases that passed from the total number of test cases at the time.
printf("%d todo tests are skipped \n", CESTER_TOTAL_TODO_TESTS);
The total number of todo test case skipped at the time.