简体   繁体   中英

Rust Integration Tests Structure

I'd like to split my integration tests into multiple files (because it's getting long). This can be easily done:

/tests/
    /data/      // <- dataset for testing
    a_tests.rs // <- this contains "common" functions which I'd like to use in b_tests.rs
    b_tests.rs

However, a_tests.rs contains some functions which I'd like to use in b_tests.rs . For example a common Lazy once_cell which reads the data file only once. And also a fn common_func(_) which does assertion based on numerical comparisons of the results.

So how do I import objects from a_tests.rs in b_tests.rs ?

Per the Rust documentation on integration tests each file in the tests directory is compiled into a separate crate, but:

Files in subdirectories of the tests directory don't get compiled as separate crates or have sections in the test output. So you can have this file structure:

/tests/
    /common/
        mod.rs
    /data/
    a_tests.rs
    b_tests.rs

Then move your shared functions from a_tests.rs into common/mod.rs and you can import them into a_tests.rs and b_tests.rs via mod common;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM