15

I have a Visual Studio solution with a C# dll project. This solution has also a test project which references the output of the C# dll project. The referenced project dll has set Copy Local = true.

If I run this test from the Visual Studio, it works fine.

But if I run it from the MSBuild task, for some reason MSTest does not copy the referenced C# dll to the MSTest working folder, so the test fails. The weird thing is, that all the other referenced libaries are copied to the MSTest working folder. And if I put a

 [DeploymentItem(@"PleaseCopyThis.dll")]

before my test method, finally it is copied to the MSTest working folder and my test runs fine.

But why does Visual Studio copy only the referenced dlls which are not part of the solution, but does not copy the referenced project dlls?

2
  • 1
    No, I just left that particular test with DeploymentItem. Maybe there was something wrong with that test project, it was imported into VS 2010 from VS 2008. Commented Sep 26, 2012 at 10:52
  • I have had the exact same problem. We load a lot of dll's dynamically and none of them get copied when running MSTest. Best solution I've seen is just add them as a deployment item. Commented Jan 24, 2014 at 20:25

1 Answer 1

14

So I have found this article: https://web-beta.archive.org/web/20140803214948/http://www.dotnetthoughts.net:80/mstest-exe-does-not-deploy-all-items/

Seems an mstest issue.

Because i just had the same issue I figured a way to fix it. In my case the referenced dll's where never actually used directly from the testproject (altough they were used by using reflection). To solve it I added a testclass with the following code:

[AssemblyInitialize]
    public static void InitializeReferencedAssemblies(TestContext context)
    {
        ObjectInAssemblyX dummy = new ObjectInAssemblyX();
        ObjectInAssemblyY dummy2 = new ObjectInAssemblyY();
    }

Now they are used so they will be copied

4
  • What benefit does that have over the OP's use of DeploymentItemAttribute? Commented Dec 12, 2013 at 5:27
  • @Stafford Williams You have to define deploymentitemattribute on each method. This why you only have to instantiate it once for the whole assembly.
    – Fabian
    Commented Dec 20, 2013 at 10:00
  • 2
    @Fabian no you don't, you can just have it above one class declaration and the item will be copied Commented Dec 22, 2013 at 1:59
  • Updated link to referenced article: web.archive.org/web/20140803214948/http://… Commented Jan 13, 2022 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.