Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test a code in a sub-sub-directory?

In Rust, how do I test a code in a sub-sub-directory, with cargo test command?

program
 `─ src
 |   `─ main.rs
 `─ tests
     `─ foo
         `─ foo.rs

main.rs:

fn main() {
}

foo.rs:

mod test_foo {
    #[test]
    fn test_foo() {
        assert!(true);
    }
}
like image 598
signal Avatar asked Apr 22 '26 15:04

signal


1 Answers

One way would be to create tests/tests.rs with the following contents:

mod foo {
    mod foo; // this will include `tests/foo/foo.rs`
}

If you run cargo test after this, it will run the test_foo test function:

$ cargo test
     Running target/debug/tests-0b79a5e208e85ac6

running 1 test
test foo::foo::test_foo::test_foo ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
like image 66
Dogbert Avatar answered Apr 24 '26 13:04

Dogbert