From b20f5932781fd08f338f06c3ba5e17cc7d6d17d5 Mon Sep 17 00:00:00 2001 From: Valery Ivanov Date: Thu, 24 Feb 2022 17:07:30 +0300 Subject: tests: add initial support of unit testing Signed-off-by: Valery Ivanov --- Makefile.am | 4 ++++ README.md | 19 +++++++++++++++++++ configure.ac | 12 ++++++++++++ tests/Makefile.am | 7 +++++++ tests/unit_tests.c | 21 +++++++++++++++++++++ 5 files changed, 63 insertions(+) create mode 100644 tests/Makefile.am create mode 100644 tests/unit_tests.c diff --git a/Makefile.am b/Makefile.am index 9e85669..1ac770c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -17,6 +17,10 @@ if ENABLE_SAMPLES SUBDIRS += sample endif +if ENABLE_TESTS +SUBDIRS += tests +endif + if ENABLE_DOXYGEN SUBDIRS += doc diff --git a/README.md b/README.md index 361fedd..6db0a98 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,25 @@ docker run -it --rm -v $(pwd):$(pwd) --workdir=$(pwd) libnet-builder make ``` +### Running Unit Tests with CMocka +```bash +# Running tests in the building Docker ^ + +./autogen.sh +./configure --enable-tests +make +./tests/libnet_unit_tests + +# Approximate output: +builder$ ./tests/libnet_unit_tests +[==========] Running 1 test(s). +[ RUN ] bool_always_success +[ OK ] bool_always_success +[==========] 1 test(s) run. +[ PASSED ] 1 test(s). +builder$ +``` + ### Building the Documentation To build the documentation (optional) you need doxygen and pod2man: diff --git a/configure.ac b/configure.ac index 199cf5b..c3ffa26 100644 --- a/configure.ac +++ b/configure.ac @@ -40,6 +40,7 @@ AC_CONFIG_FILES([Makefile \ libnet.pc \ src/Makefile \ sample/Makefile \ + tests/Makefile \ win32/Makefile]) AC_CONFIG_FILES([doc/fixmanpages], [chmod a+x doc/fixmanpages]) @@ -177,6 +178,16 @@ AC_ARG_ENABLE([samples], AC_MSG_RESULT([$enable_samples]) AM_CONDITIONAL([ENABLE_SAMPLES], [test "$enable_samples" = "yes"]) +# Check for tests enabling +AC_MSG_CHECKING([enable tests]) +AC_ARG_ENABLE([tests], + [AS_HELP_STRING([--enable-tests],[do not enable tests @<:@default=no@:>@])], + [enable_tests=$enableval], + [enable_tests=no] +) +AC_MSG_RESULT([$enable_tests]) +AM_CONDITIONAL([ENABLE_TESTS], [test "$enable_tests" = "yes"]) + # what (not) to do if the user disables shared libraries AM_CONDITIONAL([COND_SHARED], [test "x$enable_shared" != xno]) @@ -375,6 +386,7 @@ AC_MSG_RESULT([ PIC ........................... ${pic_mode} Build Sample Programs ......... ${enable_samples} Rebuild docs .................. ${rebuild_docs} + Tests.......................... ${enable_tests} To override options diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 0000000..fbd5972 --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,7 @@ +bin_PROGRAMS = libnet_unit_tests + +libnet_unit_tests_SOURCES = unit_tests.c + +libnet_unit_tests_LDFLAGS = -lcmocka + +LDADD = $(top_builddir)/src/libnet.la \ No newline at end of file diff --git a/tests/unit_tests.c b/tests/unit_tests.c new file mode 100644 index 0000000..45ad926 --- /dev/null +++ b/tests/unit_tests.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +/* A test case that does nothing and succeeds. */ +static void bool_always_success(void **state) { + (void) state; /* unused */ + + bool this_is_true = true; + + assert_true(this_is_true); +} + +int main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(bool_always_success), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} -- cgit v1.2.1