STRUCT KUNIT_CASE(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… struct kunit_case(9) β€” Kernel API Manual, bestie edition πŸŽ€

Okay so this is a page from the Kernel API Manual (struct kunit_case(9)), and honestly? It's giving "the building block of every KUnit test you'll ever write." Let's get into it ✨


🌸 NAME 🌸

struct kunit_case β€” the glow-up container that represents, like, one individual test case. Just one. A whole little icon. πŸ‘‘


✨ SYNOPSIS ✨

struct kunit_case {
    void (*run_case)(struct kunit *test);
    const char *name;
    const void* (*generate_params)(struct kunit *test, const void *prev,
char *desc);
    struct kunit_attributes attr;
    int (*param_init)(struct kunit *test);
    void (*param_exit)(struct kunit *test);
 };

πŸ¦‹ Members πŸ¦‹

  • run_case β€” the actual function doing the test-case thing. This is the main character, no cap.
  • name β€” what the test case is called. Its little nametag at the party πŸŽ€
  • generate_params β€” the generator function for parameterized tests. iykyk πŸ’«
  • attr β€” the attributes hanging out with the test.
  • param_init β€” the init function that runs before a parameterized test.
  • param_exit β€” the exit function that runs after a parameterized test. Cleanup queen 🧹

πŸ’– Description πŸ’–

So a test case is literally just a function with the signature void (*)(struct kunit *) that makes expectations and assertions (see KUNIT_EXPECT_TRUE and KUNIT_ASSERT_TRUE) about the code under test. That's the whole vibe check: you point it at some code and you assert stuff about it.

Each test case is associated with a struct kunit_suite, and it gets run after the suite's init function and followed by the suite's exit function. Like a little sandwich πŸ₯ͺ

Bestie, listen: a test case should be static, and it should only be created with the KUNIT_CASE macro. Also β€” and this is the part people forget 😭 β€” every array of test cases must be terminated with an empty test case. Do not skip the empty one. It's the periodt of the array.

🌈 Example 🌈

void add_test_basic(struct kunit *test)
{
        KUNIT_EXPECT_EQ(test, 1, add(1, 0));
        KUNIT_EXPECT_EQ(test, 2, add(1, 1));
        KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
        KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
        KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
}

static struct kunit_case example_test_cases[] = {
        KUNIT_CASE(add_test_basic),
        {}
};

See that {} at the end? That's the empty test case terminator doing its job. Obsessed. πŸ’…


🫢 SEE ALSO 🫢

The kernel file is ./include/kunit/test.h, and honestly the whole friend group is here:

enum kunit_status(9), KUNIT_CASE(9), KUNIT_CASE_ATTR(9), KUNIT_CASE_SLOW(9), KUNIT_CASE_PARAM(9), KUNIT_CASE_PARAM_ATTR(9), KUNIT_CASE_PARAM_WITH_INIT(9), struct kunit_suite(9), struct kunit(9), kunit_test_suites(9), kunit_test_init_section_suites(9), kunit_kmalloc_array(9), kunit_kmalloc(9), kunit_kfree(9), kunit_kzalloc(9), kunit_kcalloc(9), kunit_kfree_const(9), kunit_kstrdup(9), kunit_kstrdup_const(9), kunit_attach_mm(9), kunit_vm_mmap(9), kunit_mark_skipped(9), kunit_skip(9), kunit_info(9), kunit_warn(9), kunit_err(9), KUNIT_SUCCEED(9), KUNIT_FAIL(9), KUNIT_EXPECT_TRUE(9), KUNIT_EXPECT_FALSE(9), KUNIT_EXPECT_EQ(9), KUNIT_EXPECT_PTR_EQ(9), KUNIT_EXPECT_NE(9), KUNIT_EXPECT_PTR_NE(9), KUNIT_EXPECT_LT(9), KUNIT_EXPECT_LE(9), KUNIT_EXPECT_GT(9), KUNIT_EXPECT_GE(9), KUNIT_EXPECT_STREQ(9), KUNIT_EXPECT_STRNEQ(9), KUNIT_EXPECT_MEMEQ(9), KUNIT_EXPECT_MEMNEQ(9), KUNIT_EXPECT_NULL(9), KUNIT_EXPECT_NOT_NULL(9), KUNIT_EXPECT_NOT_ERR_OR_NULL(9), KUNIT_FAIL_AND_ABORT(9), KUNIT_ASSERT_TRUE(9), KUNIT_ASSERT_FALSE(9), KUNIT_ASSERT_EQ(9), KUNIT_ASSERT_PTR_EQ(9), KUNIT_ASSERT_NE(9), KUNIT_ASSERT_PTR_NE(9), KUNIT_ASSERT_LT(9), KUNIT_ASSERT_LE(9), KUNIT_ASSERT_GT(9), KUNIT_ASSERT_GE(9), KUNIT_ASSERT_STREQ(9), KUNIT_ASSERT_STRNEQ(9), KUNIT_ASSERT_MEMEQ(9), KUNIT_ASSERT_MEMNEQ(9), KUNIT_ASSERT_NULL(9), KUNIT_ASSERT_NOT_NULL(9), KUNIT_ASSERT_NOT_ERR_OR_NULL(9), KUNIT_ARRAY_PARAM(9), KUNIT_ARRAY_PARAM_DESC(9), kunit_register_params_array(9), kunit_warning_suppress(9), KUNIT_SUPPRESSED_WARNING_COUNT(9), KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(9), KUNIT_ASSERT_SUPPRESSED_WARNING_COUNT(9).


πŸ’– That's the whole page, bestie! πŸ’– This cutie came from ./include/kunit and it's dated October 2026 β€” so yeah, timestamped and slaying. πŸ¦‹πŸŒΈ Go write some tests, mwah πŸ’…βœ¨

πŸ“¦ where this page comes from