12 unstable releases (3 breaking)

new 0.10.1 Dec 9, 2024
0.10.0 Dec 2, 2024
0.9.4 Oct 25, 2024
0.9.0 Sep 30, 2024
0.1.0 Jul 1, 2024

#409 in Testing

Download history 156/week @ 2024-08-18 254/week @ 2024-08-25 52/week @ 2024-09-01 179/week @ 2024-09-08 215/week @ 2024-09-15 105/week @ 2024-09-22 281/week @ 2024-09-29 189/week @ 2024-10-06 425/week @ 2024-10-13 215/week @ 2024-10-20 106/week @ 2024-10-27 60/week @ 2024-11-03 41/week @ 2024-11-10 44/week @ 2024-11-17 29/week @ 2024-11-24 186/week @ 2024-12-01

306 downloads per month
Used in 2 crates (via ostd)

MPL-2.0 license

10KB
128 lines

The kernel mode testing framework of OSTD.

ostd-test stands for kernel-mode testing framework for OSTD. Its goal is to provide a cargo test-like experience for any #![no_std] bare metal crates.

In OSTD, all the tests written in the source tree of the crates will be run immediately after the initialization of ostd. Thus you can use any feature provided by the frame including the heap allocator, etc.

By all means, ostd-test is an individual crate that only requires:

  • a custom linker script section .ktest_array,
  • and an alloc implementation.

And the OSTD happens to provide both of them. Thus, any crates depending on the OSTD can use ostd-test without any extra dependency.

Usage

To write a unit test for any crates, it is recommended to create a new test module, e.g.:

#[cfg(ktest)]
mod test {
    use ostd::prelude::*;

    #[ktest]
    fn trivial_assertion() {
        assert_eq!(0, 0);
    }
    #[ktest]
    #[should_panic]
    fn failing_assertion() {
        assert_eq!(0, 1);
    }
    #[ktest]
    #[should_panic(expected = "expected panic message")]
    fn expect_panic() {
        panic!("expected panic message");
    }
}

Any crates using the ostd-test framework should be linked with ostd.

By the way, #[ktest] attribute along also works, but it hinders test control using cfgs since plain attribute marked test will be executed in all test runs no matter what cfgs are passed to the compiler. More importantly, using #[ktest] without cfgs occupies binary real estate since the .ktest_array section is not explicitly stripped in normal builds.

Rust cfg is used to control the compilation of the test module. In cooperation with the ktest framework, OSDK will set the RUSTFLAGS environment variable to pass the cfgs to all rustc invocations. To run the tests, you simply need to use the command cargo osdk test in the crate directory. For more information, please refer to the OSDK documentation.

We support the #[should_panic] attribute just in the same way as the standard library do, but the implementation is quite slow currently. Use it with cautious.

Doctest is not taken into consideration yet, and the interface is subject to change.

No runtime deps