€•¶}Œsphinx.addnodes”Œdocument”“”)�”}”(Œ rawsource”Œ”Œchildren”]”(Œ translations”Œ LanguagesNode”“”)�”}”(hhh]”(hŒ pending_xref”“”)�”}”(hhh]”Œdocutils.nodes”ŒText”“”ŒChinese (Simplified)”…”�”}”Œparent”hsbaŒ attributes”}”(Œids”]”Œclasses”]”Œnames”]”Œdupnames”]”Œbackrefs”]”Œ refdomain”Œstd”Œreftype”Œdoc”Œ reftarget”Œ /translations/zh_CN/rust/testing”Œmodname”NŒ classname”NŒ refexplicit”ˆuŒtagname”hhh ubh)�”}”(hhh]”hŒChinese (Traditional)”…”�”}”hh2sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ /translations/zh_TW/rust/testing”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒItalian”…”�”}”hhFsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ /translations/it_IT/rust/testing”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒJapanese”…”�”}”hhZsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ /translations/ja_JP/rust/testing”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒKorean”…”�”}”hhnsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ /translations/ko_KR/rust/testing”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒPortuguese (Brazilian)”…”�”}”hh‚sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ /translations/pt_BR/rust/testing”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒSpanish”…”�”}”hh–sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ /translations/sp_SP/rust/testing”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubeh}”(h]”h ]”h"]”h$]”h&]”Œcurrent_language”ŒEnglish”uh1h hhŒ _document”hŒsource”NŒline”NubhŒcomment”“”)�”}”(hŒ SPDX-License-Identifier: GPL-2.0”h]”hŒ SPDX-License-Identifier: GPL-2.0”…”�”}”hh·sbah}”(h]”h ]”h"]”h$]”h&]”Œ xml:space”Œpreserve”uh1hµhhh²hh³Œ:/var/lib/git/docbuild/linux/Documentation/rust/testing.rst”h´KubhŒsection”“”)�”}”(hhh]”(hŒtitle”“”)�”}”(hŒTesting”h]”hŒTesting”…”�”}”(hhÏh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhhÊh²hh³hÇh´KubhŒ paragraph”“”)�”}”(hŒRThis document contains useful information how to test the Rust code in the kernel.”h]”hŒRThis document contains useful information how to test the Rust code in the kernel.”…”�”}”(hhßh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KhhÊh²hubhÞ)�”}”(hŒThere are three sorts of tests:”h]”hŒThere are three sorts of tests:”…”�”}”(hhíh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K hhÊh²hubhŒ bullet_list”“”)�”}”(hhh]”(hŒ list_item”“”)�”}”(hŒThe KUnit tests.”h]”hÞ)�”}”(hjh]”hŒThe KUnit tests.”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K hjubah}”(h]”h ]”h"]”h$]”h&]”uh1jhhýh²hh³hÇh´Nubj)�”}”(hŒThe ``#[test]`` tests.”h]”hÞ)�”}”(hjh]”(hŒThe ”…”�”}”(hjh²hh³Nh´NubhŒliteral”“”)�”}”(hŒ ``#[test]``”h]”hŒ#[test]”…”�”}”(hj&h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hjubhŒ tests.”…”�”}”(hjh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K hjubah}”(h]”h ]”h"]”h$]”h&]”uh1jhhýh²hh³hÇh´Nubj)�”}”(hŒThe Kselftests. ”h]”hÞ)�”}”(hŒThe Kselftests.”h]”hŒThe Kselftests.”…”�”}”(hjHh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K hjDubah}”(h]”h ]”h"]”h$]”h&]”uh1jhhýh²hh³hÇh´Nubeh}”(h]”h ]”h"]”h$]”h&]”Œbullet”Œ-”uh1hûh³hÇh´K hhÊh²hubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒThe KUnit tests”h]”hŒThe KUnit tests”…”�”}”(hjgh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjdh²hh³hÇh´KubhÞ)�”}”(hŒqThese are the tests that come from the examples in the Rust documentation. They get transformed into KUnit tests.”h]”hŒqThese are the tests that come from the examples in the Rust documentation. They get transformed into KUnit tests.”…”�”}”(hjuh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Khjdh²hubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒUsage”h]”hŒUsage”…”�”}”(hj†h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjƒh²hh³hÇh´KubhÞ)�”}”(hŒeThese tests can be run via KUnit. For example via ``kunit_tool`` (``kunit.py``) on the command line::”h]”(hŒ2These tests can be run via KUnit. For example via ”…”�”}”(hj”h²hh³Nh´Nubj%)�”}”(hŒ``kunit_tool``”h]”hŒ kunit_tool”…”�”}”(hjœh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj”ubhŒ (”…”�”}”(hj”h²hh³Nh´Nubj%)�”}”(hŒ ``kunit.py``”h]”hŒkunit.py”…”�”}”(hj®h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj”ubhŒ) on the command line:”…”�”}”(hj”h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Khjƒh²hubhŒ literal_block”“”)�”}”(hŒb./tools/testing/kunit/kunit.py run --make_options LLVM=1 --arch x86_64 --kconfig_add CONFIG_RUST=y”h]”hŒb./tools/testing/kunit/kunit.py run --make_options LLVM=1 --arch x86_64 --kconfig_add CONFIG_RUST=y”…”�”}”hjÈsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jÆh³hÇh´Khjƒh²hubhÞ)�”}”(hXAlternatively, KUnit can run them as kernel built-in at boot. Refer to Documentation/dev-tools/kunit/index.rst for the general KUnit documentation and Documentation/dev-tools/kunit/architecture.rst for the details of kernel built-in vs. command line testing.”h]”hXAlternatively, KUnit can run them as kernel built-in at boot. Refer to Documentation/dev-tools/kunit/index.rst for the general KUnit documentation and Documentation/dev-tools/kunit/architecture.rst for the details of kernel built-in vs. command line testing.”…”�”}”(hjÖh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Khjƒh²hubhÞ)�”}”(hŒ Kernel Testing and Coverage -> KUnit - Enable support for unit tests CONFIG_RUST_KERNEL_DOCTESTS Kernel hacking -> Rust hacking -> Doctests for the `kernel` crate”h]”hŒÇCONFIG_KUNIT Kernel hacking -> Kernel Testing and Coverage -> KUnit - Enable support for unit tests CONFIG_RUST_KERNEL_DOCTESTS Kernel hacking -> Rust hacking -> Doctests for the `kernel` crate”…”�”}”hjòsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jÆh³hÇh´K$hjƒh²hubhÞ)�”}”(hŒin the kernel config system.”h]”hŒin the kernel config system.”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K)hjƒh²hubeh}”(h]”Œusage”ah ]”h"]”Œusage”ah$]”h&]”uh1hÈhjdh²hh³hÇh´KubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒ#KUnit tests are documentation tests”h]”hŒ#KUnit tests are documentation tests”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjh²hh³hÇh´K,ubhÞ)�”}”(hŒiThese documentation tests are typically examples of usage of any item (e.g. function, struct, module...).”h]”hŒiThese documentation tests are typically examples of usage of any item (e.g. function, struct, module...).”…”�”}”(hj'h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K.hjh²hubhÞ)�”}”(hŒaThey are very convenient because they are just written alongside the documentation. For instance:”h]”hŒaThey are very convenient because they are just written alongside the documentation. For instance:”…”�”}”(hj5h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K1hjh²hubjÇ)�”}”(hŒ}/// Sums two numbers. /// /// ``` /// assert_eq!(mymod::f(10, 20), 30); /// ``` pub fn f(a: i32, b: i32) -> i32 { a + b }”h]”hŒ}/// Sums two numbers. /// /// ``` /// assert_eq!(mymod::f(10, 20), 30); /// ``` pub fn f(a: i32, b: i32) -> i32 { a + b }”…”�”}”hjCsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆŒforce”‰Œlanguage”Œrust”Œhighlight_args”}”uh1jÆh³hÇh´K4hjh²hubhÞ)�”}”(hX"In userspace, the tests are collected and run via ``rustdoc``. Using the tool as-is would be useful already, since it allows verifying that examples compile (thus enforcing they are kept in sync with the code they document) and as well as running those that do not depend on in-kernel APIs.”h]”(hŒ2In userspace, the tests are collected and run via ”…”�”}”(hjVh²hh³Nh´Nubj%)�”}”(hŒ ``rustdoc``”h]”hŒrustdoc”…”�”}”(hj^h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hjVubhŒå. Using the tool as-is would be useful already, since it allows verifying that examples compile (thus enforcing they are kept in sync with the code they document) and as well as running those that do not depend on in-kernel APIs.”…”�”}”(hjVh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K?hjh²hubhÞ)�”}”(hŒ·For the kernel, however, these tests get transformed into KUnit test suites. This means that doctests get compiled as Rust kernel objects, allowing them to run against a built kernel.”h]”hŒ·For the kernel, however, these tests get transformed into KUnit test suites. This means that doctests get compiled as Rust kernel objects, allowing them to run against a built kernel.”…”�”}”(hjvh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KDhjh²hubhÞ)�”}”(hŒ’A benefit of this KUnit integration is that Rust doctests get to reuse existing testing facilities. For instance, the kernel log would look like::”h]”hŒ‘A benefit of this KUnit integration is that Rust doctests get to reuse existing testing facilities. For instance, the kernel log would look like:”…”�”}”(hj„h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KHhjh²hubjÇ)�”}”(hX¼KTAP version 1 1..1 KTAP version 1 # Subtest: rust_doctests_kernel 1..59 # rust_doctest_kernel_build_assert_rs_0.location: rust/kernel/build_assert.rs:13 ok 1 rust_doctest_kernel_build_assert_rs_0 # rust_doctest_kernel_build_assert_rs_1.location: rust/kernel/build_assert.rs:56 ok 2 rust_doctest_kernel_build_assert_rs_1 # rust_doctest_kernel_init_rs_0.location: rust/kernel/init.rs:122 ok 3 rust_doctest_kernel_init_rs_0 ... # rust_doctest_kernel_types_rs_2.location: rust/kernel/types.rs:150 ok 59 rust_doctest_kernel_types_rs_2 # rust_doctests_kernel: pass:59 fail:0 skip:0 total:59 # Totals: pass:59 fail:0 skip:0 total:59 ok 1 rust_doctests_kernel”h]”hX¼KTAP version 1 1..1 KTAP version 1 # Subtest: rust_doctests_kernel 1..59 # rust_doctest_kernel_build_assert_rs_0.location: rust/kernel/build_assert.rs:13 ok 1 rust_doctest_kernel_build_assert_rs_0 # rust_doctest_kernel_build_assert_rs_1.location: rust/kernel/build_assert.rs:56 ok 2 rust_doctest_kernel_build_assert_rs_1 # rust_doctest_kernel_init_rs_0.location: rust/kernel/init.rs:122 ok 3 rust_doctest_kernel_init_rs_0 ... # rust_doctest_kernel_types_rs_2.location: rust/kernel/types.rs:150 ok 59 rust_doctest_kernel_types_rs_2 # rust_doctests_kernel: pass:59 fail:0 skip:0 total:59 # Totals: pass:59 fail:0 skip:0 total:59 ok 1 rust_doctests_kernel”…”�”}”hj’sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jÆh³hÇh´KKhjh²hubhÞ)�”}”(hŒ Tests using the `? `_ operator are also supported as usual, e.g.:”h]”(hŒTests using the ”…”�”}”(hj h²hh³Nh´NubhŒ reference”“”)�”}”(hŒd`? `_”h]”hŒ?”…”�”}”(hjªh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œname”Œ?”Œrefuri”Œ]https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator”uh1j¨hj ubhŒtarget”“”)�”}”(hŒ` ”h]”h}”(h]”Œid1”ah ]”h"]”Œ?”ah$]”h&]”Œrefuri”j»uh1j¼Œ referenced”Khj ubhŒ, operator are also supported as usual, e.g.:”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K]hjh²hubjÇ)�”}”(hŒ™/// ``` /// # use kernel::{spawn_work_item, workqueue}; /// spawn_work_item!(workqueue::system(), || pr_info!("x\n"))?; /// # Ok::<(), Error>(()) /// ```”h]”hŒ™/// ``` /// # use kernel::{spawn_work_item, workqueue}; /// spawn_work_item!(workqueue::system(), || pr_info!("x\n"))?; /// # Ok::<(), Error>(()) /// ```”…”�”}”hjÖsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆjQ‰jRŒrust”jT}”uh1jÆh³hÇh´K`hjh²hubhÞ)�”}”(hŒ|The tests are also compiled with Clippy under ``CLIPPY=1``, just like normal code, thus also benefitting from extra linting.”h]”(hŒ.The tests are also compiled with Clippy under ”…”�”}”(hjæh²hh³Nh´Nubj%)�”}”(hŒ ``CLIPPY=1``”h]”hŒCLIPPY=1”…”�”}”(hjîh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hjæubhŒB, just like normal code, thus also benefitting from extra linting.”…”�”}”(hjæh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Khhjh²hubhÞ)�”}”(hŒüIn order for developers to easily see which line of doctest code caused a failure, a KTAP diagnostic line is printed to the log. This contains the location (file and line) of the original test (i.e. instead of the location in the generated Rust file)::”h]”hŒûIn order for developers to easily see which line of doctest code caused a failure, a KTAP diagnostic line is printed to the log. This contains the location (file and line) of the original test (i.e. instead of the location in the generated Rust file):”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Kkhjh²hubjÇ)�”}”(hŒC# rust_doctest_kernel_types_rs_2.location: rust/kernel/types.rs:150”h]”hŒC# rust_doctest_kernel_types_rs_2.location: rust/kernel/types.rs:150”…”�”}”hjsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jÆh³hÇh´Kphjh²hubhÞ)�”}”(hXRust tests appear to assert using the usual ``assert!`` and ``assert_eq!`` macros from the Rust standard library (``core``). We provide a custom version that forwards the call to KUnit instead. Importantly, these macros do not require passing context, unlike those for KUnit testing (i.e. ``struct kunit *``). This makes them easier to use, and readers of the documentation do not need to care about which testing framework is used. In addition, it may allow us to test third-party code more easily in the future.”h]”(hŒ,Rust tests appear to assert using the usual ”…”�”}”(hj"h²hh³Nh´Nubj%)�”}”(hŒ ``assert!``”h]”hŒassert!”…”�”}”(hj*h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj"ubhŒ and ”…”�”}”(hj"h²hh³Nh´Nubj%)�”}”(hŒ``assert_eq!``”h]”hŒ assert_eq!”…”�”}”(hj<h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj"ubhŒ( macros from the Rust standard library (”…”�”}”(hj"h²hh³Nh´Nubj%)�”}”(hŒ``core``”h]”hŒcore”…”�”}”(hjNh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj"ubhŒ§). We provide a custom version that forwards the call to KUnit instead. Importantly, these macros do not require passing context, unlike those for KUnit testing (i.e. ”…”�”}”(hj"h²hh³Nh´Nubj%)�”}”(hŒ``struct kunit *``”h]”hŒstruct kunit *”…”�”}”(hj`h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj"ubhŒÎ). This makes them easier to use, and readers of the documentation do not need to care about which testing framework is used. In addition, it may allow us to test third-party code more easily in the future.”…”�”}”(hj"h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Krhjh²hubhÞ)�”}”(hŒæA current limitation is that KUnit does not support assertions in other tasks. Thus, we presently simply print an error to the kernel log if an assertion actually failed. Additionally, doctests are not run for nonpublic functions.”h]”hŒæA current limitation is that KUnit does not support assertions in other tasks. Thus, we presently simply print an error to the kernel log if an assertion actually failed. Additionally, doctests are not run for nonpublic functions.”…”�”}”(hjxh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Kzhjh²hubhÞ)�”}”(hŒöSince these tests are examples, i.e. they are part of the documentation, they should generally be written like "real code". Thus, for example, instead of using ``unwrap()`` or ``expect()``, use the ``?`` operator. For more background, please see:”h]”(hŒ¤Since these tests are examples, i.e. they are part of the documentation, they should generally be written like “real codeâ€�. Thus, for example, instead of using ”…”�”}”(hj†h²hh³Nh´Nubj%)�”}”(hŒ ``unwrap()``”h]”hŒunwrap()”…”�”}”(hjŽh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj†ubhŒ or ”…”�”}”(hj†h²hh³Nh´Nubj%)�”}”(hŒ ``expect()``”h]”hŒexpect()”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj†ubhŒ , use the ”…”�”}”(hj†h²hh³Nh´Nubj%)�”}”(hŒ``?``”h]”hŒ?”…”�”}”(hj²h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj†ubhŒ+ operator. For more background, please see:”…”�”}”(hj†h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K~hjh²hubhŒ block_quote”“”)�”}”(hŒUhttps://rust.docs.kernel.org/kernel/error/type.Result.html#error-codes-in-c-and-rust ”h]”hÞ)�”}”(hŒThttps://rust.docs.kernel.org/kernel/error/type.Result.html#error-codes-in-c-and-rust”h]”j©)�”}”(hjÒh]”hŒThttps://rust.docs.kernel.org/kernel/error/type.Result.html#error-codes-in-c-and-rust”…”�”}”(hjÔh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œrefuri”jÒuh1j¨hjÐubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KƒhjÌubah}”(h]”h ]”h"]”h$]”h&]”uh1jÊh³hÇh´Kƒhjh²hubeh}”(h]”Œ#kunit-tests-are-documentation-tests”ah ]”h"]”Œ#kunit tests are documentation tests”ah$]”h&]”uh1hÈhjdh²hh³hÇh´K,ubeh}”(h]”Œthe-kunit-tests”ah ]”h"]”Œthe kunit tests”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´KubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒThe ``#[test]`` tests”h]”(hŒThe ”…”�”}”(hjh²hh³Nh´Nubj%)�”}”(hŒ ``#[test]``”h]”hŒ#[test]”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hjubhŒ tests”…”�”}”(hjh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjþh²hh³hÇh´K†ubhÞ)�”}”(hŒ¶Additionally, there are the ``#[test]`` tests. Like for documentation tests, these are also fairly similar to what you would expect from userspace, and they are also mapped to KUnit.”h]”(hŒAdditionally, there are the ”…”�”}”(hj!h²hh³Nh´Nubj%)�”}”(hŒ ``#[test]``”h]”hŒ#[test]”…”�”}”(hj)h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj!ubhŒ� tests. Like for documentation tests, these are also fairly similar to what you would expect from userspace, and they are also mapped to KUnit.”…”�”}”(hj!h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´Kˆhjþh²hubhÞ)�”}”(hŒzThese tests are introduced by the ``kunit_tests`` procedural macro, which takes the name of the test suite as an argument.”h]”(hŒ"These tests are introduced by the ”…”�”}”(hjAh²hh³Nh´Nubj%)�”}”(hŒ``kunit_tests``”h]”hŒ kunit_tests”…”�”}”(hjIh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hjAubhŒI procedural macro, which takes the name of the test suite as an argument.”…”�”}”(hjAh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KŒhjþh²hubhÞ)�”}”(hŒVEach test suite should be guarded by a Kconfig option in ``rust/kernel/Kconfig.test``.”h]”(hŒ9Each test suite should be guarded by a Kconfig option in ”…”�”}”(hjah²hh³Nh´Nubj%)�”}”(hŒ``rust/kernel/Kconfig.test``”h]”hŒrust/kernel/Kconfig.test”…”�”}”(hjih²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hjaubhŒ.”…”�”}”(hjah²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K�hjþh²hubhÞ)�”}”(hŒšFor instance, assume we want to test the function ``f`` from the documentation tests section. We could write, in the same file where we have our function:”h]”(hŒ2For instance, assume we want to test the function ”…”�”}”(hj�h²hh³Nh´Nubj%)�”}”(hŒ``f``”h]”hŒf”…”�”}”(hj‰h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj�ubhŒc from the documentation tests section. We could write, in the same file where we have our function:”…”�”}”(hj�h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K’hjþh²hubjÇ)�”}”(hŒ®#[cfg(CONFIG_RUST_MYMOD_KUNIT_TEST)] #[kunit_tests(rust_kernel_mymod)] mod tests { use super::*; #[test] fn test_f() { assert_eq!(f(10, 20), 30); } }”h]”hŒ®#[cfg(CONFIG_RUST_MYMOD_KUNIT_TEST)] #[kunit_tests(rust_kernel_mymod)] mod tests { use super::*; #[test] fn test_f() { assert_eq!(f(10, 20), 30); } }”…”�”}”hj¡sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆjQ‰jRŒrust”jT}”uh1jÆh³hÇh´K•hjþh²hubhÞ)�”}”(hŒ2And if we run it, the kernel log would look like::”h]”hŒ1And if we run it, the kernel log would look like:”…”�”}”(hj±h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K¢hjþh²hubjÇ)�”}”(hŒ’ KTAP version 1 # Subtest: rust_kernel_mymod # speed: normal 1..1 # test_f.speed: normal ok 1 test_f ok 1 rust_kernel_mymod”h]”hŒ’ KTAP version 1 # Subtest: rust_kernel_mymod # speed: normal 1..1 # test_f.speed: normal ok 1 test_f ok 1 rust_kernel_mymod”…”�”}”hj¿sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jÆh³hÇh´K¤hjþh²hubhÞ)�”}”(hX€Like documentation tests, the ``assert!`` and ``assert_eq!`` macros are mapped back to KUnit and do not panic. Similarly, the `? `_ operator is supported, i.e. the test functions may return either nothing (i.e. the unit type ``()``) or ``Result`` (i.e. any ``Result``). For instance:”h]”(hŒLike documentation tests, the ”…”�”}”(hjÍh²hh³Nh´Nubj%)�”}”(hŒ ``assert!``”h]”hŒassert!”…”�”}”(hjÕh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hjÍubhŒ and ”…”�”}”(hjÍh²hh³Nh´Nubj%)�”}”(hŒ``assert_eq!``”h]”hŒ assert_eq!”…”�”}”(hjçh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hjÍubhŒB macros are mapped back to KUnit and do not panic. Similarly, the ”…”�”}”(hjÍh²hh³Nh´Nubj©)�”}”(hŒd`? `_”h]”hŒ?”…”�”}”(hjùh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œname”j¹jºŒ]https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator”uh1j¨hjÍubj½)�”}”(hŒ` ”h]”h}”(h]”Œid2”ah ]”h"]”h$]”Œ?”ah&]”Œrefuri”juh1j¼jËKhjÍubhŒ^ operator is supported, i.e. the test functions may return either nothing (i.e. the unit type ”…”�”}”(hjÍh²hh³Nh´Nubj%)�”}”(hŒ``()``”h]”hŒ()”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hjÍubhŒ) or ”…”�”}”(hjÍh²hh³Nh´Nubj%)�”}”(hŒ ``Result``”h]”hŒResult”…”�”}”(hj,h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hjÍubhŒ (i.e. any ”…”�”}”(hjÍh²hh³Nh´Nubj%)�”}”(hŒ``Result``”h]”hŒ Result”…”�”}”(hj>h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hjÍubhŒ). For instance:”…”�”}”(hjÍh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´K¬hjþh²hubjÇ)�”}”(hŒÕ#[cfg(CONFIG_RUST_MYMOD_KUNIT_TEST)] #[kunit_tests(rust_kernel_mymod)] mod tests { use super::*; #[test] fn test_g() -> Result { let x = g()?; assert_eq!(x, 30); Ok(()) } }”h]”hŒÕ#[cfg(CONFIG_RUST_MYMOD_KUNIT_TEST)] #[kunit_tests(rust_kernel_mymod)] mod tests { use super::*; #[test] fn test_g() -> Result { let x = g()?; assert_eq!(x, 30); Ok(()) } }”…”�”}”hjVsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆjQ‰jRŒrust”jT}”uh1jÆh³hÇh´K²hjþh²hubhÞ)�”}”(hŒPIf we run the test and the call to ``g`` fails, then the kernel log would show::”h]”(hŒ#If we run the test and the call to ”…”�”}”(hjfh²hh³Nh´Nubj%)�”}”(hŒ``g``”h]”hŒg”…”�”}”(hjnh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hjfubhŒ' fails, then the kernel log would show:”…”�”}”(hjfh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KÁhjþh²hubjÇ)�”}”(hX KTAP version 1 # Subtest: rust_kernel_mymod # speed: normal 1..1 # test_g: ASSERTION FAILED at rust/kernel/lib.rs:335 Expected is_test_result_ok(test_g()) to be true, but is false # test_g.speed: normal not ok 1 test_g not ok 1 rust_kernel_mymod”h]”hX KTAP version 1 # Subtest: rust_kernel_mymod # speed: normal 1..1 # test_g: ASSERTION FAILED at rust/kernel/lib.rs:335 Expected is_test_result_ok(test_g()) to be true, but is false # test_g.speed: normal not ok 1 test_g not ok 1 rust_kernel_mymod”…”�”}”hj†sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jÆh³hÇh´KÃhjþh²hubhÞ)�”}”(hŒÐIf a ``#[test]`` test could be useful as an example for the user, then please use a documentation test instead. Even edge cases of an API, e.g. error or boundary cases, can be interesting to show in examples.”h]”(hŒIf a ”…”�”}”(hj”h²hh³Nh´Nubj%)�”}”(hŒ ``#[test]``”h]”hŒ#[test]”…”�”}”(hjœh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj”ubhŒÀ test could be useful as an example for the user, then please use a documentation test instead. Even edge cases of an API, e.g. error or boundary cases, can be interesting to show in examples.”…”�”}”(hj”h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KÍhjþh²hubeh}”(h]”Œthe-test-tests”ah ]”h"]”Œthe #[test] tests”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´K†ubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒThe ``rusttest`` host tests”h]”(hŒThe ”…”�”}”(hj¿h²hh³Nh´Nubj%)�”}”(hŒ ``rusttest``”h]”hŒrusttest”…”�”}”(hjÇh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj¿ubhŒ host tests”…”�”}”(hj¿h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÍhj¼h²hh³hÇh´KÒubhÞ)�”}”(hŒ’These are userspace tests that can be built and run in the host (i.e. the one that performs the kernel build) using the ``rusttest`` Make target::”h]”(hŒxThese are userspace tests that can be built and run in the host (i.e. the one that performs the kernel build) using the ”…”�”}”(hjßh²hh³Nh´Nubj%)�”}”(hŒ ``rusttest``”h]”hŒrusttest”…”�”}”(hjçh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hjßubhŒ Make target:”…”�”}”(hjßh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KÔhj¼h²hubjÇ)�”}”(hŒmake LLVM=1 rusttest”h]”hŒmake LLVM=1 rusttest”…”�”}”hjÿsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jÆh³hÇh´K×hj¼h²hubhÞ)�”}”(hŒ%This requires the kernel ``.config``.”h]”(hŒThis requires the kernel ”…”�”}”(hj h²hh³Nh´Nubj%)�”}”(hŒ ``.config``”h]”hŒ.config”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj ubhŒ.”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KÙhj¼h²hubhÞ)�”}”(hŒLCurrently, they are mostly used for testing the ``macros`` crate's examples.”h]”(hŒ0Currently, they are mostly used for testing the ”…”�”}”(hj-h²hh³Nh´Nubj%)�”}”(hŒ ``macros``”h]”hŒmacros”…”�”}”(hj5h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj-ubhŒ crate’s examples.”…”�”}”(hj-h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KÛhj¼h²hubeh}”(h]”Œthe-rusttest-host-tests”ah ]”h"]”Œthe rusttest host tests”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´KÒubhÉ)�”}”(hhh]”(hÎ)�”}”(hŒThe Kselftests”h]”hŒThe Kselftests”…”�”}”(hjXh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÍhjUh²hh³hÇh´KÞubhÞ)�”}”(hŒMKselftests are also available in the ``tools/testing/selftests/rust`` folder.”h]”(hŒ%Kselftests are also available in the ”…”�”}”(hjfh²hh³Nh´Nubj%)�”}”(hŒ ``tools/testing/selftests/rust``”h]”hŒtools/testing/selftests/rust”…”�”}”(hjnh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hjfubhŒ folder.”…”�”}”(hjfh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KàhjUh²hubhÞ)�”}”(hŒ´The kernel config options required for the tests are listed in the ``tools/testing/selftests/rust/config`` file and can be included with the aid of the ``merge_config.sh`` script::”h]”(hŒCThe kernel config options required for the tests are listed in the ”…”�”}”(hj†h²hh³Nh´Nubj%)�”}”(hŒ'``tools/testing/selftests/rust/config``”h]”hŒ#tools/testing/selftests/rust/config”…”�”}”(hjŽh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj†ubhŒ. file and can be included with the aid of the ”…”�”}”(hj†h²hh³Nh´Nubj%)�”}”(hŒ``merge_config.sh``”h]”hŒmerge_config.sh”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j$hj†ubhŒ script:”…”�”}”(hj†h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KâhjUh²hubjÇ)�”}”(hŒM./scripts/kconfig/merge_config.sh .config tools/testing/selftests/rust/config”h]”hŒM./scripts/kconfig/merge_config.sh .config tools/testing/selftests/rust/config”…”�”}”hj¸sbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jÆh³hÇh´KæhjUh²hubhÞ)�”}”(hŒƒThe kselftests are built within the kernel source tree and are intended to be executed on a system that is running the same kernel.”h]”hŒƒThe kselftests are built within the kernel source tree and are intended to be executed on a system that is running the same kernel.”…”�”}”(hjÆh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KèhjUh²hubhÞ)�”}”(hŒŠOnce a kernel matching the source tree has been installed and booted, the tests can be compiled and executed using the following command::”h]”hŒ‰Once a kernel matching the source tree has been installed and booted, the tests can be compiled and executed using the following command:”…”�”}”(hjÔh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KëhjUh²hubjÇ)�”}”(hŒmake TARGETS="rust" kselftest”h]”hŒmake TARGETS="rust" kselftest”…”�”}”hjâsbah}”(h]”h ]”h"]”h$]”h&]”hÅhÆuh1jÆh³hÇh´KîhjUh²hubhÞ)�”}”(hŒWRefer to Documentation/dev-tools/kselftest.rst for the general Kselftest documentation.”h]”hŒWRefer to Documentation/dev-tools/kselftest.rst for the general Kselftest documentation.”…”�”}”(hjðh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝh³hÇh´KðhjUh²hubeh}”(h]”Œthe-kselftests”ah ]”h"]”Œthe kselftests”ah$]”h&]”uh1hÈhhÊh²hh³hÇh´KÞubeh}”(h]”Œtesting”ah ]”h"]”Œtesting”ah$]”h&]”uh1hÈhhh²hh³hÇh´Kubeh}”(h]”h ]”h"]”h$]”h&]”Œsource”hÇuh1hŒcurrent_source”NŒ current_line”NŒsettings”Œdocutils.frontend”ŒValues”“”)�”}”(hÍNŒ generator”NŒ datestamp”NŒ source_link”NŒ source_url”NŒ toc_backlinks”Œentry”Œfootnote_backlinks”KŒ sectnum_xform”KŒstrip_comments”NŒstrip_elements_with_classes”NŒ strip_classes”NŒ report_level”KŒ halt_level”KŒexit_status_level”KŒdebug”NŒwarning_stream”NŒ traceback”ˆŒinput_encoding”Œ utf-8-sig”Œinput_encoding_error_handler”Œstrict”Œoutput_encoding”Œutf-8”Œoutput_encoding_error_handler”j1Œerror_encoding”Œutf-8”Œerror_encoding_error_handler”Œbackslashreplace”Œ language_code”Œen”Œrecord_dependencies”NŒconfig”NŒ id_prefix”hŒauto_id_prefix”Œid”Œ dump_settings”NŒdump_internals”NŒdump_transforms”NŒdump_pseudo_xml”NŒexpose_internals”NŒstrict_visitor”NŒ_disable_config”NŒ_source”hÇŒ _destination”NŒ _config_files”]”Œ7/var/lib/git/docbuild/linux/Documentation/docutils.conf”aŒfile_insertion_enabled”ˆŒ raw_enabled”KŒline_length_limit”M'Œpep_references”NŒ pep_base_url”Œhttps://peps.python.org/”Œpep_file_url_template”Œpep-%04d”Œrfc_references”NŒ rfc_base_url”Œ&https://datatracker.ietf.org/doc/html/”Œ tab_width”KŒtrim_footnote_reference_space”‰Œsyntax_highlight”Œlong”Œ smart_quotes”ˆŒsmartquotes_locales”]”Œcharacter_level_inline_markup”‰Œdoctitle_xform”‰Œ docinfo_xform”KŒsectsubtitle_xform”‰Œ image_loading”Œlink”Œembed_stylesheet”‰Œcloak_email_addresses”ˆŒsection_self_link”‰Œenv”NubŒreporter”NŒindirect_targets”]”Œsubstitution_defs”}”Œsubstitution_names”}”Œrefnames”}”Œrefids”}”Œnameids”}”(j jjûjøjjjójðjÇjÄj¹j¶jRjOjjuŒ nametypes”}”(j ‰jû‰j‰jó‰jLjj¹‰jR‰j‰uh}”(jhÊjøjdjjƒjðjjÄj¾j¶jþjj jOj¼jjUuŒ footnote_refs”}”Œ citation_refs”}”Œ autofootnotes”]”Œautofootnote_refs”]”Œsymbol_footnotes”]”Œsymbol_footnote_refs”]”Œ footnotes”]”Œ citations”]”Œautofootnote_start”KŒsymbol_footnote_start”KŒ id_counter”Œ collections”ŒCounter”“”}”j?Ks…”R”Œparse_messages”]”hŒsystem_message”“”)�”}”(hhh]”hÞ)�”}”(hŒ$Duplicate explicit target name: "?".”h]”hŒ(Duplicate explicit target name: “?â€�.”…”�”}”(hj˜h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÝhj•ubah}”(h]”h ]”h"]”h$]”h&]”jaŒlevel”KŒtype”ŒINFO”Œsource”hÇŒline”Kuh1j“hjþh²hh³hÇh´K°ubaŒtransform_messages”]”Œ transformer”NŒ include_log”]”Œ decoration”Nh²hub.