0

I need to test code that fetches and processes some data from standard C library. However, this data may not be installed at any particular system. So at the start of each test I detect whether the data is installed and skip the test.

It's easy to wrap the test in an if, but since the code that tests whether the test is possible may itself fail, I need to at least know that the tests were skipped. I know I can't simply use println! and I don't want to have to remember to pass --nocapture every time I test (and I want to see the warnings in the Travis log; Travis virtuals don't have all the data).

Community
  • 1
  • 1
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • So your question is how to show a warning if the test was skipped? – Filipe Gonçalves Mar 24 '15 at 20:01
  • Could you externally know if the data is available? If so, you might be able to use Cargo feature flags to conditionally compile (and thus run) tests. – Shepmaster Mar 24 '15 at 20:23
  • @FilipeGonçalves, yes. – Jan Hudec Mar 24 '15 at 20:23
  • @Shepmaster: No, I can't. I need to run rust code (binding to C) to find out. And it is just data, so it does not affect the functionality of the module. Just whether the particular test case can expect the particular values or not. – Jan Hudec Mar 24 '15 at 20:24
  • @Shepmaster: The data can also be installed in any combination. There are many bits and each can individually be available or not. So I may skip two tests and another two may run. – Jan Hudec Mar 24 '15 at 20:26

1 Answers1

4

A lesser-known fact is that println! uses the thread-local "standard out", which can be set to other output types. During tests, this is set to a capturing type. However, you can still get direct access to the true standard out and use that:

use std::io::{self,Write};

#[test]
fn a() {
    println!("Hi!"); // Will be captured / hidden by default
    write!(&mut io::stdout(), "Hello!").unwrap(); // Will be shown
}

Has output like:

running 1 test
Hello!test a ... ok
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366