Search code examples
unit-testingrust

How to skip a test in Rust based on a constant?


I have a constant

const SIZE: usize = 8;

and I have a bunch of tests. Some tests are for when this constant is larger than 8, some for when its lower than 8, as I test some special cases. How can I skip tests based on this? For example, one such test uses the type u8, and cannot be used if the SIZE is larger than 8. Based on How to conditionally skip tests based on runtime information?, there is no way to do this at runtime, but since I have everything at compile time, is there a way looking like

#[cfg(SIZE <= 8)]

to compile this test only if the constant is in the right range?

Example of test

#[cfg(test)]
mod test {
  use super::SIZE;
  #[test]
  fn will_panic_if_size_small() {
    assert!(SIZE > 8);
  }

  #[test]
  fn will_panic_if_size_big() {
    assert!(SIZE <= 8);
  }
}

Solution

  • Unfortunately, this is still not possible. Macros cannot evaluate consts, as they are executed before constant evaluation, so it is equivalent to runtime condition.