/* *Thistestshouldalwaysbeskipped.
*/ staticvoid example_skip_test(struct kunit *test)
{ /* This line should run */
kunit_info(test, "You should not see a line below.");
/* Skip (and abort) the test */
kunit_skip(test, "this test should be skipped");
/* This line should not execute */
KUNIT_FAIL(test, "You should not see this line.");
}
/* *Thistestshouldalwaysbemarkedskipped.
*/ staticvoid example_mark_skipped_test(struct kunit *test)
{ /* This line should run */
kunit_info(test, "You should see a line below.");
/* Skip (but do not abort) the test */
kunit_mark_skipped(test, "this test should be skipped");
/* This line should run */
kunit_info(test, "You should see this line.");
}
/* *Therearealso_MSGvariantsofalloftheabovethatletyouinclude *additionaltextonfailure.
*/
KUNIT_EXPECT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
KUNIT_ASSERT_GT_MSG(test, sizeof(int), 0, "Your ints are 0-bit?!");
}
/* This is a function we'll replace with static stubs. */ staticint add_one(int i)
{ /* This will trigger the stub if active. */
KUNIT_STATIC_STUB_REDIRECT(add_one, i);
return i + 1;
}
/* This is used as a replacement for the above function. */ staticint subtract_one(int i)
{ /* We don't need to trigger the stub from the replacement. */
/* *Thistestshowstheuseofstaticstubs.
*/ staticvoid example_static_stub_test(struct kunit *test)
{ /* By default, function is not stubbed. */
KUNIT_EXPECT_EQ(test, add_one(1), 2);
/* Replace add_one() with subtract_one(). */
kunit_activate_static_stub(test, add_one, subtract_one);
/* add_one() is now replaced. */
KUNIT_EXPECT_EQ(test, add_one(1), 0);
/* By design, param pointer will not be NULL */
KUNIT_ASSERT_NOT_NULL(test, param);
/* Test can be skipped on unsupported param values */ if (!is_power_of_2(param->value))
kunit_skip(test, "unsupported param value %d", param->value);
/* You can use param values for parameterized testing */
KUNIT_EXPECT_EQ(test, param->value % param->value, 0);
}
/* *Thistestshowstheuseoftest->priv.
*/ staticvoid example_priv_test(struct kunit *test)
{ /* unless setup in suite->init(), test->priv is NULL */
KUNIT_ASSERT_NULL(test, test->priv);
/* but can be used to pass arbitrary data to other functions */
test->priv = kunit_kzalloc(test, 1, GFP_KERNEL);
KUNIT_EXPECT_NOT_NULL(test, test->priv);
KUNIT_ASSERT_PTR_EQ(test, test->priv, kunit_get_current_test()->priv);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.