Skip to content

Commit d208025

Browse files
rmr167shuahkh
authored andcommitted
Documentation: kunit: improve example on testing static functions
The documentation on testing static functions using the KUnit macros VISIBLE_IF_KUNIT and EXPORT_SYMBOL_IF_KUNIT is lacking clarity and missing key steps in the example. This has caused bugs and confusion among developers. Improve wording of description and add missing steps to the example. This entails adding the "#include <kunit/visibility.h>" line and the "MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING);" line. Both of which were missing from the original example and key to exposing static functions. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Rae Moar <[email protected]> Reviewed-by: David Gow <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent c249338 commit d208025

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

Documentation/dev-tools/kunit/usage.rst

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -670,28 +670,50 @@ with ``kunit_remove_action``.
670670
Testing Static Functions
671671
------------------------
672672

673-
If we do not want to expose functions or variables for testing, one option is to
674-
conditionally export the used symbol. For example:
673+
If you want to test static functions without exposing those functions outside of
674+
testing, one option is conditionally export the symbol. When KUnit is enabled,
675+
the symbol is exposed but remains static otherwise. To use this method, follow
676+
the template below.
675677

676678
.. code-block:: c
677679
678-
/* In my_file.c */
680+
/* In the file containing functions to test "my_file.c" */
679681
680-
VISIBLE_IF_KUNIT int do_interesting_thing();
682+
#include <kunit/visibility.h>
683+
#include <my_file.h>
684+
...
685+
VISIBLE_IF_KUNIT int do_interesting_thing()
686+
{
687+
...
688+
}
681689
EXPORT_SYMBOL_IF_KUNIT(do_interesting_thing);
682690
683-
/* In my_file.h */
691+
/* In the header file "my_file.h" */
684692
685693
#if IS_ENABLED(CONFIG_KUNIT)
686694
int do_interesting_thing(void);
687695
#endif
688696
689-
Alternatively, you could conditionally ``#include`` the test file at the end of
690-
your .c file. For example:
697+
/* In the KUnit test file "my_file_test.c" */
698+
699+
#include <kunit/visibility.h>
700+
#include <my_file.h>
701+
...
702+
MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING);
703+
...
704+
// Use do_interesting_thing() in tests
705+
706+
For a full example, see this `patch <https://lore.kernel.org/all/[email protected]/>`_
707+
where a test is modified to conditionally expose static functions for testing
708+
using the macros above.
709+
710+
As an **alternative** to the method above, you could conditionally ``#include``
711+
the test file at the end of your .c file. This is not recommended but works
712+
if needed. For example:
691713

692714
.. code-block:: c
693715
694-
/* In my_file.c */
716+
/* In "my_file.c" */
695717
696718
static int do_interesting_thing();
697719

0 commit comments

Comments
 (0)