43

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.

James King
  • 6,233
  • 5
  • 42
  • 63
  • 3
    Awesome update, you're an absolute star!! I'd upvote more if I could... – MalcomTucker May 07 '11 at 10:34
  • 1
    Run Unit Tests did the trick for me. Good question. Thanks – Gaʀʀʏ Jun 06 '12 at 19:08
  • 1
    @James B Facing all this with Visual Studio 2012 Express edition, having recently upgraded from .NET 2.0 to .NET 4.5. Just wanted to show that SO really is a useful long-term repository of information. Your update is really helping me get a handle on this. – DavidHyogo Sep 10 '12 at 13:20
  • Glad I could help :) Trying to remember how we solved problems before the internet... not a clue – James King Sep 11 '12 at 06:29

4 Answers4

22

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");
        }
    }
Pero P.
  • 25,813
  • 9
  • 61
  • 85
  • cpedros> I update my question... basically, it looks like what you're saying is what I want, but for some reason it still didn't copy. Any ideas why that wouldn't have worked? – James King Dec 09 '10 at 19:45
  • 2
    @James B. The notation DeploymentItem("file.txt") depends on the file being available at the RelativePathRoot defined in .testrunconfig. This may not be your test project location, in which case you may need DeploymentItem("YourTestProject\\file.txt") – Pero P. Dec 09 '10 at 21:09
  • Check out this for details: http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.deploymentitemattribute(v=VS.100).aspx – Pero P. Dec 09 '10 at 21:20
  • I've read that article a couple of times over, it's helpful, but pretty generic. It does state, however: "Relative paths are relative to the project path." Which to me says, if the file I'm trying to copy is in the main project folder, it should get copied over. I tried "MyTestProject\\file.txt", but that didn't work, either. I don't see a .testrunconfig file anywhere in my solution or project folders... I have two .testsettings and a .vsmdi. The .testsettings has some promising entries I'm going to try next. I wish there was a way to see what folder it's trying to copy this file out of – James King Dec 10 '10 at 03:33
  • I'm used to using nUnit... can you tell? : \ – James King Dec 10 '10 at 03:33
  • Thanks for your help... I updated my question with the answers I found; they're very strange. I'm giving you credit for the answer, as it got me pointed in the right direction. Thanks again! – James King Dec 10 '10 at 04:39
  • 1
    It's important to note that the DeploymentItem attribute has `[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple = true)] `, meaning it can be applied to an entire TestClass so that you don't have to apply this to each TestMethod. – Ian Pugsley Sep 19 '12 at 18:08
0

I've been wrestling with deploymentattribute for a while now, and have finally just given up. Instead I'm just manually copying my files over to a test directory and then deleting them once finished:

Testfile locations:

Private _locationOfTestFilesToBeCopied As String = "C:\...\TestFilesToBeCopied"
Private _locationOfTestFiles As String = "C:\...\TestFiles"

Initialize method:

<TestInitialize()>
Public Sub Setup()
    DeleteCopiedTestFiles()
    CopyTestFilesToTestDirectory()
End Sub

Copy and delete methods:

Private Sub CopyTestFilesToTestDirectory()
    Dim sourceDirectory As New DirectoryInfo(_locationOfTestFilesToBeCopied)

    For Each fileInfo As FileInfo In sourceDirectory.GetFiles
        fileInfo.CopyTo(_locationOfTestFiles & "\" & fileInfo.Name, True)
    Next

    sourceDirectory = Nothing
End Sub

Private Sub DeleteCopiedTestFiles()
    Dim sourceDirectory As New DirectoryInfo(_locationOfTestFiles)

    For Each fileInfo As FileInfo In sourceDirectory.GetFiles
        fileInfo.Delete()
    Next

    sourceDirectory = Nothing
End Sub

I have found this works well

majjam
  • 1,286
  • 2
  • 15
  • 32
0

I was facing the issue like, one of my file was not getting copied in to out folder even if i have entered that file name in local.testSettings and set property as "Copy Always".

Following steps has solved my problem:

  1. Open Test Menu
  2. Select Test Settings
  3. Select option "Select Test Settings file"
  4. Select your .testsettings file from Open Settings File window
  5. Make sure that Selected .testsettings file is checked under Test -> Test Settings

Hope this will help some one.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
CSharp
  • 1,573
  • 2
  • 14
  • 25
0

look into using this attribute over the tests that require the xml file:

http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.deploymentitemattribute.aspx

poindexter12
  • 1,775
  • 1
  • 14
  • 20