Crate uncover[][src]

uncover

A library that makes tests easier to maintain by using instrumentation to answer these two questions:

  • Which code is exercised by this test?
  • Which test covers this bit of code?

Here's a short example:

#[macro_use]
extern crate uncover;

// This defines two macros, `covers!` and `covered_by!`.
// They will be no-ops unless `cfg!(debug_assertions)` is true.
define_uncover_macros!(
    enable_if(cfg!(debug_assertions))
);

fn parse_date(s: &str) -> Option<(u32, u32, u32)> {
    if 10 != s.len() {
        // By using `covered_by!("unique_name")`
        // we signal which test exercises this code.
        covered_by!("short date");
        return None;
    }

    if "-" != &s[4..5] || "-" != &s[7..8] {
        covered_by!("wrong dashes");
        return None;
    }
    // ...
}

#[test]
fn test_parse_date() {
    {
        // `covers!("unique_name")` creates a guard object
        // that verifies that by the end of the scope we've
        // executed the corresponding `covered_by!("unique_name")`.
        covers!("short date");
        assert!(parse_date("92").is_none());
    }

//  This will fail. Although the test looks like
//  it exercises the second condition, it does not.
//  The call to `covers!` call catches this bug in the test.
//  {
//      covers!("wrong dashes");
//      assert!(parse_date("27.2.2013").is_none());
//  }

    {
        covers!("wrong dashes");
        assert!(parse_date("27.02.2013").is_none());
    }
}

Notes on concurrency

Coverage is tracked via shared mutable state, so the following caveat applies:

  • A covers! from one test might be covered by thread of another test. As a result, a test might pass when it should have failed.

The error in the opposite direction never happens: if your code covers everything with a single thread, it will do it with several threads as well.

Macros

define_uncover_macros

Define covered_by! and covers! macros.