Skip to content

Commit 02c2d0c

Browse files
rmr167shuahkh
authored andcommitted
kunit: Add speed attribute
Add speed attribute to the test attribute API. This attribute will allow users to mark tests with a category of speed. Currently the categories of speed proposed are: normal, slow, and very_slow (outlined in enum kunit_speed). These are outlined in the enum kunit_speed. The assumed default speed for tests is "normal". This indicates that the test takes a relatively trivial amount of time (less than 1 second), regardless of the machine it is running on. Any test slower than this could be marked as "slow" or "very_slow". Add the macro KUNIT_CASE_SLOW to set a test as slow, as this is likely a common use of the attributes API. Add an example of marking a slow test to kunit-example-test.c. Reviewed-by: David Gow <[email protected]> Signed-off-by: Rae Moar <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 39e92cb commit 02c2d0c

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

include/kunit/test.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,27 @@ enum kunit_status {
6363
KUNIT_SKIPPED,
6464
};
6565

66+
/* Attribute struct/enum definitions */
67+
68+
/*
69+
* Speed Attribute is stored as an enum and separated into categories of
70+
* speed: very_slowm, slow, and normal. These speeds are relative to
71+
* other KUnit tests.
72+
*
73+
* Note: unset speed attribute acts as default of KUNIT_SPEED_NORMAL.
74+
*/
75+
enum kunit_speed {
76+
KUNIT_SPEED_UNSET,
77+
KUNIT_SPEED_VERY_SLOW,
78+
KUNIT_SPEED_SLOW,
79+
KUNIT_SPEED_NORMAL,
80+
KUNIT_SPEED_MAX = KUNIT_SPEED_NORMAL,
81+
};
82+
6683
/* Holds attributes for each test case and suite */
67-
struct kunit_attributes {};
84+
struct kunit_attributes {
85+
enum kunit_speed speed;
86+
};
6887

6988
/**
7089
* struct kunit_case - represents an individual test case.
@@ -150,6 +169,17 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
150169
{ .run_case = test_name, .name = #test_name, \
151170
.attr = attributes }
152171

172+
/**
173+
* KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case
174+
* with the slow attribute
175+
*
176+
* @test_name: a reference to a test case function.
177+
*/
178+
179+
#define KUNIT_CASE_SLOW(test_name) \
180+
{ .run_case = test_name, .name = #test_name, \
181+
.attr.speed = KUNIT_SPEED_SLOW }
182+
153183
/**
154184
* KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
155185
*

lib/kunit/attributes.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,51 @@ struct kunit_attr {
4040
enum print_ops print;
4141
};
4242

43+
/* String Lists for enum Attributes */
44+
45+
static const char * const speed_str_list[] = {"unset", "very_slow", "slow", "normal"};
46+
47+
/* To String Methods */
48+
49+
static const char *attr_enum_to_string(void *attr, const char * const str_list[], bool *to_free)
50+
{
51+
long val = (long)attr;
52+
53+
*to_free = false;
54+
if (!val)
55+
return NULL;
56+
return str_list[val];
57+
}
58+
59+
static const char *attr_speed_to_string(void *attr, bool *to_free)
60+
{
61+
return attr_enum_to_string(attr, speed_str_list, to_free);
62+
}
63+
64+
/* Get Attribute Methods */
65+
66+
static void *attr_speed_get(void *test_or_suite, bool is_test)
67+
{
68+
struct kunit_suite *suite = is_test ? NULL : test_or_suite;
69+
struct kunit_case *test = is_test ? test_or_suite : NULL;
70+
71+
if (test)
72+
return ((void *) test->attr.speed);
73+
else
74+
return ((void *) suite->attr.speed);
75+
}
76+
4377
/* List of all Test Attributes */
4478

45-
static struct kunit_attr kunit_attr_list[] = {};
79+
static struct kunit_attr kunit_attr_list[] = {
80+
{
81+
.name = "speed",
82+
.get_attr = attr_speed_get,
83+
.to_string = attr_speed_to_string,
84+
.attr_default = (void *)KUNIT_SPEED_NORMAL,
85+
.print = PRINT_ALWAYS,
86+
},
87+
};
4688

4789
/* Helper Functions to Access Attributes */
4890

lib/kunit/kunit-example-test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ static void example_params_test(struct kunit *test)
220220
KUNIT_EXPECT_EQ(test, param->value % param->value, 0);
221221
}
222222

223+
/*
224+
* This test should always pass. Can be used to practice filtering attributes.
225+
*/
226+
static void example_slow_test(struct kunit *test)
227+
{
228+
KUNIT_EXPECT_EQ(test, 1 + 1, 2);
229+
}
230+
223231
/*
224232
* Here we make a list of all the test cases we want to add to the test suite
225233
* below.
@@ -237,6 +245,7 @@ static struct kunit_case example_test_cases[] = {
237245
KUNIT_CASE(example_all_expect_macros_test),
238246
KUNIT_CASE(example_static_stub_test),
239247
KUNIT_CASE_PARAM(example_params_test, example_gen_params),
248+
KUNIT_CASE_SLOW(example_slow_test),
240249
{}
241250
};
242251

0 commit comments

Comments
 (0)