usage.rst (56778b49c9a2cbc32c6b0fbd3ba1a9d64192d3af) usage.rst (d03c720e03bd9bf0b784d80b5d3ede7e2daf3b6e)
1.. SPDX-License-Identifier: GPL-2.0
2
3Writing Tests
4=============
5
6Test Cases
7----------
8

--- 783 unchanged lines hidden (view full) ---

792 #else
793 static void my_debug_function(void) { }
794 #endif
795
796``kunit_fail_current_test()`` is safe to call even if KUnit is not enabled. If
797KUnit is not enabled, or if no test is running in the current task, it will do
798nothing. This compiles down to either a no-op or a static key check, so will
799have a negligible performance impact when no test is running.
1.. SPDX-License-Identifier: GPL-2.0
2
3Writing Tests
4=============
5
6Test Cases
7----------
8

--- 783 unchanged lines hidden (view full) ---

792 #else
793 static void my_debug_function(void) { }
794 #endif
795
796``kunit_fail_current_test()`` is safe to call even if KUnit is not enabled. If
797KUnit is not enabled, or if no test is running in the current task, it will do
798nothing. This compiles down to either a no-op or a static key check, so will
799have a negligible performance impact when no test is running.
800
801Managing Fake Devices and Drivers
802---------------------------------
803
804When testing drivers or code which interacts with drivers, many functions will
805require a ``struct device`` or ``struct device_driver``. In many cases, setting
806up a real device is not required to test any given function, so a fake device
807can be used instead.
808
809KUnit provides helper functions to create and manage these fake devices, which
810are internally of type ``struct kunit_device``, and are attached to a special
811``kunit_bus``. These devices support managed device resources (devres), as
812described in Documentation/driver-api/driver-model/devres.rst
813
814To create a KUnit-managed ``struct device_driver``, use ``kunit_driver_create()``,
815which will create a driver with the given name, on the ``kunit_bus``. This driver
816will automatically be destroyed when the corresponding test finishes, but can also
817be manually destroyed with ``driver_unregister()``.
818
819To create a fake device, use the ``kunit_device_register()``, which will create
820and register a device, using a new KUnit-managed driver created with ``kunit_driver_create()``.
821To provide a specific, non-KUnit-managed driver, use ``kunit_device_register_with_driver()``
822instead. Like with managed drivers, KUnit-managed fake devices are automatically
823cleaned up when the test finishes, but can be manually cleaned up early with
824``kunit_device_unregister()``.
825
826The KUnit devices should be used in preference to ``root_device_register()``, and
827instead of ``platform_device_register()`` in cases where the device is not otherwise
828a platform device.
829
830For example:
831
832.. code-block:: c
833
834 #include <kunit/device.h>
835
836 static void test_my_device(struct kunit *test)
837 {
838 struct device *fake_device;
839 const char *dev_managed_string;
840
841 // Create a fake device.
842 fake_device = kunit_device_register(test, "my_device");
843 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fake_device)
844
845 // Pass it to functions which need a device.
846 dev_managed_string = devm_kstrdup(fake_device, "Hello, World!");
847
848 // Everything is cleaned up automatically when the test ends.
849 }