Ignore

things in Rust test

2022/07/31 COSCUP

Antonio Yang

Test in Rust

Rust offers a good tool for testing, we can quickly run tests with test cases, examples, and documents, and also can be selected by features with cargo

cargo test

Basic use

cargo test add_

With filter

cargo test --examples
cargo test --example=add-twice

Filter by example

cargo test

Filter by features

cargo test --features=redundant

It is awesome, but...

Issues with Rust test

Integration test is not well considered in Rust

  • Run test when corresponding component existing

Integration test

  • Ignore when corresponding component is non-existing

Test

Use envar to ignore

  • Run test when envar existing
  • Ignore when envar absent

Test

Use envar to ignore

Hard to know the test ignored

Alway pass in the test case when ignoring

Not really know why ignoring

Use envar to ignore

Check envar when test running

Conditions:  envar, file, http service, tcp socket, user, cpu, .. etc.

cargo test

Ignore message need Rust version after 1.61
related RP: #92714 #94566

Show ignore in the test case when ignoring

Show ignore message

Check condition when test case compiling

Let's try ignore in run time

Ignore in runtime

Integration test is not well considered in Rust

How a testcase run with cargo test

Buildtime vs Runtime

#[test]
fn test_case () {

  ignore!("some message")

}

#[test]

#[ignore]
fn test_case () {

}

Note: ignore! is not really defined in Rust

#[test]
fn test_case () {

  ignore!("some message")

}

Similar patten with panic

#[test]
fn test_case () {

  panic!("some message")

}

Note: ignore! is not really defined in Rust

Let's look at panic!

Define return code

Test result base on return code

Parse panic message

library/test/src/test_result.rs

Ignore-by-panic

More than runtime ignore...

Still more...

cargo test -- --include-ignored
cargo test -- --ignored

Run ignored and not ignored tests

Run only ignored tests

These two options are designed for ignoring in build time, and not suitable for ignoring in runtime

Possible way to handle ignore in runtime

  • fn test::mark_ignored — mark the current test as ignored; continue.
  • fn test::mark_failed — mark the current test as failed; continue.
  • fn test::ignore — mark the current test as ignored; continue if --include-ignored, otherwise abort (unwind).
  • fn test::skip — mark the current test as ignored; if --include-ignored, fail; abort (unwind).
    • Could be omitted, as it's just an alias for {test::ignore(); test::fail()}.
  • fn test::fail — mark the current test as failed; abort (unwind).

A true requirement cannot be ignored

If ignored, it will grow savagely

If conditional statements

in side a String

A lot of documents for a small test because there is no standard way to do this

Any idea?

Collect other's idea

Reference

Thank You!

yanganto

yanganto