我在 VS2010 中使用 MS 单元测试。
我创建了一个读取文件的类..只是一个静态单词列表。该文件作为内容添加到主项目和测试项目中,并选择“始终复制”作为构建事件。
但是,当我运行单元测试时,它会在单元测试的输出目录中查找文件。你如何让它在bin目录中看起来。我不想为了测试而弄乱绝对文件路径......
Test method RequiredServicesTest.RequiredServices_JamesTest.Number2WordsTest threw exception:
Test method RequiredServicesTest.RequiredServices_JamesTest.Number2WordsTest threw exception:
System.IO.FileNotFoundException: Could not find file E:\scomA3proj\TestResults\James_JAMES-PC 2012-02-24 21_22_17\Out\randomdictionary.txt.
更新:我忘记提及的一件事是,这是一个 WCF 服务库项目。
最佳答案
请考虑以下技巧以获得正确的路径:
var assembly = typeof(RequiredServices_JamesTest).Assembly;
var assemblyPath = Path.GetDirectoryName(new Uri(assembly.EscapedCodeBase).LocalPath);
var filePath = Path.Combine(assemblyPath, "randomdictionary.txt");
// You have the correct file path now!
关于c# - Visual Studio 单元测试文件路径烦恼,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9441248/