0

How to make a unit test for a function defined some configuration like what follows

struct I32Add;
impl I32Add{
    #[cfg(unstable)]
    fn add(x:i32, y:i32) -> i32{x+y}
}

#[test]
fn add_test(){
    assert_eq!(I32Add::add(1,2),3)
}

Of course, the test doesn't work. how to make it work?

markalex
  • 8,623
  • 2
  • 7
  • 32
asmmo
  • 6,922
  • 1
  • 11
  • 25
  • Does this answer your question? [Is it possible to write a test in Rust so it does not run on a specific operating system?](https://stackoverflow.com/questions/49588087/is-it-possible-to-write-a-test-in-rust-so-it-does-not-run-on-a-specific-operatin) – kmdreko Dec 17 '21 at 19:57
  • @kmdreko this disables/enables the test on enabling/disabling the feature. My question is how to make a unit test i.e. in the same crate. – asmmo Dec 17 '21 at 20:00
  • My proposal would be to add `#[cfg(unstable)]` to your test just as you've done for your function [as shown here](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9dd4bdd0b186417e76c20e6706aa5d37). That way, if the function isn't there, the test isn't there so you don't get compilation issues. Is that not what the question is about? – kmdreko Dec 17 '21 at 20:09
  • *"My question is how to make a unit test i.e. in the same crate."* - I'm not sure I understand, `add_test` *is* a unit test and its in the same crate as `I32Add`... – kmdreko Dec 17 '21 at 20:09
  • @kmdreko the test doesn't work. ``` running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s ``` – asmmo Dec 17 '21 at 20:12
  • So the question is how to check if the test passes or not in the same crate? – asmmo Dec 17 '21 at 20:13
  • @kmdreko I think u should remove the closing suggestion? – asmmo Dec 17 '21 at 20:17

2 Answers2

1

You can add #[cfg(unstable)] to your test just as you've done for your function. So the test is only compiled if that function is compiled:

#[cfg(unstable)]
#[test]
fn add_test() {
    assert_eq!(I32Add::add(1, 2), 3)
}

To get your function and the test to compile and run, you have to enable the unstable config option:

RUSTFLAGS="--cfg unstable" cargo test

However, I would recommended that you use a cargo feature instead of a config option for conditionally enabling portions of your code-base.

struct I32Add;
impl I32Add{
    #[cfg(feature = "unstable")]
    fn add(x:i32, y:i32) -> i32{x+y}
}

#[cfg(feature = "unstable")]
#[test]
fn add_test(){
    assert_eq!(I32Add::add(1,2),3)
}

with this in your cargo.toml:

[features]
unstable = []

And then run it like:

cargo test --features=unstable

See:

kmdreko
  • 42,554
  • 6
  • 57
  • 106
0

Instead of adding #[cfg(unstable)] to your test, you can always enable the function when testing (assuming the cfg doesn't protect from details such as operating system, rustc version, or any other detail that you cannot control):

impl I32Add{
    #[cfg(any(unstable, test))]
    fn add(x: i32, y: i32) -> i32 { x + y }
}
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77