在 Rust 测试中使用外部文件

7

我正在编写一个服务,从文件中读取内容,该文件的位置是从环境变量FILE_LOCATION读取的。

fn my_function(){
    let path = match env::var("FILE_LOCATION") {
    ...
    let contents = match fs::read(&path) {
}

我有一个测试文件 test_file.json,位于:

proj
  resources
    test
      test_file.json
  src
    main.rs
  tests
    unit_tests.rs


在测试中,我可以设置FILE_LOCATION:
env::set_var("FILE_LOCATION", "path/to/test/file");

问题是path/to/test/file的值应该是什么?
换句话说:
如何在Rust测试中访问外部文件?
1个回答

12

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接