Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0 |
| 2 | |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 3 | Writing Tests |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 4 | ============= |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 5 | |
| 6 | Test Cases |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 7 | ---------- |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 8 | |
| 9 | The fundamental unit in KUnit is the test case. A test case is a function with |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 10 | the signature ``void (*)(struct kunit *test)``. It calls the function under test |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 11 | and then sets *expectations* for what should happen. For example: |
| 12 | |
| 13 | .. code-block:: c |
| 14 | |
| 15 | void example_test_success(struct kunit *test) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | void example_test_failure(struct kunit *test) |
| 20 | { |
| 21 | KUNIT_FAIL(test, "This test never passes."); |
| 22 | } |
| 23 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 24 | In the above example, ``example_test_success`` always passes because it does |
| 25 | nothing; no expectations are set, and therefore all expectations pass. On the |
| 26 | other hand ``example_test_failure`` always fails because it calls ``KUNIT_FAIL``, |
| 27 | which is a special expectation that logs a message and causes the test case to |
| 28 | fail. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 29 | |
| 30 | Expectations |
| 31 | ~~~~~~~~~~~~ |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 32 | An *expectation* specifies that we expect a piece of code to do something in a |
| 33 | test. An expectation is called like a function. A test is made by setting |
| 34 | expectations about the behavior of a piece of code under test. When one or more |
| 35 | expectations fail, the test case fails and information about the failure is |
| 36 | logged. For example: |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 37 | |
| 38 | .. code-block:: c |
| 39 | |
| 40 | void add_test_basic(struct kunit *test) |
| 41 | { |
| 42 | KUNIT_EXPECT_EQ(test, 1, add(1, 0)); |
| 43 | KUNIT_EXPECT_EQ(test, 2, add(1, 1)); |
| 44 | } |
| 45 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 46 | In the above example, ``add_test_basic`` makes a number of assertions about the |
| 47 | behavior of a function called ``add``. The first parameter is always of type |
| 48 | ``struct kunit *``, which contains information about the current test context. |
| 49 | The second parameter, in this case, is what the value is expected to be. The |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 50 | last value is what the value actually is. If ``add`` passes all of these |
| 51 | expectations, the test case, ``add_test_basic`` will pass; if any one of these |
Randy Dunlap | 873ddeb | 2020-10-28 10:43:19 -0700 | [diff] [blame] | 52 | expectations fails, the test case will fail. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 53 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 54 | A test case *fails* when any expectation is violated; however, the test will |
| 55 | continue to run, and try other expectations until the test case ends or is |
| 56 | otherwise terminated. This is as opposed to *assertions* which are discussed |
| 57 | later. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 58 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 59 | To learn about more KUnit expectations, see Documentation/dev-tools/kunit/api/test.rst. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 60 | |
| 61 | .. note:: |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 62 | A single test case should be short, easy to understand, and focused on a |
| 63 | single behavior. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 64 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 65 | For example, if we want to rigorously test the ``add`` function above, create |
| 66 | additional tests cases which would test each property that an ``add`` function |
| 67 | should have as shown below: |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 68 | |
| 69 | .. code-block:: c |
| 70 | |
| 71 | void add_test_basic(struct kunit *test) |
| 72 | { |
| 73 | KUNIT_EXPECT_EQ(test, 1, add(1, 0)); |
| 74 | KUNIT_EXPECT_EQ(test, 2, add(1, 1)); |
| 75 | } |
| 76 | |
| 77 | void add_test_negative(struct kunit *test) |
| 78 | { |
| 79 | KUNIT_EXPECT_EQ(test, 0, add(-1, 1)); |
| 80 | } |
| 81 | |
| 82 | void add_test_max(struct kunit *test) |
| 83 | { |
| 84 | KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX)); |
| 85 | KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN)); |
| 86 | } |
| 87 | |
| 88 | void add_test_overflow(struct kunit *test) |
| 89 | { |
| 90 | KUNIT_EXPECT_EQ(test, INT_MIN, add(INT_MAX, 1)); |
| 91 | } |
| 92 | |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 93 | Assertions |
| 94 | ~~~~~~~~~~ |
| 95 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 96 | An assertion is like an expectation, except that the assertion immediately |
| 97 | terminates the test case if the condition is not satisfied. For example: |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 98 | |
| 99 | .. code-block:: c |
| 100 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 101 | static void test_sort(struct kunit *test) |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 102 | { |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 103 | int *a, i, r = 1; |
| 104 | a = kunit_kmalloc_array(test, TEST_LEN, sizeof(*a), GFP_KERNEL); |
| 105 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a); |
| 106 | for (i = 0; i < TEST_LEN; i++) { |
| 107 | r = (r * 725861) % 6599; |
| 108 | a[i] = r; |
| 109 | } |
| 110 | sort(a, TEST_LEN, sizeof(*a), cmpint, NULL); |
| 111 | for (i = 0; i < TEST_LEN-1; i++) |
| 112 | KUNIT_EXPECT_LE(test, a[i], a[i + 1]); |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 115 | In this example, the method under test should return pointer to a value. If the |
| 116 | pointer returns null or an errno, we want to stop the test since the following |
| 117 | expectation could crash the test case. `ASSERT_NOT_ERR_OR_NULL(...)` allows us |
| 118 | to bail out of the test case if the appropriate conditions are not satisfied to |
| 119 | complete the test. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 120 | |
| 121 | Test Suites |
| 122 | ~~~~~~~~~~~ |
| 123 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 124 | We need many test cases covering all the unit's behaviors. It is common to have |
| 125 | many similar tests. In order to reduce duplication in these closely related |
| 126 | tests, most unit testing frameworks (including KUnit) provide the concept of a |
| 127 | *test suite*. A test suite is a collection of test cases for a unit of code |
| 128 | with a setup function that gets invoked before every test case and then a tear |
| 129 | down function that gets invoked after every test case completes. For example: |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 130 | |
| 131 | .. code-block:: c |
| 132 | |
| 133 | static struct kunit_case example_test_cases[] = { |
| 134 | KUNIT_CASE(example_test_foo), |
| 135 | KUNIT_CASE(example_test_bar), |
| 136 | KUNIT_CASE(example_test_baz), |
| 137 | {} |
| 138 | }; |
| 139 | |
| 140 | static struct kunit_suite example_test_suite = { |
| 141 | .name = "example", |
| 142 | .init = example_test_init, |
| 143 | .exit = example_test_exit, |
| 144 | .test_cases = example_test_cases, |
| 145 | }; |
| 146 | kunit_test_suite(example_test_suite); |
| 147 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 148 | In the above example, the test suite ``example_test_suite`` would run the test |
| 149 | cases ``example_test_foo``, ``example_test_bar``, and ``example_test_baz``. Each |
| 150 | would have ``example_test_init`` called immediately before it and |
| 151 | ``example_test_exit`` called immediately after it. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 152 | ``kunit_test_suite(example_test_suite)`` registers the test suite with the |
| 153 | KUnit test framework. |
| 154 | |
| 155 | .. note:: |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 156 | A test case will only run if it is associated with a test suite. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 157 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 158 | ``kunit_test_suite(...)`` is a macro which tells the linker to put the |
| 159 | specified test suite in a special linker section so that it can be run by KUnit |
| 160 | either after ``late_init``, or when the test module is loaded (if the test was |
| 161 | built as a module). |
Brendan Higgins | a82763e | 2020-08-04 13:47:45 -0700 | [diff] [blame] | 162 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 163 | For more information, see Documentation/dev-tools/kunit/api/test.rst. |
| 164 | |
| 165 | Writing Tests For Other Architectures |
| 166 | ------------------------------------- |
| 167 | |
| 168 | It is better to write tests that run on UML to tests that only run under a |
| 169 | particular architecture. It is better to write tests that run under QEMU or |
| 170 | another easy to obtain (and monetarily free) software environment to a specific |
| 171 | piece of hardware. |
| 172 | |
| 173 | Nevertheless, there are still valid reasons to write a test that is architecture |
| 174 | or hardware specific. For example, we might want to test code that really |
| 175 | belongs in ``arch/some-arch/*``. Even so, try to write the test so that it does |
| 176 | not depend on physical hardware. Some of our test cases may not need hardware, |
| 177 | only few tests actually require the hardware to test it. When hardware is not |
| 178 | available, instead of disabling tests, we can skip them. |
| 179 | |
| 180 | Now that we have narrowed down exactly what bits are hardware specific, the |
| 181 | actual procedure for writing and running the tests is same as writing normal |
| 182 | KUnit tests. |
| 183 | |
| 184 | .. important:: |
| 185 | We may have to reset hardware state. If this is not possible, we may only |
| 186 | be able to run one test case per invocation. |
| 187 | |
| 188 | .. TODO(brendanhiggins@google.com): Add an actual example of an architecture- |
| 189 | dependent KUnit test. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 190 | |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 191 | Common Patterns |
| 192 | =============== |
| 193 | |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 194 | Isolating Behavior |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 195 | ------------------ |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 196 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 197 | Unit testing limits the amount of code under test to a single unit. It controls |
| 198 | what code gets run when the unit under test calls a function. Where a function |
| 199 | is exposed as part of an API such that the definition of that function can be |
| 200 | changed without affecting the rest of the code base. In the kernel, this comes |
| 201 | from two constructs: classes, which are structs that contain function pointers |
| 202 | provided by the implementer, and architecture-specific functions, which have |
| 203 | definitions selected at compile time. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 204 | |
| 205 | Classes |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 206 | ~~~~~~~ |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 207 | |
| 208 | Classes are not a construct that is built into the C programming language; |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 209 | however, it is an easily derived concept. Accordingly, in most cases, every |
| 210 | project that does not use a standardized object oriented library (like GNOME's |
| 211 | GObject) has their own slightly different way of doing object oriented |
| 212 | programming; the Linux kernel is no exception. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 213 | |
| 214 | The central concept in kernel object oriented programming is the class. In the |
| 215 | kernel, a *class* is a struct that contains function pointers. This creates a |
| 216 | contract between *implementers* and *users* since it forces them to use the |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 217 | same function signature without having to call the function directly. To be a |
| 218 | class, the function pointers must specify that a pointer to the class, known as |
| 219 | a *class handle*, be one of the parameters. Thus the member functions (also |
| 220 | known as *methods*) have access to member variables (also known as *fields*) |
| 221 | allowing the same implementation to have multiple *instances*. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 222 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 223 | A class can be *overridden* by *child classes* by embedding the *parent class* |
| 224 | in the child class. Then when the child class *method* is called, the child |
| 225 | implementation knows that the pointer passed to it is of a parent contained |
| 226 | within the child. Thus, the child can compute the pointer to itself because the |
| 227 | pointer to the parent is always a fixed offset from the pointer to the child. |
| 228 | This offset is the offset of the parent contained in the child struct. For |
| 229 | example: |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 230 | |
| 231 | .. code-block:: c |
| 232 | |
| 233 | struct shape { |
| 234 | int (*area)(struct shape *this); |
| 235 | }; |
| 236 | |
| 237 | struct rectangle { |
| 238 | struct shape parent; |
| 239 | int length; |
| 240 | int width; |
| 241 | }; |
| 242 | |
| 243 | int rectangle_area(struct shape *this) |
| 244 | { |
| 245 | struct rectangle *self = container_of(this, struct shape, parent); |
| 246 | |
| 247 | return self->length * self->width; |
| 248 | }; |
| 249 | |
| 250 | void rectangle_new(struct rectangle *self, int length, int width) |
| 251 | { |
| 252 | self->parent.area = rectangle_area; |
| 253 | self->length = length; |
| 254 | self->width = width; |
| 255 | } |
| 256 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 257 | In this example, computing the pointer to the child from the pointer to the |
| 258 | parent is done by ``container_of``. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 259 | |
| 260 | Faking Classes |
| 261 | ~~~~~~~~~~~~~~ |
| 262 | |
| 263 | In order to unit test a piece of code that calls a method in a class, the |
| 264 | behavior of the method must be controllable, otherwise the test ceases to be a |
| 265 | unit test and becomes an integration test. |
| 266 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 267 | A fake class implements a piece of code that is different than what runs in a |
| 268 | production instance, but behaves identical from the standpoint of the callers. |
| 269 | This is done to replace a dependency that is hard to deal with, or is slow. For |
| 270 | example, implementing a fake EEPROM that stores the "contents" in an |
| 271 | internal buffer. Assume we have a class that represents an EEPROM: |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 272 | |
| 273 | .. code-block:: c |
| 274 | |
| 275 | struct eeprom { |
| 276 | ssize_t (*read)(struct eeprom *this, size_t offset, char *buffer, size_t count); |
| 277 | ssize_t (*write)(struct eeprom *this, size_t offset, const char *buffer, size_t count); |
| 278 | }; |
| 279 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 280 | And we want to test code that buffers writes to the EEPROM: |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 281 | |
| 282 | .. code-block:: c |
| 283 | |
| 284 | struct eeprom_buffer { |
| 285 | ssize_t (*write)(struct eeprom_buffer *this, const char *buffer, size_t count); |
| 286 | int flush(struct eeprom_buffer *this); |
| 287 | size_t flush_count; /* Flushes when buffer exceeds flush_count. */ |
| 288 | }; |
| 289 | |
| 290 | struct eeprom_buffer *new_eeprom_buffer(struct eeprom *eeprom); |
| 291 | void destroy_eeprom_buffer(struct eeprom *eeprom); |
| 292 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 293 | We can test this code by *faking out* the underlying EEPROM: |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 294 | |
| 295 | .. code-block:: c |
| 296 | |
| 297 | struct fake_eeprom { |
| 298 | struct eeprom parent; |
| 299 | char contents[FAKE_EEPROM_CONTENTS_SIZE]; |
| 300 | }; |
| 301 | |
| 302 | ssize_t fake_eeprom_read(struct eeprom *parent, size_t offset, char *buffer, size_t count) |
| 303 | { |
| 304 | struct fake_eeprom *this = container_of(parent, struct fake_eeprom, parent); |
| 305 | |
| 306 | count = min(count, FAKE_EEPROM_CONTENTS_SIZE - offset); |
| 307 | memcpy(buffer, this->contents + offset, count); |
| 308 | |
| 309 | return count; |
| 310 | } |
| 311 | |
Brendan Higgins | e7d7ad0 | 2019-11-19 15:38:10 -0800 | [diff] [blame] | 312 | ssize_t fake_eeprom_write(struct eeprom *parent, size_t offset, const char *buffer, size_t count) |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 313 | { |
| 314 | struct fake_eeprom *this = container_of(parent, struct fake_eeprom, parent); |
| 315 | |
| 316 | count = min(count, FAKE_EEPROM_CONTENTS_SIZE - offset); |
| 317 | memcpy(this->contents + offset, buffer, count); |
| 318 | |
| 319 | return count; |
| 320 | } |
| 321 | |
| 322 | void fake_eeprom_init(struct fake_eeprom *this) |
| 323 | { |
| 324 | this->parent.read = fake_eeprom_read; |
| 325 | this->parent.write = fake_eeprom_write; |
| 326 | memset(this->contents, 0, FAKE_EEPROM_CONTENTS_SIZE); |
| 327 | } |
| 328 | |
| 329 | We can now use it to test ``struct eeprom_buffer``: |
| 330 | |
| 331 | .. code-block:: c |
| 332 | |
| 333 | struct eeprom_buffer_test { |
| 334 | struct fake_eeprom *fake_eeprom; |
| 335 | struct eeprom_buffer *eeprom_buffer; |
| 336 | }; |
| 337 | |
| 338 | static void eeprom_buffer_test_does_not_write_until_flush(struct kunit *test) |
| 339 | { |
| 340 | struct eeprom_buffer_test *ctx = test->priv; |
| 341 | struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer; |
| 342 | struct fake_eeprom *fake_eeprom = ctx->fake_eeprom; |
| 343 | char buffer[] = {0xff}; |
| 344 | |
| 345 | eeprom_buffer->flush_count = SIZE_MAX; |
| 346 | |
| 347 | eeprom_buffer->write(eeprom_buffer, buffer, 1); |
| 348 | KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0); |
| 349 | |
| 350 | eeprom_buffer->write(eeprom_buffer, buffer, 1); |
| 351 | KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0); |
| 352 | |
| 353 | eeprom_buffer->flush(eeprom_buffer); |
| 354 | KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff); |
| 355 | KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff); |
| 356 | } |
| 357 | |
| 358 | static void eeprom_buffer_test_flushes_after_flush_count_met(struct kunit *test) |
| 359 | { |
| 360 | struct eeprom_buffer_test *ctx = test->priv; |
| 361 | struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer; |
| 362 | struct fake_eeprom *fake_eeprom = ctx->fake_eeprom; |
| 363 | char buffer[] = {0xff}; |
| 364 | |
| 365 | eeprom_buffer->flush_count = 2; |
| 366 | |
| 367 | eeprom_buffer->write(eeprom_buffer, buffer, 1); |
| 368 | KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0); |
| 369 | |
| 370 | eeprom_buffer->write(eeprom_buffer, buffer, 1); |
| 371 | KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff); |
| 372 | KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff); |
| 373 | } |
| 374 | |
| 375 | static void eeprom_buffer_test_flushes_increments_of_flush_count(struct kunit *test) |
| 376 | { |
| 377 | struct eeprom_buffer_test *ctx = test->priv; |
| 378 | struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer; |
| 379 | struct fake_eeprom *fake_eeprom = ctx->fake_eeprom; |
| 380 | char buffer[] = {0xff, 0xff}; |
| 381 | |
| 382 | eeprom_buffer->flush_count = 2; |
| 383 | |
| 384 | eeprom_buffer->write(eeprom_buffer, buffer, 1); |
| 385 | KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0); |
| 386 | |
| 387 | eeprom_buffer->write(eeprom_buffer, buffer, 2); |
| 388 | KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff); |
| 389 | KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff); |
| 390 | /* Should have only flushed the first two bytes. */ |
| 391 | KUNIT_EXPECT_EQ(test, fake_eeprom->contents[2], 0); |
| 392 | } |
| 393 | |
| 394 | static int eeprom_buffer_test_init(struct kunit *test) |
| 395 | { |
| 396 | struct eeprom_buffer_test *ctx; |
| 397 | |
| 398 | ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); |
| 399 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); |
| 400 | |
| 401 | ctx->fake_eeprom = kunit_kzalloc(test, sizeof(*ctx->fake_eeprom), GFP_KERNEL); |
| 402 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->fake_eeprom); |
| 403 | fake_eeprom_init(ctx->fake_eeprom); |
| 404 | |
| 405 | ctx->eeprom_buffer = new_eeprom_buffer(&ctx->fake_eeprom->parent); |
| 406 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->eeprom_buffer); |
| 407 | |
| 408 | test->priv = ctx; |
| 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static void eeprom_buffer_test_exit(struct kunit *test) |
| 414 | { |
| 415 | struct eeprom_buffer_test *ctx = test->priv; |
| 416 | |
| 417 | destroy_eeprom_buffer(ctx->eeprom_buffer); |
| 418 | } |
| 419 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 420 | Testing Against Multiple Inputs |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 421 | ------------------------------- |
| 422 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 423 | Testing just a few inputs is not enough to ensure that the code works correctly, |
| 424 | for example: testing a hash function. |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 425 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 426 | We can write a helper macro or function. The function is called for each input. |
| 427 | For example, to test ``sha1sum(1)``, we can write: |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 428 | |
| 429 | .. code-block:: c |
| 430 | |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 431 | #define TEST_SHA1(in, want) \ |
| 432 | sha1sum(in, out); \ |
David Gow | 99a8e89 | 2021-05-13 12:31:57 -0700 | [diff] [blame] | 433 | KUNIT_EXPECT_STREQ_MSG(test, out, want, "sha1sum(%s)", in); |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 434 | |
| 435 | char out[40]; |
| 436 | TEST_SHA1("hello world", "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"); |
| 437 | TEST_SHA1("hello world!", "430ce34d020724ed75a196dfc2ad67c77772d169"); |
| 438 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 439 | Note the use of the ``_MSG`` version of ``KUNIT_EXPECT_STREQ`` to print a more |
| 440 | detailed error and make the assertions clearer within the helper macros. |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 441 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 442 | The ``_MSG`` variants are useful when the same expectation is called multiple |
| 443 | times (in a loop or helper function) and thus the line number is not enough to |
| 444 | identify what failed, as shown below. |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 445 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 446 | In complicated cases, we recommend using a *table-driven test* compared to the |
| 447 | helper macro variation, for example: |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 448 | |
| 449 | .. code-block:: c |
| 450 | |
| 451 | int i; |
| 452 | char out[40]; |
| 453 | |
| 454 | struct sha1_test_case { |
| 455 | const char *str; |
| 456 | const char *sha1; |
| 457 | }; |
| 458 | |
| 459 | struct sha1_test_case cases[] = { |
| 460 | { |
| 461 | .str = "hello world", |
| 462 | .sha1 = "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", |
| 463 | }, |
| 464 | { |
| 465 | .str = "hello world!", |
| 466 | .sha1 = "430ce34d020724ed75a196dfc2ad67c77772d169", |
| 467 | }, |
| 468 | }; |
| 469 | for (i = 0; i < ARRAY_SIZE(cases); ++i) { |
| 470 | sha1sum(cases[i].str, out); |
David Gow | 99a8e89 | 2021-05-13 12:31:57 -0700 | [diff] [blame] | 471 | KUNIT_EXPECT_STREQ_MSG(test, out, cases[i].sha1, |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 472 | "sha1sum(%s)", cases[i].str); |
| 473 | } |
| 474 | |
| 475 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 476 | There is more boilerplate code involved, but it can: |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 477 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 478 | * be more readable when there are multiple inputs/outputs (due to field names). |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 479 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 480 | * For example, see ``fs/ext4/inode-test.c``. |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 481 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 482 | * reduce duplication if test cases are shared across multiple tests. |
| 483 | |
| 484 | * For example: if we want to test ``sha256sum``, we could add a ``sha256`` |
Daniel Latypov | 1f0e943 | 2020-11-23 14:57:59 -0800 | [diff] [blame] | 485 | field and reuse ``cases``. |
| 486 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 487 | * be converted to a "parameterized test". |
Daniel Latypov | 8db50be | 2020-12-15 16:22:46 -0800 | [diff] [blame] | 488 | |
| 489 | Parameterized Testing |
| 490 | ~~~~~~~~~~~~~~~~~~~~~ |
| 491 | |
| 492 | The table-driven testing pattern is common enough that KUnit has special |
| 493 | support for it. |
| 494 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 495 | By reusing the same ``cases`` array from above, we can write the test as a |
Daniel Latypov | 8db50be | 2020-12-15 16:22:46 -0800 | [diff] [blame] | 496 | "parameterized test" with the following. |
| 497 | |
| 498 | .. code-block:: c |
| 499 | |
| 500 | // This is copy-pasted from above. |
| 501 | struct sha1_test_case { |
| 502 | const char *str; |
| 503 | const char *sha1; |
| 504 | }; |
| 505 | struct sha1_test_case cases[] = { |
| 506 | { |
| 507 | .str = "hello world", |
| 508 | .sha1 = "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", |
| 509 | }, |
| 510 | { |
| 511 | .str = "hello world!", |
| 512 | .sha1 = "430ce34d020724ed75a196dfc2ad67c77772d169", |
| 513 | }, |
| 514 | }; |
| 515 | |
| 516 | // Need a helper function to generate a name for each test case. |
| 517 | static void case_to_desc(const struct sha1_test_case *t, char *desc) |
| 518 | { |
| 519 | strcpy(desc, t->str); |
| 520 | } |
| 521 | // Creates `sha1_gen_params()` to iterate over `cases`. |
| 522 | KUNIT_ARRAY_PARAM(sha1, cases, case_to_desc); |
| 523 | |
| 524 | // Looks no different from a normal test. |
| 525 | static void sha1_test(struct kunit *test) |
| 526 | { |
| 527 | // This function can just contain the body of the for-loop. |
| 528 | // The former `cases[i]` is accessible under test->param_value. |
| 529 | char out[40]; |
| 530 | struct sha1_test_case *test_param = (struct sha1_test_case *)(test->param_value); |
| 531 | |
| 532 | sha1sum(test_param->str, out); |
David Gow | 99a8e89 | 2021-05-13 12:31:57 -0700 | [diff] [blame] | 533 | KUNIT_EXPECT_STREQ_MSG(test, out, test_param->sha1, |
Daniel Latypov | 8db50be | 2020-12-15 16:22:46 -0800 | [diff] [blame] | 534 | "sha1sum(%s)", test_param->str); |
| 535 | } |
| 536 | |
| 537 | // Instead of KUNIT_CASE, we use KUNIT_CASE_PARAM and pass in the |
| 538 | // function declared by KUNIT_ARRAY_PARAM. |
| 539 | static struct kunit_case sha1_test_cases[] = { |
| 540 | KUNIT_CASE_PARAM(sha1_test, sha1_gen_params), |
| 541 | {} |
| 542 | }; |
| 543 | |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 544 | .. _kunit-on-non-uml: |
| 545 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 546 | Exiting Early on Failed Expectations |
| 547 | ------------------------------------ |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 548 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 549 | We can use ``KUNIT_EXPECT_EQ`` to mark the test as failed and continue |
| 550 | execution. In some cases, it is unsafe to continue. We can use the |
| 551 | ``KUNIT_ASSERT`` variant to exit on failure. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 552 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 553 | .. code-block:: c |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 554 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 555 | void example_test_user_alloc_function(struct kunit *test) |
| 556 | { |
| 557 | void *object = alloc_some_object_for_me(); |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 558 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 559 | /* Make sure we got a valid pointer back. */ |
| 560 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, object); |
| 561 | do_something_with_object(object); |
| 562 | } |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 563 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 564 | Allocating Memory |
| 565 | ----------------- |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 566 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 567 | Where you might use ``kzalloc``, you can instead use ``kunit_kzalloc`` as KUnit |
| 568 | will then ensure that the memory is freed once the test completes. |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 569 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 570 | This is useful because it lets us use the ``KUNIT_ASSERT_EQ`` macros to exit |
| 571 | early from a test without having to worry about remembering to call ``kfree``. |
| 572 | For example: |
Brendan Higgins | 12ca7a8 | 2021-05-26 14:24:07 -0700 | [diff] [blame] | 573 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 574 | .. code-block:: c |
Brendan Higgins | 12ca7a8 | 2021-05-26 14:24:07 -0700 | [diff] [blame] | 575 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 576 | void example_test_allocation(struct kunit *test) |
| 577 | { |
| 578 | char *buffer = kunit_kzalloc(test, 16, GFP_KERNEL); |
| 579 | /* Ensure allocation succeeded. */ |
| 580 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buffer); |
Brendan Higgins | 12ca7a8 | 2021-05-26 14:24:07 -0700 | [diff] [blame] | 581 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 582 | KUNIT_ASSERT_STREQ(test, buffer, ""); |
| 583 | } |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 584 | |
| 585 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 586 | Testing Static Functions |
| 587 | ------------------------ |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 588 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 589 | If we do not want to expose functions or variables for testing, one option is to |
| 590 | conditionally ``#include`` the test file at the end of your .c file. For |
| 591 | example: |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 592 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 593 | .. code-block:: c |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 594 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 595 | /* In my_file.c */ |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 596 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 597 | static int do_interesting_thing(); |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 598 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 599 | #ifdef CONFIG_MY_KUNIT_TEST |
| 600 | #include "my_kunit_test.c" |
| 601 | #endif |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 602 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 603 | Injecting Test-Only Code |
| 604 | ------------------------ |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 605 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 606 | Similar to as shown above, we can add test-specific logic. For example: |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 607 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 608 | .. code-block:: c |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 609 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 610 | /* In my_file.h */ |
Alan Maguire | 6ae2bfd | 2020-01-06 22:28:23 +0000 | [diff] [blame] | 611 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 612 | #ifdef CONFIG_MY_KUNIT_TEST |
| 613 | /* Defined in my_kunit_test.c */ |
| 614 | void test_only_hook(void); |
| 615 | #else |
| 616 | void test_only_hook(void) { } |
| 617 | #endif |
Alan Maguire | 6ae2bfd | 2020-01-06 22:28:23 +0000 | [diff] [blame] | 618 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 619 | This test-only code can be made more useful by accessing the current ``kunit_test`` |
| 620 | as shown in next section: *Accessing The Current Test*. |
Alan Maguire | 6ae2bfd | 2020-01-06 22:28:23 +0000 | [diff] [blame] | 621 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 622 | Accessing The Current Test |
| 623 | -------------------------- |
Alan Maguire | 6ae2bfd | 2020-01-06 22:28:23 +0000 | [diff] [blame] | 624 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 625 | In some cases, we need to call test-only code from outside the test file. |
| 626 | For example, see example in section *Injecting Test-Only Code* or if |
| 627 | we are providing a fake implementation of an ops struct. Using |
| 628 | ``kunit_test`` field in ``task_struct``, we can access it via |
| 629 | ``current->kunit_test``. |
Brendan Higgins | e20d8e8 | 2020-01-31 16:01:02 -0800 | [diff] [blame] | 630 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 631 | The example below includes how to implement "mocking": |
Alan Maguire | 6ae2bfd | 2020-01-06 22:28:23 +0000 | [diff] [blame] | 632 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 633 | .. code-block:: c |
Alan Maguire | 6ae2bfd | 2020-01-06 22:28:23 +0000 | [diff] [blame] | 634 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 635 | #include <linux/sched.h> /* for current */ |
SeongJae Park | f0b6203 | 2020-10-21 21:25:18 +0200 | [diff] [blame] | 636 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 637 | struct test_data { |
| 638 | int foo_result; |
| 639 | int want_foo_called_with; |
| 640 | }; |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 641 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 642 | static int fake_foo(int arg) |
| 643 | { |
| 644 | struct kunit *test = current->kunit_test; |
| 645 | struct test_data *test_data = test->priv; |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 646 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 647 | KUNIT_EXPECT_EQ(test, test_data->want_foo_called_with, arg); |
| 648 | return test_data->foo_result; |
| 649 | } |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 650 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 651 | static void example_simple_test(struct kunit *test) |
| 652 | { |
| 653 | /* Assume priv (private, a member used to pass test data from |
| 654 | * the init function) is allocated in the suite's .init */ |
| 655 | struct test_data *test_data = test->priv; |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 656 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 657 | test_data->foo_result = 42; |
| 658 | test_data->want_foo_called_with = 1; |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 659 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 660 | /* In a real test, we'd probably pass a pointer to fake_foo somewhere |
| 661 | * like an ops struct, etc. instead of calling it directly. */ |
| 662 | KUNIT_EXPECT_EQ(test, fake_foo(1), 42); |
| 663 | } |
Brendan Higgins | c23a283 | 2019-09-23 02:02:45 -0700 | [diff] [blame] | 664 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 665 | In this example, we are using the ``priv`` member of ``struct kunit`` as a way |
| 666 | of passing data to the test from the init function. In general ``priv`` is |
| 667 | pointer that can be used for any user data. This is preferred over static |
| 668 | variables, as it avoids concurrency issues. |
Alan Maguire | 3252690 | 2020-03-26 14:25:10 +0000 | [diff] [blame] | 669 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 670 | Had we wanted something more flexible, we could have used a named ``kunit_resource``. |
| 671 | Each test can have multiple resources which have string names providing the same |
| 672 | flexibility as a ``priv`` member, but also, for example, allowing helper |
| 673 | functions to create resources without conflicting with each other. It is also |
| 674 | possible to define a clean up function for each resource, making it easy to |
| 675 | avoid resource leaks. For more information, see Documentation/dev-tools/kunit/api/test.rst. |
Alan Maguire | 3252690 | 2020-03-26 14:25:10 +0000 | [diff] [blame] | 676 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 677 | Failing The Current Test |
| 678 | ------------------------ |
Alan Maguire | 3252690 | 2020-03-26 14:25:10 +0000 | [diff] [blame] | 679 | |
Harinder Singh | 9535743 | 2021-12-17 04:49:09 +0000 | [diff] [blame^] | 680 | If we want to fail the current test, we can use ``kunit_fail_current_test(fmt, args...)`` |
| 681 | which is defined in ``<kunit/test-bug.h>`` and does not require pulling in ``<kunit/test.h>``. |
| 682 | For example, we have an option to enable some extra debug checks on some data |
| 683 | structures as shown below: |
| 684 | |
| 685 | .. code-block:: c |
| 686 | |
| 687 | #include <kunit/test-bug.h> |
| 688 | |
| 689 | #ifdef CONFIG_EXTRA_DEBUG_CHECKS |
| 690 | static void validate_my_data(struct data *data) |
| 691 | { |
| 692 | if (is_valid(data)) |
| 693 | return; |
| 694 | |
| 695 | kunit_fail_current_test("data %p is invalid", data); |
| 696 | |
| 697 | /* Normal, non-KUnit, error reporting code here. */ |
| 698 | } |
| 699 | #else |
| 700 | static void my_debug_function(void) { } |
| 701 | #endif |
| 702 | |