MSTest cannot find the assembly

夙愿已清 提交于 2019-11-29 13:19:35

You can install the Prism file in the GAC of your build server.

Tal Segal

All assemblies that are not used directly in test will not be copied to test folder. Therefor, those test methods should be decorated with attribute like:

[DeploymentItem("Microsoft.Practices.Prism.dll")]

This solves the problem without adding the assembly to the GAC.

granadaCoder

Ok. The DeploymentItem is a way to fix this. However, DeploymentItem is a little fragile.

Here is how I fixed it.

The "current directory" has to line up with the DeploymentItem. The best compromise I found would be to set the current directory to where the .sln file is.

Here is my folder structure.

C:\SomeRootFolder\
C:\SomeRootFolder\MySolution.sln
C:\SomeRootFolder\packages\
C:\SomeRootFolder\packages\MyNugetPackage.1.2.3.4\lib\net45\SomeThirdPartyDll.dll
C:\SomeRootFolder\MyTestProject\MyTestProject.csproj
C:\SomeRootFolder\MyTestProject\MyTestClass.cs

MyTestClass.cs

[TestClass]
public class MyTestClass
{
    [TestMethod]
    /* The DeploymentItem item below is for error ::: Warning: Test Run deployment issue: The assembly or module 'SomeDll' directly or indirectly referenced by the test container 'C:\SomeRootFolder\MyTestProject\bin\debug\MyTestProject.dll' was not found. */
    /* There must be a CD (to the .sln folder) command... before the MsTest.exe command is executed */
    [DeploymentItem(@".\packages\MyNugetPackage.1.2.3.4\lib\net45\SomeDll.dll")]
    public void MyTest()
    {
    }
}

The "trick" is to do a CD (change directory) to the folder that houses the .sln.

REM Now the normal restore,build lines
nuget.exe restore "C:\SomeRootFolder\MySolution.sln"
REM the above nuget restore would create "C:\SomeRootFolder\packages\MyNugetPackage.1.2.3.4\lib\net45\SomeThirdPartyDll.dll"
MSBuild.exe "C:\SomeRootFolder\MySolution.sln" /p:Configuration=Debug;FavoriteFood=Popeyes /l:FileLogger,Microsoft.Build.Engine;logfile=MySolution.Debug.Build.log
REM (the below line is the trick to line up the 'current folder' with the relative path of the DeploymentItem)
cd "C:\SomeRootFolder\"
REM now the below will work without the annoying message, note that C:\SomeRootFolder\MyTestProject\bin\Debug\SomeThirdPartyDll.dll exists
MsTest.exe /testcontainer:"C:\SomeRootFolder\MyTestProject\bin\Debug\MyTestProject.dll" /resultsfile:MyTestProject.Dll.Results.trx

Now because the "current directory" (the result of the CD) is at "C:\SomeRootFolder\", the DeploymentItem relative path works correctly.

Jimminy Crickets.......that is a little nutsy.

Note, the Paul Taylor answer here

Running MsTest from the command line with a custom assembly base directory

did not work for me.

easiest way. Just add

 string value = AppDomain.CurrentDomain.BaseDirectory;

to your code (at the start point of your test method) Add a breakpoint to the newly added code and check what is the path of value variable.

continue the test process and after everything get success navigate to the folder of values variable.

You might see all the dlls inside the folder. Just copy them and past ware ever you want and execute the project dll using mstest command line tool.

set mstestPath="C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE"

%mstestpath%\mstest /testcontainer:CodedUITestProject1.dll
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!