Skip to content

Give the unit tests a big overhaul [v9.18]

Ondřej Surý requested to merge ondrej-unit-tests-refactoring-v9_18 into v9_18

The unit tests contain a lot of duplicated code and here's an attempt to reduce code duplication.

This commit does several things:

  1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake conditionals.

  2. Create a set of ISC_TEST_* and ISC_*TEST macros to wrap the test implementations, test lists, and the main test routine, so we don't have to repeat this all over again. The macros were modeled after libuv test suite but adapted to cmocka as the test driver.

    A simple example of a unit test would be:

    ISC_RUN_TEST_IMPL(test1) { assert_true(true); }

    ISC_TEST_LIST_START ISC_TEST_ENTRY(test1) ISC_TEST_LIST_END

    ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)

    For more complicated examples including group setup and teardown functions, and per-test setup and teardown functions.

  3. The macros prefix the test functions and cmocka entries, so the name of the test can now match the tested function name, and we don't have to append _test because run_test_ is automatically prepended to the main test function, and setup_test_ and teardown_test_ is prepended to setup and teardown function.

  4. Update all the unit tests to use the new syntax and fix a few bits here and there.

  5. In the future, we can separate the test declarations and test implementations which are going to greatly help with uncluttering the bigger unit tests like doh_test and netmgr_test, because the test implementations are not declared static (see ISC_RUN_TEST_DECLARE and ISC_RUN_TEST_IMPL for more details.

NOTE: This heavily relies on preprocessor macros, but the result greatly outweighs all the negatives of using the macros. There's less duplicated code, the tests are more uniform and the implementation can be more flexible.

(cherry picked from commit 63fe9312)

Backport of !6243 (merged)

Edited by Ondřej Surý

Merge request reports