usage.rst 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. .. SPDX-License-Identifier: GPL-2.0
  2. Writing Tests
  3. =============
  4. Test Cases
  5. ----------
  6. The fundamental unit in KUnit is the test case. A test case is a function with
  7. the signature ``void (*)(struct kunit *test)``. It calls the function under test
  8. and then sets *expectations* for what should happen. For example:
  9. .. code-block:: c
  10. void example_test_success(struct kunit *test)
  11. {
  12. }
  13. void example_test_failure(struct kunit *test)
  14. {
  15. KUNIT_FAIL(test, "This test never passes.");
  16. }
  17. In the above example, ``example_test_success`` always passes because it does
  18. nothing; no expectations are set, and therefore all expectations pass. On the
  19. other hand ``example_test_failure`` always fails because it calls ``KUNIT_FAIL``,
  20. which is a special expectation that logs a message and causes the test case to
  21. fail.
  22. Expectations
  23. ~~~~~~~~~~~~
  24. An *expectation* specifies that we expect a piece of code to do something in a
  25. test. An expectation is called like a function. A test is made by setting
  26. expectations about the behavior of a piece of code under test. When one or more
  27. expectations fail, the test case fails and information about the failure is
  28. logged. For example:
  29. .. code-block:: c
  30. void add_test_basic(struct kunit *test)
  31. {
  32. KUNIT_EXPECT_EQ(test, 1, add(1, 0));
  33. KUNIT_EXPECT_EQ(test, 2, add(1, 1));
  34. }
  35. In the above example, ``add_test_basic`` makes a number of assertions about the
  36. behavior of a function called ``add``. The first parameter is always of type
  37. ``struct kunit *``, which contains information about the current test context.
  38. The second parameter, in this case, is what the value is expected to be. The
  39. last value is what the value actually is. If ``add`` passes all of these
  40. expectations, the test case, ``add_test_basic`` will pass; if any one of these
  41. expectations fails, the test case will fail.
  42. A test case *fails* when any expectation is violated; however, the test will
  43. continue to run, and try other expectations until the test case ends or is
  44. otherwise terminated. This is as opposed to *assertions* which are discussed
  45. later.
  46. To learn about more KUnit expectations, see Documentation/dev-tools/kunit/api/test.rst.
  47. .. note::
  48. A single test case should be short, easy to understand, and focused on a
  49. single behavior.
  50. For example, if we want to rigorously test the ``add`` function above, create
  51. additional tests cases which would test each property that an ``add`` function
  52. should have as shown below:
  53. .. code-block:: c
  54. void add_test_basic(struct kunit *test)
  55. {
  56. KUNIT_EXPECT_EQ(test, 1, add(1, 0));
  57. KUNIT_EXPECT_EQ(test, 2, add(1, 1));
  58. }
  59. void add_test_negative(struct kunit *test)
  60. {
  61. KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
  62. }
  63. void add_test_max(struct kunit *test)
  64. {
  65. KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
  66. KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
  67. }
  68. void add_test_overflow(struct kunit *test)
  69. {
  70. KUNIT_EXPECT_EQ(test, INT_MIN, add(INT_MAX, 1));
  71. }
  72. Assertions
  73. ~~~~~~~~~~
  74. An assertion is like an expectation, except that the assertion immediately
  75. terminates the test case if the condition is not satisfied. For example:
  76. .. code-block:: c
  77. static void test_sort(struct kunit *test)
  78. {
  79. int *a, i, r = 1;
  80. a = kunit_kmalloc_array(test, TEST_LEN, sizeof(*a), GFP_KERNEL);
  81. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a);
  82. for (i = 0; i < TEST_LEN; i++) {
  83. r = (r * 725861) % 6599;
  84. a[i] = r;
  85. }
  86. sort(a, TEST_LEN, sizeof(*a), cmpint, NULL);
  87. for (i = 0; i < TEST_LEN-1; i++)
  88. KUNIT_EXPECT_LE(test, a[i], a[i + 1]);
  89. }
  90. In this example, we need to be able to allocate an array to test the ``sort()``
  91. function. So we use ``KUNIT_ASSERT_NOT_ERR_OR_NULL()`` to abort the test if
  92. there's an allocation error.
  93. .. note::
  94. In other test frameworks, ``ASSERT`` macros are often implemented by calling
  95. ``return`` so they only work from the test function. In KUnit, we stop the
  96. current kthread on failure, so you can call them from anywhere.
  97. .. note::
  98. Warning: There is an exception to the above rule. You shouldn't use assertions
  99. in the suite's exit() function, or in the free function for a resource. These
  100. run when a test is shutting down, and an assertion here prevents further
  101. cleanup code from running, potentially leading to a memory leak.
  102. Customizing error messages
  103. --------------------------
  104. Each of the ``KUNIT_EXPECT`` and ``KUNIT_ASSERT`` macros have a ``_MSG``
  105. variant. These take a format string and arguments to provide additional
  106. context to the automatically generated error messages.
  107. .. code-block:: c
  108. char some_str[41];
  109. generate_sha1_hex_string(some_str);
  110. /* Before. Not easy to tell why the test failed. */
  111. KUNIT_EXPECT_EQ(test, strlen(some_str), 40);
  112. /* After. Now we see the offending string. */
  113. KUNIT_EXPECT_EQ_MSG(test, strlen(some_str), 40, "some_str='%s'", some_str);
  114. Alternatively, one can take full control over the error message by using
  115. ``KUNIT_FAIL()``, e.g.
  116. .. code-block:: c
  117. /* Before */
  118. KUNIT_EXPECT_EQ(test, some_setup_function(), 0);
  119. /* After: full control over the failure message. */
  120. if (some_setup_function())
  121. KUNIT_FAIL(test, "Failed to setup thing for testing");
  122. Test Suites
  123. ~~~~~~~~~~~
  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 optional setup and teardown functions that run before/after the whole
  129. suite and/or every test case.
  130. .. note::
  131. A test case will only run if it is associated with a test suite.
  132. For example:
  133. .. code-block:: c
  134. static struct kunit_case example_test_cases[] = {
  135. KUNIT_CASE(example_test_foo),
  136. KUNIT_CASE(example_test_bar),
  137. KUNIT_CASE(example_test_baz),
  138. {}
  139. };
  140. static struct kunit_suite example_test_suite = {
  141. .name = "example",
  142. .init = example_test_init,
  143. .exit = example_test_exit,
  144. .suite_init = example_suite_init,
  145. .suite_exit = example_suite_exit,
  146. .test_cases = example_test_cases,
  147. };
  148. kunit_test_suite(example_test_suite);
  149. In the above example, the test suite ``example_test_suite`` would first run
  150. ``example_suite_init``, then run the test cases ``example_test_foo``,
  151. ``example_test_bar``, and ``example_test_baz``. Each would have
  152. ``example_test_init`` called immediately before it and ``example_test_exit``
  153. called immediately after it. Finally, ``example_suite_exit`` would be called
  154. after everything else. ``kunit_test_suite(example_test_suite)`` registers the
  155. test suite with the KUnit test framework.
  156. .. note::
  157. The ``exit`` and ``suite_exit`` functions will run even if ``init`` or
  158. ``suite_init`` fail. Make sure that they can handle any inconsistent
  159. state which may result from ``init`` or ``suite_init`` encountering errors
  160. or exiting early.
  161. ``kunit_test_suite(...)`` is a macro which tells the linker to put the
  162. specified test suite in a special linker section so that it can be run by KUnit
  163. either after ``late_init``, or when the test module is loaded (if the test was
  164. built as a module).
  165. For more information, see Documentation/dev-tools/kunit/api/test.rst.
  166. .. _kunit-on-non-uml:
  167. Writing Tests For Other Architectures
  168. -------------------------------------
  169. It is better to write tests that run on UML to tests that only run under a
  170. particular architecture. It is better to write tests that run under QEMU or
  171. another easy to obtain (and monetarily free) software environment to a specific
  172. piece of hardware.
  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. Now that we have narrowed down exactly what bits are hardware specific, the
  180. actual procedure for writing and running the tests is same as writing normal
  181. KUnit tests.
  182. .. important::
  183. We may have to reset hardware state. If this is not possible, we may only
  184. be able to run one test case per invocation.
  185. .. TODO(brendanhiggins@google.com): Add an actual example of an architecture-
  186. dependent KUnit test.
  187. Common Patterns
  188. ===============
  189. Isolating Behavior
  190. ------------------
  191. Unit testing limits the amount of code under test to a single unit. It controls
  192. what code gets run when the unit under test calls a function. Where a function
  193. is exposed as part of an API such that the definition of that function can be
  194. changed without affecting the rest of the code base. In the kernel, this comes
  195. from two constructs: classes, which are structs that contain function pointers
  196. provided by the implementer, and architecture-specific functions, which have
  197. definitions selected at compile time.
  198. Classes
  199. ~~~~~~~
  200. Classes are not a construct that is built into the C programming language;
  201. however, it is an easily derived concept. Accordingly, in most cases, every
  202. project that does not use a standardized object oriented library (like GNOME's
  203. GObject) has their own slightly different way of doing object oriented
  204. programming; the Linux kernel is no exception.
  205. The central concept in kernel object oriented programming is the class. In the
  206. kernel, a *class* is a struct that contains function pointers. This creates a
  207. contract between *implementers* and *users* since it forces them to use the
  208. same function signature without having to call the function directly. To be a
  209. class, the function pointers must specify that a pointer to the class, known as
  210. a *class handle*, be one of the parameters. Thus the member functions (also
  211. known as *methods*) have access to member variables (also known as *fields*)
  212. allowing the same implementation to have multiple *instances*.
  213. A class can be *overridden* by *child classes* by embedding the *parent class*
  214. in the child class. Then when the child class *method* is called, the child
  215. implementation knows that the pointer passed to it is of a parent contained
  216. within the child. Thus, the child can compute the pointer to itself because the
  217. pointer to the parent is always a fixed offset from the pointer to the child.
  218. This offset is the offset of the parent contained in the child struct. For
  219. example:
  220. .. code-block:: c
  221. struct shape {
  222. int (*area)(struct shape *this);
  223. };
  224. struct rectangle {
  225. struct shape parent;
  226. int length;
  227. int width;
  228. };
  229. int rectangle_area(struct shape *this)
  230. {
  231. struct rectangle *self = container_of(this, struct rectangle, parent);
  232. return self->length * self->width;
  233. };
  234. void rectangle_new(struct rectangle *self, int length, int width)
  235. {
  236. self->parent.area = rectangle_area;
  237. self->length = length;
  238. self->width = width;
  239. }
  240. In this example, computing the pointer to the child from the pointer to the
  241. parent is done by ``container_of``.
  242. Faking Classes
  243. ~~~~~~~~~~~~~~
  244. In order to unit test a piece of code that calls a method in a class, the
  245. behavior of the method must be controllable, otherwise the test ceases to be a
  246. unit test and becomes an integration test.
  247. A fake class implements a piece of code that is different than what runs in a
  248. production instance, but behaves identical from the standpoint of the callers.
  249. This is done to replace a dependency that is hard to deal with, or is slow. For
  250. example, implementing a fake EEPROM that stores the "contents" in an
  251. internal buffer. Assume we have a class that represents an EEPROM:
  252. .. code-block:: c
  253. struct eeprom {
  254. ssize_t (*read)(struct eeprom *this, size_t offset, char *buffer, size_t count);
  255. ssize_t (*write)(struct eeprom *this, size_t offset, const char *buffer, size_t count);
  256. };
  257. And we want to test code that buffers writes to the EEPROM:
  258. .. code-block:: c
  259. struct eeprom_buffer {
  260. ssize_t (*write)(struct eeprom_buffer *this, const char *buffer, size_t count);
  261. int flush(struct eeprom_buffer *this);
  262. size_t flush_count; /* Flushes when buffer exceeds flush_count. */
  263. };
  264. struct eeprom_buffer *new_eeprom_buffer(struct eeprom *eeprom);
  265. void destroy_eeprom_buffer(struct eeprom *eeprom);
  266. We can test this code by *faking out* the underlying EEPROM:
  267. .. code-block:: c
  268. struct fake_eeprom {
  269. struct eeprom parent;
  270. char contents[FAKE_EEPROM_CONTENTS_SIZE];
  271. };
  272. ssize_t fake_eeprom_read(struct eeprom *parent, size_t offset, char *buffer, size_t count)
  273. {
  274. struct fake_eeprom *this = container_of(parent, struct fake_eeprom, parent);
  275. count = min(count, FAKE_EEPROM_CONTENTS_SIZE - offset);
  276. memcpy(buffer, this->contents + offset, count);
  277. return count;
  278. }
  279. ssize_t fake_eeprom_write(struct eeprom *parent, size_t offset, const char *buffer, size_t count)
  280. {
  281. struct fake_eeprom *this = container_of(parent, struct fake_eeprom, parent);
  282. count = min(count, FAKE_EEPROM_CONTENTS_SIZE - offset);
  283. memcpy(this->contents + offset, buffer, count);
  284. return count;
  285. }
  286. void fake_eeprom_init(struct fake_eeprom *this)
  287. {
  288. this->parent.read = fake_eeprom_read;
  289. this->parent.write = fake_eeprom_write;
  290. memset(this->contents, 0, FAKE_EEPROM_CONTENTS_SIZE);
  291. }
  292. We can now use it to test ``struct eeprom_buffer``:
  293. .. code-block:: c
  294. struct eeprom_buffer_test {
  295. struct fake_eeprom *fake_eeprom;
  296. struct eeprom_buffer *eeprom_buffer;
  297. };
  298. static void eeprom_buffer_test_does_not_write_until_flush(struct kunit *test)
  299. {
  300. struct eeprom_buffer_test *ctx = test->priv;
  301. struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer;
  302. struct fake_eeprom *fake_eeprom = ctx->fake_eeprom;
  303. char buffer[] = {0xff};
  304. eeprom_buffer->flush_count = SIZE_MAX;
  305. eeprom_buffer->write(eeprom_buffer, buffer, 1);
  306. KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0);
  307. eeprom_buffer->write(eeprom_buffer, buffer, 1);
  308. KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0);
  309. eeprom_buffer->flush(eeprom_buffer);
  310. KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff);
  311. KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff);
  312. }
  313. static void eeprom_buffer_test_flushes_after_flush_count_met(struct kunit *test)
  314. {
  315. struct eeprom_buffer_test *ctx = test->priv;
  316. struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer;
  317. struct fake_eeprom *fake_eeprom = ctx->fake_eeprom;
  318. char buffer[] = {0xff};
  319. eeprom_buffer->flush_count = 2;
  320. eeprom_buffer->write(eeprom_buffer, buffer, 1);
  321. KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0);
  322. eeprom_buffer->write(eeprom_buffer, buffer, 1);
  323. KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff);
  324. KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff);
  325. }
  326. static void eeprom_buffer_test_flushes_increments_of_flush_count(struct kunit *test)
  327. {
  328. struct eeprom_buffer_test *ctx = test->priv;
  329. struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer;
  330. struct fake_eeprom *fake_eeprom = ctx->fake_eeprom;
  331. char buffer[] = {0xff, 0xff};
  332. eeprom_buffer->flush_count = 2;
  333. eeprom_buffer->write(eeprom_buffer, buffer, 1);
  334. KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0);
  335. eeprom_buffer->write(eeprom_buffer, buffer, 2);
  336. KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff);
  337. KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff);
  338. /* Should have only flushed the first two bytes. */
  339. KUNIT_EXPECT_EQ(test, fake_eeprom->contents[2], 0);
  340. }
  341. static int eeprom_buffer_test_init(struct kunit *test)
  342. {
  343. struct eeprom_buffer_test *ctx;
  344. ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
  345. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
  346. ctx->fake_eeprom = kunit_kzalloc(test, sizeof(*ctx->fake_eeprom), GFP_KERNEL);
  347. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->fake_eeprom);
  348. fake_eeprom_init(ctx->fake_eeprom);
  349. ctx->eeprom_buffer = new_eeprom_buffer(&ctx->fake_eeprom->parent);
  350. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->eeprom_buffer);
  351. test->priv = ctx;
  352. return 0;
  353. }
  354. static void eeprom_buffer_test_exit(struct kunit *test)
  355. {
  356. struct eeprom_buffer_test *ctx = test->priv;
  357. destroy_eeprom_buffer(ctx->eeprom_buffer);
  358. }
  359. Testing Against Multiple Inputs
  360. -------------------------------
  361. Testing just a few inputs is not enough to ensure that the code works correctly,
  362. for example: testing a hash function.
  363. We can write a helper macro or function. The function is called for each input.
  364. For example, to test ``sha1sum(1)``, we can write:
  365. .. code-block:: c
  366. #define TEST_SHA1(in, want) \
  367. sha1sum(in, out); \
  368. KUNIT_EXPECT_STREQ_MSG(test, out, want, "sha1sum(%s)", in);
  369. char out[40];
  370. TEST_SHA1("hello world", "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed");
  371. TEST_SHA1("hello world!", "430ce34d020724ed75a196dfc2ad67c77772d169");
  372. Note the use of the ``_MSG`` version of ``KUNIT_EXPECT_STREQ`` to print a more
  373. detailed error and make the assertions clearer within the helper macros.
  374. The ``_MSG`` variants are useful when the same expectation is called multiple
  375. times (in a loop or helper function) and thus the line number is not enough to
  376. identify what failed, as shown below.
  377. In complicated cases, we recommend using a *table-driven test* compared to the
  378. helper macro variation, for example:
  379. .. code-block:: c
  380. int i;
  381. char out[40];
  382. struct sha1_test_case {
  383. const char *str;
  384. const char *sha1;
  385. };
  386. struct sha1_test_case cases[] = {
  387. {
  388. .str = "hello world",
  389. .sha1 = "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed",
  390. },
  391. {
  392. .str = "hello world!",
  393. .sha1 = "430ce34d020724ed75a196dfc2ad67c77772d169",
  394. },
  395. };
  396. for (i = 0; i < ARRAY_SIZE(cases); ++i) {
  397. sha1sum(cases[i].str, out);
  398. KUNIT_EXPECT_STREQ_MSG(test, out, cases[i].sha1,
  399. "sha1sum(%s)", cases[i].str);
  400. }
  401. There is more boilerplate code involved, but it can:
  402. * be more readable when there are multiple inputs/outputs (due to field names).
  403. * For example, see ``fs/ext4/inode-test.c``.
  404. * reduce duplication if test cases are shared across multiple tests.
  405. * For example: if we want to test ``sha256sum``, we could add a ``sha256``
  406. field and reuse ``cases``.
  407. * be converted to a "parameterized test".
  408. Parameterized Testing
  409. ~~~~~~~~~~~~~~~~~~~~~
  410. To run a test case against multiple inputs, KUnit provides a parameterized
  411. testing framework. This feature formalizes and extends the concept of
  412. table-driven tests discussed previously.
  413. A KUnit test is determined to be parameterized if a parameter generator function
  414. is provided when registering the test case. A test user can either write their
  415. own generator function or use one that is provided by KUnit. The generator
  416. function is stored in ``kunit_case->generate_params`` and can be set using the
  417. macros described in the section below.
  418. To establish the terminology, a "parameterized test" is a test which is run
  419. multiple times (once per "parameter" or "parameter run"). Each parameter run has
  420. both its own independent ``struct kunit`` (the "parameter run context") and
  421. access to a shared parent ``struct kunit`` (the "parameterized test context").
  422. Passing Parameters to a Test
  423. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  424. There are three ways to provide the parameters to a test:
  425. Array Parameter Macros:
  426. KUnit provides special support for the common table-driven testing pattern.
  427. By applying either ``KUNIT_ARRAY_PARAM`` or ``KUNIT_ARRAY_PARAM_DESC`` to the
  428. ``cases`` array from the previous section, we can create a parameterized test
  429. as shown below:
  430. .. code-block:: c
  431. // This is copy-pasted from above.
  432. struct sha1_test_case {
  433. const char *str;
  434. const char *sha1;
  435. };
  436. static const struct sha1_test_case cases[] = {
  437. {
  438. .str = "hello world",
  439. .sha1 = "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed",
  440. },
  441. {
  442. .str = "hello world!",
  443. .sha1 = "430ce34d020724ed75a196dfc2ad67c77772d169",
  444. },
  445. };
  446. // Creates `sha1_gen_params()` to iterate over `cases` while using
  447. // the struct member `str` for the case description.
  448. KUNIT_ARRAY_PARAM_DESC(sha1, cases, str);
  449. // Looks no different from a normal test.
  450. static void sha1_test(struct kunit *test)
  451. {
  452. // This function can just contain the body of the for-loop.
  453. // The former `cases[i]` is accessible under test->param_value.
  454. char out[40];
  455. struct sha1_test_case *test_param = (struct sha1_test_case *)(test->param_value);
  456. sha1sum(test_param->str, out);
  457. KUNIT_EXPECT_STREQ_MSG(test, out, test_param->sha1,
  458. "sha1sum(%s)", test_param->str);
  459. }
  460. // Instead of KUNIT_CASE, we use KUNIT_CASE_PARAM and pass in the
  461. // function declared by KUNIT_ARRAY_PARAM or KUNIT_ARRAY_PARAM_DESC.
  462. static struct kunit_case sha1_test_cases[] = {
  463. KUNIT_CASE_PARAM(sha1_test, sha1_gen_params),
  464. {}
  465. };
  466. Custom Parameter Generator Function:
  467. The generator function is responsible for generating parameters one-by-one
  468. and has the following signature:
  469. ``const void* (*)(struct kunit *test, const void *prev, char *desc)``.
  470. You can pass the generator function to the ``KUNIT_CASE_PARAM``
  471. or ``KUNIT_CASE_PARAM_WITH_INIT`` macros.
  472. The function receives the previously generated parameter as the ``prev`` argument
  473. (which is ``NULL`` on the first call) and can also access the parameterized
  474. test context passed as the ``test`` argument. KUnit calls this function
  475. repeatedly until it returns ``NULL``, which signifies that a parameterized
  476. test ended.
  477. Below is an example of how it works:
  478. .. code-block:: c
  479. #define MAX_TEST_BUFFER_SIZE 8
  480. // Example generator function. It produces a sequence of buffer sizes that
  481. // are powers of two, starting at 1 (e.g., 1, 2, 4, 8).
  482. static const void *buffer_size_gen_params(struct kunit *test, const void *prev, char *desc)
  483. {
  484. long prev_buffer_size = (long)prev;
  485. long next_buffer_size = 1; // Start with an initial size of 1.
  486. // Stop generating parameters if the limit is reached or exceeded.
  487. if (prev_buffer_size >= MAX_TEST_BUFFER_SIZE)
  488. return NULL;
  489. // For subsequent calls, calculate the next size by doubling the previous one.
  490. if (prev)
  491. next_buffer_size = prev_buffer_size << 1;
  492. return (void *)next_buffer_size;
  493. }
  494. // Simple test to validate that kunit_kzalloc provides zeroed memory.
  495. static void buffer_zero_test(struct kunit *test)
  496. {
  497. long buffer_size = (long)test->param_value;
  498. // Use kunit_kzalloc to allocate a zero-initialized buffer. This makes the
  499. // memory "parameter run managed," meaning it's automatically cleaned up at
  500. // the end of each parameter run.
  501. int *buf = kunit_kzalloc(test, buffer_size * sizeof(int), GFP_KERNEL);
  502. // Ensure the allocation was successful.
  503. KUNIT_ASSERT_NOT_NULL(test, buf);
  504. // Loop through the buffer and confirm every element is zero.
  505. for (int i = 0; i < buffer_size; i++)
  506. KUNIT_EXPECT_EQ(test, buf[i], 0);
  507. }
  508. static struct kunit_case buffer_test_cases[] = {
  509. KUNIT_CASE_PARAM(buffer_zero_test, buffer_size_gen_params),
  510. {}
  511. };
  512. Runtime Parameter Array Registration in the Init Function:
  513. For scenarios where you might need to initialize a parameterized test, you
  514. can directly register a parameter array to the parameterized test context.
  515. To do this, you must pass the parameterized test context, the array itself,
  516. the array size, and a ``get_description()`` function to the
  517. ``kunit_register_params_array()`` macro. This macro populates
  518. ``struct kunit_params`` within the parameterized test context, effectively
  519. storing a parameter array object. The ``get_description()`` function will
  520. be used for populating parameter descriptions and has the following signature:
  521. ``void (*)(struct kunit *test, const void *param, char *desc)``. Note that it
  522. also has access to the parameterized test context.
  523. .. important::
  524. When using this way to register a parameter array, you will need to
  525. manually pass ``kunit_array_gen_params()`` as the generator function to
  526. ``KUNIT_CASE_PARAM_WITH_INIT``. ``kunit_array_gen_params()`` is a KUnit
  527. helper that will use the registered array to generate the parameters.
  528. If needed, instead of passing the KUnit helper, you can also pass your
  529. own custom generator function that utilizes the parameter array. To
  530. access the parameter array from within the parameter generator
  531. function use ``test->params_array.params``.
  532. The ``kunit_register_params_array()`` macro should be called within a
  533. ``param_init()`` function that initializes the parameterized test and has
  534. the following signature ``int (*)(struct kunit *test)``. For a detailed
  535. explanation of this mechanism please refer to the "Adding Shared Resources"
  536. section that is after this one. This method supports registering both
  537. dynamically built and static parameter arrays.
  538. The code snippet below shows the ``example_param_init_dynamic_arr`` test that
  539. utilizes ``make_fibonacci_params()`` to create a dynamic array, which is then
  540. registered using ``kunit_register_params_array()``. To see the full code
  541. please refer to lib/kunit/kunit-example-test.c.
  542. .. code-block:: c
  543. /*
  544. * Example of a parameterized test param_init() function that registers a dynamic
  545. * array of parameters.
  546. */
  547. static int example_param_init_dynamic_arr(struct kunit *test)
  548. {
  549. size_t seq_size;
  550. int *fibonacci_params;
  551. kunit_info(test, "initializing parameterized test\n");
  552. seq_size = 6;
  553. fibonacci_params = make_fibonacci_params(test, seq_size);
  554. if (!fibonacci_params)
  555. return -ENOMEM;
  556. /*
  557. * Passes the dynamic parameter array information to the parameterized test
  558. * context struct kunit. The array and its metadata will be stored in
  559. * test->parent->params_array. The array itself will be located in
  560. * params_data.params.
  561. */
  562. kunit_register_params_array(test, fibonacci_params, seq_size,
  563. example_param_dynamic_arr_get_desc);
  564. return 0;
  565. }
  566. static struct kunit_case example_test_cases[] = {
  567. /*
  568. * Note how we pass kunit_array_gen_params() to use the array we
  569. * registered in example_param_init_dynamic_arr() to generate
  570. * parameters.
  571. */
  572. KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init_dynamic_arr,
  573. kunit_array_gen_params,
  574. example_param_init_dynamic_arr,
  575. example_param_exit_dynamic_arr),
  576. {}
  577. };
  578. Adding Shared Resources
  579. ^^^^^^^^^^^^^^^^^^^^^^^
  580. All parameter runs in this framework hold a reference to the parameterized test
  581. context, which can be accessed using the parent ``struct kunit`` pointer. The
  582. parameterized test context is not used to execute any test logic itself; instead,
  583. it serves as a container for shared resources.
  584. It's possible to add resources to share between parameter runs within a
  585. parameterized test by using ``KUNIT_CASE_PARAM_WITH_INIT``, to which you pass
  586. custom ``param_init()`` and ``param_exit()`` functions. These functions run once
  587. before and once after the parameterized test, respectively.
  588. The ``param_init()`` function, with the signature ``int (*)(struct kunit *test)``,
  589. can be used for adding resources to the ``resources`` or ``priv`` fields of
  590. the parameterized test context, registering the parameter array, and any other
  591. initialization logic.
  592. The ``param_exit()`` function, with the signature ``void (*)(struct kunit *test)``,
  593. can be used to release any resources that were not parameterized test managed (i.e.
  594. not automatically cleaned up after the parameterized test ends) and for any other
  595. exit logic.
  596. Both ``param_init()`` and ``param_exit()`` are passed the parameterized test
  597. context behind the scenes. However, the test case function receives the parameter
  598. run context. Therefore, to manage and access shared resources from within a test
  599. case function, you must use ``test->parent``.
  600. For instance, finding a shared resource allocated by the Resource API requires
  601. passing ``test->parent`` to ``kunit_find_resource()``. This principle extends to
  602. all other APIs that might be used in the test case function, including
  603. ``kunit_kzalloc()``, ``kunit_kmalloc_array()``, and others (see
  604. Documentation/dev-tools/kunit/api/test.rst and the
  605. Documentation/dev-tools/kunit/api/resource.rst).
  606. .. note::
  607. The ``suite->init()`` function, which executes before each parameter run,
  608. receives the parameter run context. Therefore, any resources set up in
  609. ``suite->init()`` are cleaned up after each parameter run.
  610. The code below shows how you can add the shared resources. Note that this code
  611. utilizes the Resource API, which you can read more about here:
  612. Documentation/dev-tools/kunit/api/resource.rst. To see the full version of this
  613. code please refer to lib/kunit/kunit-example-test.c.
  614. .. code-block:: c
  615. static int example_resource_init(struct kunit_resource *res, void *context)
  616. {
  617. ... /* Code that allocates memory and stores context in res->data. */
  618. }
  619. /* This function deallocates memory for the kunit_resource->data field. */
  620. static void example_resource_free(struct kunit_resource *res)
  621. {
  622. kfree(res->data);
  623. }
  624. /* This match function locates a test resource based on defined criteria. */
  625. static bool example_resource_alloc_match(struct kunit *test, struct kunit_resource *res,
  626. void *match_data)
  627. {
  628. return res->data && res->free == example_resource_free;
  629. }
  630. /* Function to initialize the parameterized test. */
  631. static int example_param_init(struct kunit *test)
  632. {
  633. int ctx = 3; /* Data to be stored. */
  634. void *data = kunit_alloc_resource(test, example_resource_init,
  635. example_resource_free,
  636. GFP_KERNEL, &ctx);
  637. if (!data)
  638. return -ENOMEM;
  639. kunit_register_params_array(test, example_params_array,
  640. ARRAY_SIZE(example_params_array));
  641. return 0;
  642. }
  643. /* Example test that uses shared resources in test->resources. */
  644. static void example_params_test_with_init(struct kunit *test)
  645. {
  646. int threshold;
  647. const struct example_param *param = test->param_value;
  648. /* Here we pass test->parent to access the parameterized test context. */
  649. struct kunit_resource *res = kunit_find_resource(test->parent,
  650. example_resource_alloc_match,
  651. NULL);
  652. threshold = *((int *)res->data);
  653. KUNIT_ASSERT_LE(test, param->value, threshold);
  654. kunit_put_resource(res);
  655. }
  656. static struct kunit_case example_test_cases[] = {
  657. KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init, kunit_array_gen_params,
  658. example_param_init, NULL),
  659. {}
  660. };
  661. As an alternative to using the KUnit Resource API for sharing resources, you can
  662. place them in ``test->parent->priv``. This serves as a more lightweight method
  663. for resource storage, best for scenarios where complex resource management is
  664. not required.
  665. As stated previously ``param_init()`` and ``param_exit()`` get the parameterized
  666. test context. So, you can directly use ``test->priv`` within ``param_init/exit``
  667. to manage shared resources. However, from within the test case function, you must
  668. navigate up to the parent ``struct kunit`` i.e. the parameterized test context.
  669. Therefore, you need to use ``test->parent->priv`` to access those same
  670. resources.
  671. The resources placed in ``test->parent->priv`` will need to be allocated in
  672. memory to persist across the parameter runs. If memory is allocated using the
  673. KUnit memory allocation APIs (described more in the "Allocating Memory" section
  674. below), you won't need to worry about deallocation. The APIs will make the memory
  675. parameterized test 'managed', ensuring that it will automatically get cleaned up
  676. after the parameterized test concludes.
  677. The code below demonstrates example usage of the ``priv`` field for shared
  678. resources:
  679. .. code-block:: c
  680. static const struct example_param {
  681. int value;
  682. } example_params_array[] = {
  683. { .value = 3, },
  684. { .value = 2, },
  685. { .value = 1, },
  686. { .value = 0, },
  687. };
  688. /* Initialize the parameterized test context. */
  689. static int example_param_init_priv(struct kunit *test)
  690. {
  691. int ctx = 3; /* Data to be stored. */
  692. int arr_size = ARRAY_SIZE(example_params_array);
  693. /*
  694. * Allocate memory using kunit_kzalloc(). Since the `param_init`
  695. * function receives the parameterized test context, this memory
  696. * allocation will be scoped to the lifetime of the parameterized test.
  697. */
  698. test->priv = kunit_kzalloc(test, sizeof(int), GFP_KERNEL);
  699. /* Assign the context value to test->priv.*/
  700. *((int *)test->priv) = ctx;
  701. /* Register the parameter array. */
  702. kunit_register_params_array(test, example_params_array, arr_size, NULL);
  703. return 0;
  704. }
  705. static void example_params_test_with_init_priv(struct kunit *test)
  706. {
  707. int threshold;
  708. const struct example_param *param = test->param_value;
  709. /* By design, test->parent will not be NULL. */
  710. KUNIT_ASSERT_NOT_NULL(test, test->parent);
  711. /* Here we use test->parent->priv to access the shared resource. */
  712. threshold = *(int *)test->parent->priv;
  713. KUNIT_ASSERT_LE(test, param->value, threshold);
  714. }
  715. static struct kunit_case example_tests[] = {
  716. KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init_priv,
  717. kunit_array_gen_params,
  718. example_param_init_priv, NULL),
  719. {}
  720. };
  721. Allocating Memory
  722. -----------------
  723. Where you might use ``kzalloc``, you can instead use ``kunit_kzalloc`` as KUnit
  724. will then ensure that the memory is freed once the test completes.
  725. This is useful because it lets us use the ``KUNIT_ASSERT_EQ`` macros to exit
  726. early from a test without having to worry about remembering to call ``kfree``.
  727. For example:
  728. .. code-block:: c
  729. void example_test_allocation(struct kunit *test)
  730. {
  731. char *buffer = kunit_kzalloc(test, 16, GFP_KERNEL);
  732. /* Ensure allocation succeeded. */
  733. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buffer);
  734. KUNIT_ASSERT_STREQ(test, buffer, "");
  735. }
  736. Registering Cleanup Actions
  737. ---------------------------
  738. If you need to perform some cleanup beyond simple use of ``kunit_kzalloc``,
  739. you can register a custom "deferred action", which is a cleanup function
  740. run when the test exits (whether cleanly, or via a failed assertion).
  741. Actions are simple functions with no return value, and a single ``void*``
  742. context argument, and fulfill the same role as "cleanup" functions in Python
  743. and Go tests, "defer" statements in languages which support them, and
  744. (in some cases) destructors in RAII languages.
  745. These are very useful for unregistering things from global lists, closing
  746. files or other resources, or freeing resources.
  747. For example:
  748. .. code-block:: C
  749. static void cleanup_device(void *ctx)
  750. {
  751. struct device *dev = (struct device *)ctx;
  752. device_unregister(dev);
  753. }
  754. void example_device_test(struct kunit *test)
  755. {
  756. struct my_device dev;
  757. device_register(&dev);
  758. kunit_add_action(test, &cleanup_device, &dev);
  759. }
  760. Note that, for functions like device_unregister which only accept a single
  761. pointer-sized argument, it's possible to automatically generate a wrapper
  762. with the ``KUNIT_DEFINE_ACTION_WRAPPER()`` macro, for example:
  763. .. code-block:: C
  764. KUNIT_DEFINE_ACTION_WRAPPER(device_unregister, device_unregister_wrapper, struct device *);
  765. kunit_add_action(test, &device_unregister_wrapper, &dev);
  766. You should do this in preference to manually casting to the ``kunit_action_t`` type,
  767. as casting function pointers will break Control Flow Integrity (CFI).
  768. ``kunit_add_action`` can fail if, for example, the system is out of memory.
  769. You can use ``kunit_add_action_or_reset`` instead which runs the action
  770. immediately if it cannot be deferred.
  771. If you need more control over when the cleanup function is called, you
  772. can trigger it early using ``kunit_release_action``, or cancel it entirely
  773. with ``kunit_remove_action``.
  774. Testing Static Functions
  775. ------------------------
  776. If you want to test static functions without exposing those functions outside of
  777. testing, one option is conditionally export the symbol. When KUnit is enabled,
  778. the symbol is exposed but remains static otherwise. To use this method, follow
  779. the template below.
  780. .. code-block:: c
  781. /* In the file containing functions to test "my_file.c" */
  782. #include <kunit/visibility.h>
  783. #include <my_file.h>
  784. ...
  785. VISIBLE_IF_KUNIT int do_interesting_thing()
  786. {
  787. ...
  788. }
  789. EXPORT_SYMBOL_IF_KUNIT(do_interesting_thing);
  790. /* In the header file "my_file.h" */
  791. #if IS_ENABLED(CONFIG_KUNIT)
  792. int do_interesting_thing(void);
  793. #endif
  794. /* In the KUnit test file "my_file_test.c" */
  795. #include <kunit/visibility.h>
  796. #include <my_file.h>
  797. ...
  798. MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
  799. ...
  800. // Use do_interesting_thing() in tests
  801. For a full example, see this `patch <https://lore.kernel.org/all/20221207014024.340230-3-rmoar@google.com/>`_
  802. where a test is modified to conditionally expose static functions for testing
  803. using the macros above.
  804. As an **alternative** to the method above, you could conditionally ``#include``
  805. the test file at the end of your .c file. This is not recommended but works
  806. if needed. For example:
  807. .. code-block:: c
  808. /* In "my_file.c" */
  809. static int do_interesting_thing();
  810. #ifdef CONFIG_MY_KUNIT_TEST
  811. #include "my_kunit_test.c"
  812. #endif
  813. Injecting Test-Only Code
  814. ------------------------
  815. Similar to as shown above, we can add test-specific logic. For example:
  816. .. code-block:: c
  817. /* In my_file.h */
  818. #ifdef CONFIG_MY_KUNIT_TEST
  819. /* Defined in my_kunit_test.c */
  820. void test_only_hook(void);
  821. #else
  822. void test_only_hook(void) { }
  823. #endif
  824. This test-only code can be made more useful by accessing the current ``kunit_test``
  825. as shown in next section: *Accessing The Current Test*.
  826. Accessing The Current Test
  827. --------------------------
  828. In some cases, we need to call test-only code from outside the test file. This
  829. is helpful, for example, when providing a fake implementation of a function, or
  830. to fail any current test from within an error handler.
  831. We can do this via the ``kunit_test`` field in ``task_struct``, which we can
  832. access using the ``kunit_get_current_test()`` function in ``kunit/test-bug.h``.
  833. ``kunit_get_current_test()`` is safe to call even if KUnit is not enabled. If
  834. KUnit is not enabled, or if no test is running in the current task, it will
  835. return ``NULL``. This compiles down to either a no-op or a static key check,
  836. so will have a negligible performance impact when no test is running.
  837. The example below uses this to implement a "mock" implementation of a function, ``foo``:
  838. .. code-block:: c
  839. #include <kunit/test-bug.h> /* for kunit_get_current_test */
  840. struct test_data {
  841. int foo_result;
  842. int want_foo_called_with;
  843. };
  844. static int fake_foo(int arg)
  845. {
  846. struct kunit *test = kunit_get_current_test();
  847. struct test_data *test_data = test->priv;
  848. KUNIT_EXPECT_EQ(test, test_data->want_foo_called_with, arg);
  849. return test_data->foo_result;
  850. }
  851. static void example_simple_test(struct kunit *test)
  852. {
  853. /* Assume priv (private, a member used to pass test data from
  854. * the init function) is allocated in the suite's .init */
  855. struct test_data *test_data = test->priv;
  856. test_data->foo_result = 42;
  857. test_data->want_foo_called_with = 1;
  858. /* In a real test, we'd probably pass a pointer to fake_foo somewhere
  859. * like an ops struct, etc. instead of calling it directly. */
  860. KUNIT_EXPECT_EQ(test, fake_foo(1), 42);
  861. }
  862. In this example, we are using the ``priv`` member of ``struct kunit`` as a way
  863. of passing data to the test from the init function. In general ``priv`` is
  864. pointer that can be used for any user data. This is preferred over static
  865. variables, as it avoids concurrency issues.
  866. Had we wanted something more flexible, we could have used a named ``kunit_resource``.
  867. Each test can have multiple resources which have string names providing the same
  868. flexibility as a ``priv`` member, but also, for example, allowing helper
  869. functions to create resources without conflicting with each other. It is also
  870. possible to define a clean up function for each resource, making it easy to
  871. avoid resource leaks. For more information, see Documentation/dev-tools/kunit/api/resource.rst.
  872. Failing The Current Test
  873. ------------------------
  874. If we want to fail the current test, we can use ``kunit_fail_current_test(fmt, args...)``
  875. which is defined in ``<kunit/test-bug.h>`` and does not require pulling in ``<kunit/test.h>``.
  876. For example, we have an option to enable some extra debug checks on some data
  877. structures as shown below:
  878. .. code-block:: c
  879. #include <kunit/test-bug.h>
  880. #ifdef CONFIG_EXTRA_DEBUG_CHECKS
  881. static void validate_my_data(struct data *data)
  882. {
  883. if (is_valid(data))
  884. return;
  885. kunit_fail_current_test("data %p is invalid", data);
  886. /* Normal, non-KUnit, error reporting code here. */
  887. }
  888. #else
  889. static void my_debug_function(void) { }
  890. #endif
  891. ``kunit_fail_current_test()`` is safe to call even if KUnit is not enabled. If
  892. KUnit is not enabled, or if no test is running in the current task, it will do
  893. nothing. This compiles down to either a no-op or a static key check, so will
  894. have a negligible performance impact when no test is running.
  895. Managing Fake Devices and Drivers
  896. ---------------------------------
  897. When testing drivers or code which interacts with drivers, many functions will
  898. require a ``struct device`` or ``struct device_driver``. In many cases, setting
  899. up a real device is not required to test any given function, so a fake device
  900. can be used instead.
  901. KUnit provides helper functions to create and manage these fake devices, which
  902. are internally of type ``struct kunit_device``, and are attached to a special
  903. ``kunit_bus``. These devices support managed device resources (devres), as
  904. described in Documentation/driver-api/driver-model/devres.rst
  905. To create a KUnit-managed ``struct device_driver``, use ``kunit_driver_create()``,
  906. which will create a driver with the given name, on the ``kunit_bus``. This driver
  907. will automatically be destroyed when the corresponding test finishes, but can also
  908. be manually destroyed with ``driver_unregister()``.
  909. To create a fake device, use the ``kunit_device_register()``, which will create
  910. and register a device, using a new KUnit-managed driver created with ``kunit_driver_create()``.
  911. To provide a specific, non-KUnit-managed driver, use ``kunit_device_register_with_driver()``
  912. instead. Like with managed drivers, KUnit-managed fake devices are automatically
  913. cleaned up when the test finishes, but can be manually cleaned up early with
  914. ``kunit_device_unregister()``.
  915. The KUnit devices should be used in preference to ``root_device_register()``, and
  916. instead of ``platform_device_register()`` in cases where the device is not otherwise
  917. a platform device.
  918. For example:
  919. .. code-block:: c
  920. #include <kunit/device.h>
  921. static void test_my_device(struct kunit *test)
  922. {
  923. struct device *fake_device;
  924. const char *dev_managed_string;
  925. // Create a fake device.
  926. fake_device = kunit_device_register(test, "my_device");
  927. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fake_device)
  928. // Pass it to functions which need a device.
  929. dev_managed_string = devm_kstrdup(fake_device, "Hello, World!");
  930. // Everything is cleaned up automatically when the test ends.
  931. }