Where should I put test utility functions in Rust?

前端 未结 2 462
刺人心
刺人心 2021-01-02 06:24

I have the following code defining a path where generated files can be placed:

fn gen_test_dir() -> tempdir::TempDir {                                             


        
2条回答
  •  太阳男子
    2021-01-02 06:50

    What I do is put my unit tests with any other utilities into a submodule protected with #[cfg(test)]:

    #[cfg(test)]
    mod tests {  // The contents could be a separate file if it helps organisation
        // Not a test, but available to tests.
        fn some_utility(s: String) -> u32 {
            ...
        }
    
        #[test]
        fn test_foo() {
            assert_eq!(...);
        }
        // more tests
    }
    

提交回复
热议问题