1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
Unit Testing
============
For Unit Testing [CMocka](https://cmocka.org/) is used. Unit Tests
resides in the `test/` subdirectory.
Example
------
This is an example of how to add a test for the completely made up
libnet function `libnet_build_foo()`.
First, we create a new file `test/foo.c` where we add a test function
`test_libnet_build_foo()`, which is where all the test logic goes:
```c
static void test_libnet_build_foo(void **state)
{
(void)state; /* unused */
...
assert_..._(...);
...
}
```
> **Tip:** For help on the various checks you can do, see the [CMocka
> Assert Macros](https://api.cmocka.org/group__cmocka__asserts.html).
Add the test function to `tests[]` to let CMocka run it. You can have
multiple test functions testing various aspects/APIs of a given module.
```c
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_libnet_build_foo),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
```
Second, add the new test file to `test/Makefile.am`, in the TESTS
variables, alphabetically:
```Makefile
...
AM_LDFLAGS = -lcmocka $(top_builddir)/src/libnet.la
TESTS = ethernet
TESTS += foo
TESTS += udld
...
```
Finally, compile and run. For details, see section [Running Unit Tests
with CMocka](../README.md#running-unit-tests-with-cmocka) in the README.
```bash
~/src/libnet(ethernet-test)$ make check
```
|