简体   繁体   English

如何在VS 2008单元测试中使用数据文件?

[英]How can I use a data file in a VS 2008 unit test?

I have the files checked in to svn. 我已将文件签入svn。 The key is that I don't want to hardcode the paths to the files. 关键是我不想对文件的路径进行硬编码。 How can I use relative paths and be able to find the data files consistantly? 如何使用相对路径并能够一致地找到数据文件?

The best way to do this is as follows: 最好的方法如下:

  1. Add a directory called "Test Data" to your VS2008 or VS2010 Test Project. 将名为“测试数据”的目录添加到VS2008或VS2010测试项目中。
  2. Put your data file in that directory. 将您的数据文件放在该目录中。
  3. In the properties for your data file, Set "Copy to Output Directory" to "Copy if Newer" 在数据文件的属性中,将“复制到输出目录”设置为“如果更新则复制”
  4. Add this attribute to your test method: [TestMethod, DeploymentItem(@"Test Data\\", @"Test Data\\")] 将此属性添加到您的测试方法:[TestMethod,DeploymentItem(@“ Test Data \\”,@“ Test Data \\”)]
  5. Your test data will be in the same directory as your assembly when you run the unit tests. 运行单元测试时,测试数据将与程序集位于同一目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM