DeploymentItem not deploying files
Asked Answered
R

4

20

I am using MS unit testing framework for testing my C# library. I have to open a file which I'm deploying using DeploymentItem attribute. But it is not deploying file to the Test deployment directory.

In my Unit-test project, I created a folder TestFile in this folder there are multiple files, lets say a.txt, b.txt.

Now in my Unit-test class I added DeploymentItem attribute. But file is not being copied.

Here is my code.

[TestClass]
[DeploymentItem("TestFile")]
public class CustomLibraryTest
{
    public TestContext TestContext { get; set; }

    [TestMethod]
    [DeploymentItem(@"TestFiles\a.txt")]  // THis should deploy a.txt to test deployment directory
    {
        var path = TestContext.TestDeploymentDir + "a.txt";
        // Now when I debug this unit-test, and open path in explorer,
        // a.txt is not present in "TestResults\Deploy_fhafeez 2013-05-28 13_02_37\Out" folder
    }
}

What I am doing wrong?

Resurrection answered 28/5, 2013 at 8:10 Comment(0)
P
21

I have found two possible solutions in this thread:

  1. Set the "Copy To Output Folder" property of your deployment files to "Copy Always" (see Problems with DeploymentItem attribute)
  2. Check the "Enable Deployment" setting in the Local.testsettings file (see Problems with DeploymentItem attribute)

Hope this helps.

Precipitate answered 28/5, 2013 at 8:25 Comment(1)
Thanks for help. I set "Copy Always". I have done deployment using test settings, I was thinking to use DeplyomentAttribute this time.Resurrection
H
7

For future references, from what I have noticed using VS 2015 - the path you specify in the deployment item attribute must be relative to the build output (debug folder). If your folder structure is "UnitTesting\TestData\Test.xml" the DeploymentItem must be DeploymentItem("..\..\TestData\Test.xml") In this case, the TestData folder need not be included in the UnitTesting project.

Highwrought answered 9/1, 2016 at 18:14 Comment(2)
this post works for me, mine is VS2015 enterprise, without adding "..\..\", run test, always has file not foundArtillery
I was including my project name ("UnitTesting" in the example above) and that was killing it. As soon as I took that out, it worked. Note that it works fine whether the ..\ or ..\..\ are there or not which makes me think we are already at the root and any number of ..\ aren't going to do anything.Gambrel
C
2

I had a set of tests where the copy was working for some tests but not the latest, even though the DeploymentItem attribute was set exactly the same way. After exhausting everything else, I did a "Clean solution" on the solution and reran and it started copying correctly. YMMV

Collodion answered 15/11, 2018 at 19:28 Comment(1)
This has done the job for me, thanks!Dilemma
A
0

In my case the issue was the "" instead of "/". More explanation here

Acre answered 5/8, 2024 at 18:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.