Documentation/dev-tools/kunit/usage.rst
Source file repositories/reference/linux-study-clean/Documentation/dev-tools/kunit/usage.rst
File Facts
- System
- Linux kernel
- Corpus path
Documentation/dev-tools/kunit/usage.rst- Extension
.rst- Size
- 43999 bytes
- Lines
- 1259
- Domain
- Support Tooling And Documentation
- Bucket
- Documentation
- Inferred role
- Support Tooling And Documentation: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
kunit/visibility.hmy_file.hmy_kunit_test.ckunit/test-bug.hkunit/device.h
Detected Declarations
struct shapestruct rectanglestruct eepromstruct eeprom_bufferstruct fake_eepromstruct eeprom_buffer_teststruct sha1_test_casestruct sha1_test_casestruct test_datafunction example_test_successfunction add_test_basicfunction add_test_basicfunction add_test_negativefunction add_test_maxfunction add_test_overflowfunction test_sortfunction some_testfunction some_testfunction rectangle_areafunction rectangle_newfunction fake_eeprom_readfunction fake_eeprom_writefunction fake_eeprom_initfunction eeprom_buffer_test_does_not_write_until_flushfunction eeprom_buffer_test_flushes_after_flush_count_metfunction eeprom_buffer_test_flushes_increments_of_flush_countfunction eeprom_buffer_test_initfunction eeprom_buffer_test_exitfunction sha1_testfunction buffer_zero_testfunction param_initfunction managedfunction example_resource_alloc_matchfunction example_param_initfunction example_params_test_with_initfunction example_param_init_privfunction example_params_test_with_init_privfunction example_test_allocationfunction exitsfunction example_device_testfunction Integrityfunction test_only_hookfunction fake_foofunction example_simple_testfunction validate_my_datafunction my_debug_function
Annotated Snippet
require a ``struct device`` or ``struct device_driver``. In many cases, setting
up a real device is not required to test any given function, so a fake device
can be used instead.
KUnit provides helper functions to create and manage these fake devices, which
are internally of type ``struct kunit_device``, and are attached to a special
``kunit_bus``. These devices support managed device resources (devres), as
described in Documentation/driver-api/driver-model/devres.rst
To create a KUnit-managed ``struct device_driver``, use ``kunit_driver_create()``,
which will create a driver with the given name, on the ``kunit_bus``. This driver
will automatically be destroyed when the corresponding test finishes, but can also
be manually destroyed with ``driver_unregister()``.
To create a fake device, use the ``kunit_device_register()``, which will create
and register a device, using a new KUnit-managed driver created with ``kunit_driver_create()``.
To provide a specific, non-KUnit-managed driver, use ``kunit_device_register_with_driver()``
instead. Like with managed drivers, KUnit-managed fake devices are automatically
cleaned up when the test finishes, but can be manually cleaned up early with
``kunit_device_unregister()``.
The KUnit devices should be used in preference to ``root_device_register()``, and
instead of ``platform_device_register()`` in cases where the device is not otherwise
a platform device.
For example:
.. code-block:: c
#include <kunit/device.h>
static void test_my_device(struct kunit *test)
{
struct device *fake_device;
const char *dev_managed_string;
// Create a fake device.
fake_device = kunit_device_register(test, "my_device");
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fake_device)
// Pass it to functions which need a device.
dev_managed_string = devm_kstrdup(fake_device, "Hello, World!");
// Everything is cleaned up automatically when the test ends.
}
Annotation
- Immediate include surface: `kunit/visibility.h`, `my_file.h`, `my_kunit_test.c`, `kunit/test-bug.h`, `kunit/device.h`.
- Detected declarations: `struct shape`, `struct rectangle`, `struct eeprom`, `struct eeprom_buffer`, `struct fake_eeprom`, `struct eeprom_buffer_test`, `struct sha1_test_case`, `struct sha1_test_case`, `struct test_data`, `function example_test_success`.
- Atlas domain: Support Tooling And Documentation / Documentation.
- Implementation status: pattern implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.