单元测试:如何访问文本文件? [英] Unit testing: how to access a text file?

查看:101
本文介绍了单元测试:如何访问文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Visual Studio 2008与Microsoft测试工具一起使用.我需要从单元测试中访问一个文本文件.

I'm using Visual Studio 2008 with Microsoft test tools. I need to access a text file from within the unit test.

我已经配置了将构建操作设置为内容"的文件,并将其复制到输出目录到始终复制",但是文件没有被复制到输出目录,根据System.Environment.CurrentDirectory就是

I've already configured the file with build action set to 'Content' and copy to output directory to 'Copy Always', but the file is not being copied to the output directory, which according to System.Environment.CurrentDirectory is

'{project_path} \ TestResults \ Pablo_COMPU 2009-11-26 15_01_23 \ Out'

'{project_path}\TestResults\Pablo_COMPU 2009-11-26 15_01_23\Out'

此文件夹包含项目的所有DLL依赖项,但我的文本文件不存在.

This folder contains all the DLL dependencies of the project, but my text file is not there.

哪种是从单元测试访问文本文件的正确方法?

Which is the correct way to access a text file from a unit test?

推荐答案

您必须将DeploymentItem属性添加到测试类.使用此属性,您可以指定要复制到out目录中以进行测试运行的文件.

You have to add the DeploymentItem attribute to your test class. With this attribute you specify the files which are copied into the out directory for the test run.

例如:

[TestMethod]
[DeploymentItem(@"myfile.txt", "optionalOutFolder")]
public void MyTest()
{
    ...
}

另请参阅: http://msdn.microsoft.com/zh-CN/library/ms182475.aspx .

这篇关于单元测试:如何访问文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆