Visual Studio-单元测试项目中的加载资源 [英] Visual Studio - Unit tests loading resources in the project

查看:133
本文介绍了Visual Studio-单元测试项目中的加载资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是在这些Xml文件中的某些数据下运行一些测试.

The goal is to run some tests given some data in those Xml files.

如何在单元测试方法中轻松地将给定的Xml文件加载到XmlDoc中?

How would you easily load a given Xml file into an XmlDoc within the unit test methods?

当前状态为:

  XmlDocument doc = new XmlDocument();
  string xmlFile = "4.xml";
  string dir = System.IO.Directory.GetCurrentDirectory() + @"\Msgs\" 

  //dir is then the value of the current exe's path, which is
  //d:\sourcecode\myproject\TestResults\myComputer 2009-10-08 16_07_45\Out

  //we actually need:
  //d:\sourcecode\myproject\Msgs\ 
  doc.Load( dir + fileName); //should really use System.IO.Path.Combine()!

将路径放入app.config是否只是一个简单的问题?考虑到开发人员机器上不同路径的可能性,我希望避免这种情况.

Is it just a simple matter of putting that path in an app.config? I was hoping to avoid that, given the possibility of different paths on developer machines.

问题:您将如何编写在单元测试方法中将给定Xml文件加载到XmlDocument中的算法?

Question: How would you write the algorithm to load a given Xml file into an XmlDocument in the unit test method?

推荐答案

在单元测试项目中,添加一个构建后事件,该事件将XML文件复制到输出目录.然后,您可以使用原始代码来获取XML文件.

In the unit test project add a post-build event that copies the XML file to the output directory. Then, you can use your original code to get the XML file.

构建后事件将如下所示:

The post build event will look like something like this:

copy $(SolutionDir)file.xml $(ProjectDir)$(OutDir)file.xml

您可能还需要将此添加到您的路径:

You may also need this to add to your path:

Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

这篇关于Visual Studio-单元测试项目中的加载资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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