summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorValery Ivanov <ivalery111@gmail.com>2022-02-24 17:07:30 +0300
committerValery Ivanov <ivalery111@gmail.com>2022-03-12 15:43:21 +0300
commitb20f5932781fd08f338f06c3ba5e17cc7d6d17d5 (patch)
treeb1085bc2824a75882e27a0a9713e8208cb30ca9d
parent7d4adfa4fb256b2944c287a4fd2cf8f7ced4ba09 (diff)
downloadlibnet-b20f5932781fd08f338f06c3ba5e17cc7d6d17d5.tar.gz
tests: add initial support of unit testing
Signed-off-by: Valery Ivanov <ivalery111@gmail.com>
-rw-r--r--Makefile.am4
-rw-r--r--README.md19
-rw-r--r--configure.ac12
-rw-r--r--tests/Makefile.am7
-rw-r--r--tests/unit_tests.c21
5 files changed, 63 insertions, 0 deletions
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 <stdio.h>
+#include <stdbool.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+/* 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);
+}