c# - Visual Studio 单元测试文件路径烦恼

标签 c# visual-studio unit-testing

我在 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/

相关文章:

javascript - Mocha 测试 Promise 仍停留在流程中(已锁定)

c# - 基于用户权限的自定义认证和授权

visual-studio - Xamarin Visual Studio - 不支持的 Major.minor 版本 52.0

c# - 对生成不同字符串的类进行单元测试

c++ - VS 2015 C++ Redistributable 不在单个 DLL 中?

visual-studio - 在 Visual Studio 2010 中开发和单击 CTRL 时进行实时编译器检查?

c# - .NET Core 单元测试未显示在 AppVeyor 测试窗口(和徽章)中

unit-testing - 如何使用@testing-library/react-native 监视 React Native 中的 TouchableNativeFeedback onPress?

c# - 级联下拉列表数据源

c# - 无法从 Task<> 隐式转换类型