kat*_*roh 34 c# msbuild mstest app-config
我试图通过MSTest和app.config的部署来自动运行单元测试.我阅读了多个帖子和博客,尝试了多项内容,但在MSTest执行期间,似乎仍然没有选择app.config.有一个包含我用msbuild构建的所有单元测试的dll,这是我试过的...
[DeploymentItem("MyTests.dll.config")]
为每个测试添加了属性MSTest.exe /noisolation /testcontainer:d:\MyTestTests.dll /test:MyTest
MSTest.exe /runconfig:d:\local.testrunconfig /testcontainer:d:\MyTestTests.dll /test:MyTest
结果:
正在加载d:\ local.testrunconfig ...
d:\ local.testrunconfig
d:\ local.testrunconfig
......没有任何反应:没有错误,没有执行任何测试!
编辑/解决方案:默认情况下,MSTest在单独的进程中执行测试.在这种情况下,如果配置文件的名称类似于"dllname.dll.config",则会自动获取配置文件.但是,如果在VS之外运行,则很难调试在单独进程中运行的测试./ noisolation开关用于使MSTest在一个进程中运行所有测试.但是,在这种情况下,不会选择测试配置文件.而是使用MSTest.exe.config文件,该文件与MSTest位于同一目录中.要解决此问题,可以像下面这样实际加载配置文件:
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = @"path to config file";
Configuration config =
ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
Run Code Online (Sandbox Code Playgroud)
Kateroh,
我的设置看起来像这样(我正在使用来自TFSbuild.proj的msbuild):
建立sln
将所有输出复制到%TEMP%(黑魔法:D)不知道为什么但mstest正在%TEMP%中查找引用.
使用parms运行mstest:
"C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe" /nologo /testcontainer:$(TestDir)\mylib.dll /resultsfile:$(TestResultFile) /runconfig:$(SlnDir)\AutomaticBuildTest.testrunconfig /searchpathroot:$(TestDir) /publish:mytfsserver /publishbuild:$(BuildDefinition) /flavor:Debug /platform:AnyCPU /teamproject:mytfsproject
其中AutomaticBuildTest.testrunconfig如下所示
<?xml version="1.0" encoding="UTF-8"?>
<TestSettings name="AutomaticBuildTest" id="eda99352-93e1-402e-9517-d04fffa66b35" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<!--<Deployment enabled="false" />-->
<Deployment enabled="true">
<DeploymentItem filename="D:\sa12\78\bin\Debug" />
</Deployment>
<NamingScheme baseName="BC2ALibraryTest" appendTimeStamp="false" useDefault="false" />
<!-- http://blogs.msdn.com/b/vstsqualitytools/archive/2009/12/01/executing-unit-tests-in-parallel-on-a-multi-cpu-core-machine.aspx -->
<Execution location="Local" hostProcessPlatform="MSIL">
<!--http://msdn.microsoft.com/en-us/library/ms404663.aspx-->
<ExecutionThread apartmentState="MTA" />
<Hosts skipUnhostableTests="false" />
<TestTypeSpecific>
<UnitTestRunConfig testTypeId="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b">
<AssemblyResolution applicationBaseDirectory="D:\sa12\78\bin\Debug">
<TestDirectory useLoadContext="false" />
</AssemblyResolution>
</UnitTestRunConfig>
</TestTypeSpecific>
<AgentRule name="LocalMachineDefaultRole">
</AgentRule>
</Execution>
</TestSettings>
您的应用程序不使用app.config.它使用应用程序 .exe.config.这就是你需要部署的东西.
对不起,没看到你在测试DLL.
这不是.NET配置的工作方式.每个DLL都不使用自己的配置文件.它只能使用它运行的.exe的配置文件(实际上,它正在运行的AppDomain).
你会以某种方式需要让MSTEST将你的.dll.config添加到它自己的配置中,假设它实际上是主机进程.我不知道如何从命令行执行此操作.
我通常在Visual Studio中使用单元测试项目,所以我只是从该项目部署配置文件.工作良好.