la10736 / rstest

Fixture-based test framework for Rust
Apache License 2.0
1.21k stars 43 forks source link

Include fixtures in separate files #198

Closed DawChihLiou closed 1 year ago

DawChihLiou commented 1 year ago

Hi maintainer, thanks for the amazing crate! I'm wondering if there's a way to write fixtures in separate files. Some of my fixture sizes are vary large so I would love to put them in separate files and include them in tests. Would that be possible?

la10736 commented 1 year ago

Yes it's possible to use fixtures that are in separated modules or create. You should just then import them.

DawChihLiou commented 1 year ago

@la10736 thanks for getting back to me! Are there some examples I can follow? I'm not sure how to do it just by your comment. Thanks!

la10736 commented 1 year ago

Sorry, I answered from my mobile and swipe introduced lot of craps in my answer. In https://github.com/la10736/rstest/blob/master/rstest/tests/resources/fixture/from_other_module.rs there is the example that I'm using in tests. I rewrote it here focused on reusable example

mod my_mod {
    use rstest::fixture;

    #[fixture]
    pub fn mod_fixture() -> u32 {
        42
    }
}

use my_mod::mod_fixture;
use rstest::rstest;

#[rstest]
fn example(mod_fixture: u32) {
    assert_eq!(42, mod_fixture);
}
DawChihLiou commented 1 year ago

Thank you so much @la10736. It's very clear!