Why MSTest does not copy referenced project librar

2019-02-12 05:32发布

问题:

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?

回答1:

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