xml文件复制不来测试输出目录 [英] Xml file not copying to test output directory

查看:407
本文介绍了xml文件复制不来测试输出目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio 2010中,64位机,采用内置的Web服务器承载了一组使用内置的测试框架。



<单元测试WCF服务p >我有我的测试需要加载运行一个XML文件。我已经包含在测试项目这个文件,并在文件中设置为内容和总是复制到输出目录。这个文件复制到bin\debug目录罚款。



当我执行的测试,但是,xml文件不存在。而不是寻找项目的bin\debug文件夹,它会寻找它在测试的工作目录C:\Projects\SAP Reapprovals\TestResults\\\
ame_machine 2010-12-06 13_45_43\Out的文件。还没有被复制那里。



有没有办法迫使这个文件进行复制,或是否需要从测试中完全限定的参考?



TIA!

詹姆斯





更新

我设置了 DeploymentItem 属性,但该文件仍然无法复制,但是这肯定看起来像我想要的东西.. ?任何想法,为什么不工作



我的测试代码:

  [TestMethod的] 
[DeploymentItem(SAP - GS3 format.xml)]
公共无效TestProcessSapRoles(){

//我得到一个文件未找到错误在这里,当
//我检查输出目录,它是不是有
的XElement rolesRoot = XElement.Load(SAP - GS3 format.xml);

}



解答:

要感谢CPedros,在他的帮助我放大了这一点。我跑Sysinternals的进程监视器,看到这里有人找我的XML文件。下面是我发现的:



当我跑使用Ctrl + R键,Ctrl + T键(在目前的情况下调试测试)的测试中,视觉工作室 忽略 DeploymentItem 属性完全和甚至没有尝试在任何地方复制的文件。在这种情况下,我得到了一个找不到文件异常,当我试图打开它进行阅读。 Visual Studio创建了一个测试临时工作目录,但在它只有一个文件,AgentRestart.dat。



当我跑使用运行单元测试测试按钮,在我的工具栏(不知道是什么测试选项即是),Visual Studio中未将文件拷贝过来,而是直接从项目目录中引用它。测试通过,并没有临时工作目录已创建。



当我跑从菜单选项测试运行 - >在目前的情况下测试(跑,不调试),一个临时工作目录的创建,以及XML文件和所有可执行文件被复制到它。测试结果通过。



当我编辑Local.testsettings(于室温下我的测试文件夹项目文件夹下),我选择'部署'从左边的菜单,并添加XML文件。它被添加为[解决方案目录] \ [项目目录] \file.xml。我删除了 DeploymentItem 属性。现在,我能够调试试验; 。xml文件和所有可执行文件都复制到测试创建的临时目录



TLDR:的Visual Studio是忽略了 DeploymentItem 属性运行测试的某些方面。解决方法是编辑 Local.testsettings 部署菜单,手动添加文件。



感谢您的帮助!我给CPedros信用他的回答,因为它是在解决这一最有帮助。


解决方案

试着用注释测试该DeploymentItem属性:的http:// MSDN .microsoft.com / EN-US /库/ microsoft.visualstudio.testtools.unittesting.deploymentitemattribute(v = VS.100)的.aspx



下面是一个从文档的代码片段:

  [TestClass中] 
公共类UnitTest1
{
[ TestMethod的()]
[DeploymentItem(testFile1.txt)]
公共无效ConstructorTest()
{
//创建文件部署
Car.CarInfo ();
字符串文件=testFile1.txt; //检查
如果创建的文件在部署目录
Assert.IsTrue存在(File.Exists(文件),部署失败:+文件+
没有得到部署) ;
}
}


Visual Studio 2010, x64 machine, using the built-in web server to host a WCF service with a set of unit tests using the built-in test framework.

I have an XML file that my tests need to load to run. I've included this file in the test project, and have the file set to 'content' and 'always copy to output directory'. This file copies to the bin\debug directory fine.

When I execute the tests, however, the xml file is not there. Instead of looking in the project's bin\debug folder, it looks for it in the test's working directory, C:\Projects\SAP Reapprovals\TestResults\name_machine 2010-12-06 13_45_43\Out". The file hasn't been copied there.

Is there a way to force this file to copy, or do I need to fully qualify the reference from within the test?

TIA!
James


Update
I set the DeploymentItem attribute, but the file still doesn't copy. But that sure looks like what I want... any ideas why that isn't working?

My test code:

[TestMethod]
[DeploymentItem("SAP - GS3 format.xml")]
public void TestProcessSapRoles() {

    //  I get a 'file not found' error here, and when 
    //  I check the output directory, it isn't there
    XElement rolesRoot = XElement.Load("SAP - GS3 format.xml");

}


ANSWERS:
Thanks go out to CPedros, with his help I've zoomed in on this a bit. I ran SysInternals' Process Monitor, to see where it was looking for my xml file. Here's what I found:

When I ran the tests using ctrl+r,ctrl+t (debug tests in current context), visual studio ignored the DeploymentItem attribute completely and did not even try to copy the file anywhere. In this case I got a "File Not Found" exception when I tried to open it for reading. Visual studio created a temporary working directory for the tests, but there was only one file in it, AgentRestart.dat.

When I ran the tests using the 'Run Unit Tests' button in my toolbar (not sure what test option that is), Visual Studio did not copy the file over, but referenced it directly from the project directory. The test passed, and no temporary working directory was created.

When I ran the test from the menu option "run -> tests in current context" (run, not debug), a temporary working directory was created, and the xml file and all executables were copied to it. The test passed.

When I edited Local.testsettings (under a Solution Items folder under my tests folder), I chose 'Deployment' from the left menu, and added the xml file. It was added as [solution directory]\[project directory]\file.xml. I removed the DeploymentItem attribute. Now I was able to debug the tests; the xml file and all executables were copied to the temporary directory created for the test.

TLDR: Visual Studio is ignoring the DeploymentItem attribute for certain ways of running the test. The solution is to edit Local.testsettings, the Deployment menu, and add the file by hand.

Thanks for the help! I'm giving CPedros credit for his answer, as it was the most helpful in resolving this.

解决方案

Try annotating your test with the DeploymentItem attribute: http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.deploymentitemattribute(v=VS.100).aspx

Here's a code snippet from the documentation:

  [TestClass]
    public class UnitTest1
    {
        [TestMethod()]
        [DeploymentItem("testFile1.txt")]
        public void ConstructorTest()
        {
            // Create the file to deploy
            Car.CarInfo();
            string file = "testFile1.txt";
            // Check if the created file exists in the deployment directory
            Assert.IsTrue(File.Exists(file), "deployment failed: " + file +
                " did not get deployed");
        }
    }

这篇关于xml文件复制不来测试输出目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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