C# DeploymentItem fails to copy file for MSTest unit test
Asked Answered
F

4

10

I'm having trouble getting an XSL file to be copied to the same directory as the test assembly when I use the DeploymentItem attribute on an MSTest unit test. I followed the chosen answer for this question and the file I need copied has its "Copy to Output Directory" set to "Copy Always". When I check my ProjectDir\bin directory (the Target directory), the file I want copied is indeed there, alongside the DLLs and PDBs.

I have a couple unit tests with the following setup:

private const string DLL = "Service.dll";
private const string XSL_PATH = "transform.xsl";

[TestInitialize]
public void InitializeTest()
{
    Assert.IsTrue(File.Exists(DLL)); // passes
}

[TestMethod]
[DeploymentItem(DLL)]
[DeploymentItem(XSL_PATH)]
public void XmlToResultsTest()
{
    Assert.IsTrue(File.Exists(XSL_PATH)); // fails
}

The XSL test fails because when I check MSTest's TestResults\particularTestRun\Out directory, I see the DLLs and the PDBs, but my XSL file is not there. What I want to know is why the XSL file does not get copied alongside the DLLs and PDBs even when I explicitly tell Visual Studio to copy it there via DeploymentItem?

Fuscous answered 25/1, 2010 at 17:16 Comment(0)
F
10

Thanks to Marc Gravell's answer to a related question of mine, I tried updating my MSTest .testrunconfig file so that my XSL file is included in the 'Deployment' section. This lets my unit tests pass, but I'm still perturbed that I had to do this--shouldn't the combination of DeploymentItem and marking the file's properties in my project to copy to the output directory be sufficient?

Fuscous answered 25/1, 2010 at 17:30 Comment(1)
I had this same problem Sarah - very confusing indeed!Fitzwater
R
1

Allegedly VS2008 deployment items silently fail unless the output directory is a literal string. Hmmm ^_^

Reserve answered 2/3, 2010 at 18:20 Comment(2)
Where's your source for that? If that's true, then that's just crazy. I want my string constants, dang it!Fuscous
Not something I've reproduced personally, but I had some deployment issues and googled around, found plenty of blogs saying they had problems with DeploymentItem, e.g: sergeyshishkin.spaces.live.com/blog/… Finally I opted for a global deployment folder in a .testrunconfig file.Reserve
S
1

I had the same problem even though I was using a literal string in the deployment item. I even tried adding the file to the 'Deployment' section of the test settings which didn't work either. It turned out that the problem was related to the testing platform.

I have a 64 bit machine and both the project I was testing and the unit test project's platform target were 'Any CPU'.

I found that the deployment item was only copied if I selected 'Run tests in a 64 bit process on 64 bit machine".

Sheliasheline answered 8/7, 2011 at 18:48 Comment(0)
C
1

We had a similar situation at work where the DeploymentItem attribute was not working as expected. We were also using 64 bit machines, with project platform set at "Any CPU". As a workaround, we included the file required by the unit test as a link from the unit test project and set its "Copy to Output Directory" to "Copy Always".

Countercheck answered 12/6, 2013 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.