On 10/6/22 16:41, Sadiya Kazi wrote:
Consider updating this section as below: In this section, you will learn how to write a program to test the addition of two numbers using KUnit. To do so, you must write the addition driver code followed by the test case.
1.To write the addition driver code, follow the steps given below:
a.Navigate to the Kernel repository.
b.Create a file ``drivers/misc/example.h``. c.In the ``example.h`` file, add the following code to declare the
function ``misc_example_add()``:
-1. Create a file ``drivers/misc/example.h``, which includes: +1. Write the feature that will be tested. First, write the declaration
- for ``misc_example_add()`` in ``drivers/misc/example.h``:
-.. code-block:: c
.. code-block:: c
int misc_example_add(int left, int right);
-2. Create a file ``drivers/misc/example.c``, which includes:
- Then implement the function in ``drivers/misc/example.c``:
d. To implement the function, create a file ``drivers/misc/example.c` and add the following code to it:
-.. code-block:: c
.. code-block:: c
#include <linux/errno.h>
@@ -152,24 +154,25 @@ In your kernel repository, let's add some code that we can test. return left + right; }
-3. Add the following lines to ``drivers/misc/Kconfig``: +2. Add Kconfig menu entry for the feature to ``drivers/misc/Kconfig``:
e. Update ``drivers/misc/Kconfig`` with the following code to add the driver configuration:
-.. code-block:: kconfig
.. code-block:: kconfig
config MISC_EXAMPLE bool "My example"
-4. Add the following lines to ``drivers/misc/Makefile``: +3. Add the kbuild goal that will build the feature to
- ``drivers/misc/Makefile``:
f.To build the feature, update ``drivers/misc/Makefile`` with the following code:
-.. code-block:: make
.. code-block:: make
obj-$(CONFIG_MISC_EXAMPLE) += example.o
Now we are ready to write the test cases.
- To write the test cases, follow the steps given below:
-1. Add the below test case in ``drivers/misc/example_test.c``: +1. Write the test in ``drivers/misc/example_test.c``:
a. Write the test in ``drivers/misc/example_test.c``:
-.. code-block:: c
.. code-block:: c
#include <kunit/test.h> #include "example.h"
@@ -202,31 +205,32 @@ Now we are ready to write the test cases. }; kunit_test_suite(misc_example_test_suite);
-2. Add the following lines to ``drivers/misc/Kconfig``: +2. Add following Kconfig entry for the test to ``drivers/misc/Kconfig``:
b. Add the following test configuration to ``drivers/misc/Kconfig``:
-.. code-block:: kconfig
.. code-block:: kconfig
config MISC_EXAMPLE_TEST tristate "Test for my example" if !KUNIT_ALL_TESTS depends on MISC_EXAMPLE && KUNIT=y default KUNIT_ALL_TESTS
-3. Add the following lines to ``drivers/misc/Makefile``: +3. Add kbuild goal of the test to ``drivers/misc/Makefile``:
c. Update ``drivers/misc/Makefile`` with the following code:
-.. code-block:: make
.. code-block:: make
obj-$(CONFIG_MISC_EXAMPLE_TEST) += example_test.o
-4. Add following configuration fragments to ``.kunit/.kunitconfig``: +4. Add following configuration fragments for the test to
- ``.kunit/.kunitconfig``:
d. Add the following test configuration to ``.kunit/.kunitconfig``:
-.. code-block:: none
.. code-block:: none
CONFIG_MISC_EXAMPLE=y CONFIG_MISC_EXAMPLE_TEST=y
- Run the test:
e. Run the test using the following command:
-.. code-block:: bash
.. code-block:: bash
./tools/testing/kunit/kunit.py run
I think the documentation assumes the knowledge of writing kernel code (C language and build infrastructure). This means that the instructions should be written for brevity.