MereTest is a header file that provides test and assertion macros for C/C++. It aims to provide dead simple unit testing.
To install MereTest system-wide on Unix-based systems, simply run the following:
make install
Depending on your system you may need to be root before running that command. By default, it will install using /usr/local
as the prefix. This can be changed easily inside Makefile
.
Simply copy meretest.h somewhere in your project. If the file is placed in a separate directory outside of the location you will be placing your test source files, configure your build system to include it for your test source files.
Create a new source file that will hold your tests and include the header file.
#include <meretest.h>
Change the angular brackets to quotes if you simply placed meretest.h along with your test source files.
Then, start writing your tests using this format:
TEST(my_first_meretest) {
int i = 0;
int j = 0;
ASSERT_EQUAL(i, j)
}
Finally, create the entry point to your tests:
TEST_MAIN(
// List each test function here.
my_first_meretest,
another_test
)
The following is a functioning test and is all that it takes to get started with MereTest.
#include <meretest.h>
TEST(my_first_meretest) {
int i = 0;
int j = 0;
ASSERT_EQUAL(i, j)
}
TEST_MAIN(
my_first_meretest
)
Compiling and running it will yield these results:
Test my_first_meretest passed!
Results: 1 out of 1 test passed.
It is possible to have multiple test source files with only one entry point. Simply create a header file and source file like you normally would and prototype the test function in the header file.
-
second_test.h
#ifndef SECOND_TEST_H #define SECOND_TEST_H #include <meretest.h> TEST(my_second_meretest); #endif
-
second_test.c
#include "second_test.h" TEST(my_second_meretest) { // Test things here. }
-
first_test.c
#include <meretest.h> // Optional now since second_test.h provides it #include "second_test.h" TEST(my_first_meretest) { int i = 0; int j = 0; ASSERT_EQUAL(i, j) } TEST_MAIN( my_first_meretest, my_second_meretest )
Currently, there are only a few assertions available. These assertions are similar to MiniTest's.
-
ASSERT(test)
Asserts if
test
is true. -
ASSERT_EQUAL(exp, act)
Asserts if
exp
is equal toact
. -
ASSERT_NULL(test)
Asserts if
test
isNULL
.
It is possible to add the test
make target with CMake and have it work with MereTest. It is best not to use CMake's testing commands as those are more geared towards CTest.
If you're creating an executable, an object library target can be used in order to avoid compiling your main source files twice.
To do so, consider this example:
add_library(myproject_obj OBJECT
# Do not include the source file containing the entry point.
src/file1.c
src/file2.c
)
add_executable(myproject $<TARGET_OBJECTS:myproject_obj> src/main.c)
The source file containing the entry point (main
) is only provided to the executable. If it was compiled into the object library, it would result in a linking error regarding duplicate symbols when compiling the test source files as the tests have their own entry point. If you are unit testing functions that are inside the source file that contains the entry point, consider moving them outside of that source file.
Assuming that your test source files are in test/
, you will want to tell CMake to add the test/
subdirectory.
add_subdirectory(test)
Then inside test/
, create a CMakeLists.txt
file that provides targets for your tests. If you're building an executable, the object library will be referenced here as well. The test executable name cannot be the same name as the custom test
target.
-
For executables:
add_executable(myproject_test $<TARGET_OBJECTS:myproject_obj> test_main.c test1.c test2.c ) add_custom_target(test "${CMAKE_BINARY_DIR}/test/myproject_test" DEPENDS myproject_test)
-
For libraries:
add_executable(myproject_test test_main.c test1.c test2.c ) target_link_libraries(myproject_test myproject) add_custom_target(test "${CMAKE_BINARY_DIR}/test/project_test" DEPENDS myproject_test)