Skip to content

Instantly share code, notes, and snippets.

@max-itzpapalotl
Created March 17, 2024 21:59
Show Gist options
  • Save max-itzpapalotl/7ac7041a0f9a2a6d68f29c927f50b287 to your computer and use it in GitHub Desktop.
Save max-itzpapalotl/7ac7041a0f9a2a6d68f29c927f50b287 to your computer and use it in GitHub Desktop.
27. Testing

27. Testing

In this video we show Rust's support for writing tests.

Test functions and executables

fn f(a: i64, b: i64) -> i64 {
    2 * a + 3 * b
}

fn main() {
    println!("Hello, world!\n{}", f(3, 2));
}

#[test]
fn test_f() {
    assert_eq!(f(1, 0), 2);
    assert_eq!(f(0, 1), 3);
    assert_eq!(f(-1, 1), 1);
}

Ignoring tests with [ignore]:

fn f(a: i64, b: i64) -> i64 {
    2 * a + 3 * b
}

fn main() {
    println!("Hello, world!\n{}", f(3, 2));
}

#[test]
#[ignore]
fn test_f() {
    assert_eq!(f(1, 0), 2);
    assert_eq!(f(0, 1), 3);
    assert_eq!(f(-1, 1), 1);
}

Using -- --ignored:

cargo test -- --ignored

Separate testing module

fn f(a: i64, b: i64) -> i64 {
    2 * a + 3 * b
}

fn main() {
    println!("Hello, world!\n{}", f(3, 2));
}

#[cfg(test)]
mod tests {

    use super::f;

    #[test]
    fn test_f() {
        assert_eq!(f(1, 0), 2);
        assert_eq!(f(0, 1), 3);
        assert_eq!(f(-1, 1), 1);
    }
}

Testing module in a separate file

In src/main.rs:

fn f(a: i64, b: i64) -> i64 {
    2 * a + 3 * b
}

fn main() {
    println!("Hello, world!\n{}", f(3, 2));
}

#[cfg(test)]
mod tests;

In src/tests.rs:

use super::f;

#[test]
fn test_f() {
    assert_eq!(f(1, 0), 2);
    assert_eq!(f(0, 1), 3);
    assert_eq!(f(-1, 1), 1);
}

Only running specific tests

fn f(a: i64, b: i64) -> i64 {
    2 * a + 3 * b
}

fn g(a: i64, b: i64) -> i64 {
    7 * a + 4 * b
}

fn main() {
    println!("Hello, world!\n{}", f(3, 2));
    println!("And: {}", g(5, 6));
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_f() {
        assert_eq!(f(1, 0), 2);
        assert_eq!(f(0, 1), 3);
        assert_eq!(f(-1, 1), 1);
    }

    #[test]
    fn test_g() {
        assert_eq!(g(1, 0), 7);
        assert_eq!(g(0, 1), 4);
        assert_eq!(g(1, -1), 3);
    }
}

Running only test test_f:

cargo test test_f

Integration tests:

We can only test public code in lib.rs and not in main.rs:

In src/main.rs:

use testing::f;

fn main() {
    println!("Hello, world!\n{}", f(3, 2));
}

In src/lib.rs:

pub fn f(a: i64, b: i64) -> i64 {
    2 * a + 3 * b + g(a, b)
}

fn g(a: i64, b: i64) -> i64 {
    7 * a + 4 * b
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_f() {
        assert_eq!(f(1, 0), 9);
        assert_eq!(f(0, 1), 7);
        assert_eq!(f(-1, 1), -2);
    }

    #[test]
    fn test_g() {
        assert_eq!(g(1, 0), 7);
        assert_eq!(g(0, 1), 4);
        assert_eq!(g(1, -1), 3);
    }
}

In tests/integration.rs:

#[test]
fn integration_test() {
    assert_eq!(testing::f(1, 1), 16);
}

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment