DeploymentItem fails copying directories when there are multiple test projects per solution

3.1k views Asked by At

I have a number of test classes and methods that copy a particular directory like so:

[TestClass, DeploymentItem("LanguageData", "LanguageData")]
public class OcrTests
{
    [TestMethod]
    public void Can_Capture_Field()
    {
        // some code that expects the LanguageData directory to be in the test results Out directory
    }

    // etc
}

[TestClass]
public class OcrBuilderTests
{
    [TestMethod, DeploymentItem("LanguageData", "LanguageData")]
    public void Can_Build_Specific_Ocr_Engine_Implementation()
    {
        // some more code that expects the LanguageData directory to be in the test results Out directory
    }

    // etc
}

Those tests are in a single assembly and all the files in the LangaugeData directory have their Copy to Output Directory set to Copy Always.

It all works fine and the directory is copied to the test results Out directory as long as I only have that one test assembly loaded into the solution or that's the only assembly I run tests from (i.e. run tests only in current context/class).

As soon as I add a second assembly and run all the tests in the solution then that directory no longer gets copied, but any other DeploymentItems that are just individual files seem to get copied fine.

The tests themselves all still run, but the ones that depend on that directory crash. Presumably that's because MSTest can't find the directory - perhaps it's expecting it to be in the build directory of one of the other test assemblies?

Any ideas what it is about multiple test projects that's preventing the copy, and what I can do to get around it, short of adding every single file in that directory as an individual DeploymentItem?

2

There are 2 answers

0
PlaceboZA On BEST ANSWER

This question is quite old, but could still benefit others. Especially since I ended up here :)

It seems that DeploymentItemAttribute doesn't support using the same source path name in multiple test classes. Note: I said same path name, not physical folder (think different test projects with same folder name to deploy).

However the destination folder name can be different, with no ill effects.

My suggestion is:

  1. Create a fixture base class (if you prefer, in separate project)
  2. Add the attribute: [TestClass, DeploymentItem("LanguageData", "LanguageData")]
  3. Change your OcrTests and OcrBuilderTests classes to inherit the new class.
  4. Remember to remove the deploymentitem attributes for 'LanguageData' from OcrTests and OcrBuilderTests

I've tried this, with Great Success. In my case, I had a common test fixture project and multiple test projects, each using the base class.

Unfortunately the DeploymentItemAttribute is filled with Gotchas, see here for more.

1
Gatis Bergšpics On

Tried your approach but still it did not copy folder properly, so what i did instead copied files not directories(maybe this helps someone):

[TestClass]
[DeploymentItem("connectionStrings.config")]

// should be able to do this, but it does not work always, only sometimes
//[DeploymentItem("Configs", "Configs")]

// this instead should work always
[DeploymentItem("Configs\\file1.txt", "Configs")]
[DeploymentItem("Configs\\file2.txt", "Configs")]
[DeploymentItem("Configs\\file3.txt", "Configs")]
.....
[DeploymentItem("Configs\\filen.txt", "Configs")]
public class BaseTests
{
}