* [PATCH] Documentation: kunit: include example of a parameterized test
@ 2020-12-16 0:22 Daniel Latypov
2020-12-16 6:28 ` David Gow
2021-01-14 22:29 ` Brendan Higgins
0 siblings, 2 replies; 3+ messages in thread
From: Daniel Latypov @ 2020-12-16 0:22 UTC (permalink / raw)
To: davidgow, brendanhiggins
Cc: elver, 98.arpi, linux-kernel, linux-kselftest, skhan, Daniel Latypov
Commit fadb08e7c750 ("kunit: Support for Parameterized Testing")
introduced support but lacks documentation for how to use it.
This patch builds on commit 1f0e943df68a ("Documentation: kunit: provide
guidance for testing many inputs") to show a minimal example of the new
feature.
Signed-off-by: Daniel Latypov <dlatypov@google.com>
---
Documentation/dev-tools/kunit/usage.rst | 57 +++++++++++++++++++++++++
1 file changed, 57 insertions(+)
diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst
index d9fdc14f0677..650f99590df5 100644
--- a/Documentation/dev-tools/kunit/usage.rst
+++ b/Documentation/dev-tools/kunit/usage.rst
@@ -522,6 +522,63 @@ There's more boilerplate involved, but it can:
* E.g. if we wanted to also test ``sha256sum``, we could add a ``sha256``
field and reuse ``cases``.
+* be converted to a "parameterized test", see below.
+
+Parameterized Testing
+~~~~~~~~~~~~~~~~~~~~~
+
+The table-driven testing pattern is common enough that KUnit has special
+support for it.
+
+Reusing the same ``cases`` array from above, we can write the test as a
+"parameterized test" with the following.
+
+.. code-block:: c
+
+ // This is copy-pasted from above.
+ struct sha1_test_case {
+ const char *str;
+ const char *sha1;
+ };
+ struct sha1_test_case cases[] = {
+ {
+ .str = "hello world",
+ .sha1 = "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed",
+ },
+ {
+ .str = "hello world!",
+ .sha1 = "430ce34d020724ed75a196dfc2ad67c77772d169",
+ },
+ };
+
+ // Need a helper function to generate a name for each test case.
+ static void case_to_desc(const struct sha1_test_case *t, char *desc)
+ {
+ strcpy(desc, t->str);
+ }
+ // Creates `sha1_gen_params()` to iterate over `cases`.
+ KUNIT_ARRAY_PARAM(sha1, cases, case_to_desc);
+
+ // Looks no different from a normal test.
+ static void sha1_test(struct kunit *test)
+ {
+ // This function can just contain the body of the for-loop.
+ // The former `cases[i]` is accessible under test->param_value.
+ char out[40];
+ struct sha1_test_case *test_param = (struct sha1_test_case *)(test->param_value);
+
+ sha1sum(test_param->str, out);
+ KUNIT_EXPECT_STREQ_MSG(test, (char *)out, test_param->sha1,
+ "sha1sum(%s)", test_param->str);
+ }
+
+ // Instead of KUNIT_CASE, we use KUNIT_CASE_PARAM and pass in the
+ // function declared by KUNIT_ARRAY_PARAM.
+ static struct kunit_case sha1_test_cases[] = {
+ KUNIT_CASE_PARAM(sha1_test, sha1_gen_params),
+ {}
+ };
+
.. _kunit-on-non-uml:
KUnit on non-UML architectures
base-commit: 5f6b99d0287de2c2d0b5e7abcb0092d553ad804a
--
2.29.2.684.gfbc64c5ab5-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] Documentation: kunit: include example of a parameterized test
2020-12-16 0:22 [PATCH] Documentation: kunit: include example of a parameterized test Daniel Latypov
@ 2020-12-16 6:28 ` David Gow
2021-01-14 22:29 ` Brendan Higgins
1 sibling, 0 replies; 3+ messages in thread
From: David Gow @ 2020-12-16 6:28 UTC (permalink / raw)
To: Daniel Latypov
Cc: Brendan Higgins, Marco Elver, Arpitha Raghunandan,
Linux Kernel Mailing List, open list:KERNEL SELFTEST FRAMEWORK,
Shuah Khan
[-- Attachment #1: Type: text/plain, Size: 521 bytes --]
On Wed, Dec 16, 2020 at 8:23 AM Daniel Latypov <dlatypov@google.com> wrote:
>
> Commit fadb08e7c750 ("kunit: Support for Parameterized Testing")
> introduced support but lacks documentation for how to use it.
>
> This patch builds on commit 1f0e943df68a ("Documentation: kunit: provide
> guidance for testing many inputs") to show a minimal example of the new
> feature.
>
> Signed-off-by: Daniel Latypov <dlatypov@google.com>
> ---
This looks good to me. Thanks!
Reviewed-by: David Gow <davidgow@google.com>
-- David
[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4000 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] Documentation: kunit: include example of a parameterized test
2020-12-16 0:22 [PATCH] Documentation: kunit: include example of a parameterized test Daniel Latypov
2020-12-16 6:28 ` David Gow
@ 2021-01-14 22:29 ` Brendan Higgins
1 sibling, 0 replies; 3+ messages in thread
From: Brendan Higgins @ 2021-01-14 22:29 UTC (permalink / raw)
To: Daniel Latypov
Cc: David Gow, Marco Elver, Arpitha Raghunandan,
Linux Kernel Mailing List, open list:KERNEL SELFTEST FRAMEWORK,
Shuah Khan
On Tue, Dec 15, 2020 at 4:23 PM Daniel Latypov <dlatypov@google.com> wrote:
>
> Commit fadb08e7c750 ("kunit: Support for Parameterized Testing")
> introduced support but lacks documentation for how to use it.
>
> This patch builds on commit 1f0e943df68a ("Documentation: kunit: provide
> guidance for testing many inputs") to show a minimal example of the new
> feature.
>
> Signed-off-by: Daniel Latypov <dlatypov@google.com>
Tested-by: Brendan Higgins <brendanhiggins@google.com>
Acked-by: Brendan Higgins <brendanhiggins@google.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-01-14 22:30 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-16 0:22 [PATCH] Documentation: kunit: include example of a parameterized test Daniel Latypov
2020-12-16 6:28 ` David Gow
2021-01-14 22:29 ` Brendan Higgins
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).