MSTest 和 app.config 问题 [英] MSTest and app.config issue

查看:23
本文介绍了MSTest 和 app.config 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 MSTest 和 app.config 部署自动化单元测试.我阅读了多篇文章和博客,尝试了多种方法,但在 MSTest 执行期间似乎仍然没有接收到 app.config.有一个包含我使用 msbuild 构建的所有单元测试的 dll,这是我尝试过的...

尝试 1

  1. 将 app.config 与 MyTests.dll 复制到与 MyTests.dll.config 相同的位置(在 msdn 论坛之一上,据说它会被自动提取)
  2. 为每个测试添加了 [DeploymentItem("MyTests.dll.config")] 属性
  3. 运行 MSTest.exe/noisolation/testcontainer:d:\MyTestTests.dll/test:MyTest

尝试 2

  1. 使用以下内容创建 local.testrunco​​nfig 文件(如下)
  2. 使用/runco​​nfig 运行 mstest 并且没有隔离,但没有执行任何操作:MSTest.exe/runco​​nfig:d:\local.testrunco​​nfig/testcontainer:d:\MyTestTests.dll/test:MyTest

结果:正在加载 d:\local.testrunco​​nfig...
d:\local.testrunco​​nfig
d:\local.testrunco​​nfig

... 没有任何反应:没有错误,没有执行任何测试!

<小时>

编辑/解决方案:默认情况下,MSTest 在单独的进程中执行测试.在这种情况下,如果配置文件命名为dllname.dll.config",则会自动获取配置文件.但是,如果在 VS 之外运行,则很难调试在单独进程中运行的测试./noisolation 开关用于使 MSTest 在一个进程中运行所有测试.但是,在这种情况下, 选择了测试配置文件.而是使用 MSTest.exe.config 文件,该文件与 MSTest 位于同一目录中.为了解决这个问题,可以像这样实际地加载配置文件:

<预><代码>ExeConfigurationFileMap map = new ExeConfigurationFileMap();map.ExeConfigFilename = @"配置文件路径";配置配置=ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

解决方案

Kateroh,

我的设置看起来像这样(我使用的是来自 TFSbuild.proj 的 msbuild):

  1. 构建 sln

  2. 将所有内容从输出复制到 %TEMP%(黑魔法:D)不知道为什么,但 mstest 正在 %TEMP% 中寻找引用.

  3. 使用参数运行 mstest:

<代码>"C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe"/nologo/testcontainer:$(TestDir)\mylib.dll/resultsfile:$(TestResultFile)/runco​​nfig:$(SlnDir)\AutomaticBuildTest.testrunco​​nfig/searchpathroot:$(TestDir)/publish:mytfsserver/publishbuild:$(BuildDefinition)/flavor:Debug/platform:AnyCPU/teamproject:mytfsproject

下面是 AutomaticBuildTest.testrunco​​nfig

<前><代码><?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"/>--><部署已启用="true"><DeploymentItem filename="D:\sa12\78\bin\Debug"/></部署><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--><执行位置="本地" hostProcessPlatform="MSIL"><!--http://msdn.microsoft.com/en-us/library/ms404663.aspx--><ExecutionThread apartmentState="MTA"/><主机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></执行></测试设置>

I am stuck trying to automate unit tests runs with MSTest and deployment of app.config. I read multiple posts and blogs, tried multiple things and yet still app.config doesn't seem to be picked up during MSTest execution. Having a dll that contains all my unit tests built with msbuild, here is what I've tried...

Attempt 1

  1. Copied app.config to the same location with MyTests.dll as MyTests.dll.config (on one of the msdn forums it was said it'd be picked up automagically)
  2. Added [DeploymentItem("MyTests.dll.config")] attribute to each test
  3. Ran MSTest.exe /noisolation /testcontainer:d:\MyTestTests.dll /test:MyTest

Attempt 2

  1. Created local.testrunconfig file with the following content (below)
  2. Ran mstest with /runconfig and no isolation, but nothing was executed: MSTest.exe /runconfig:d:\local.testrunconfig /testcontainer:d:\MyTestTests.dll /test:MyTest

Result: Loading d:\local.testrunconfig...
d:\local.testrunconfig
d:\local.testrunconfig

... and nothing happens: no errors, no tests are executed!


EDIT/RESOLUTION: By default, MSTest executes tests in separate processes. In this case, config file is automatically picked up if it is named like "dllname.dll.config". However, it is hard to debug tests running in separate processes if they run outside of VS. /noisolation switch is used to make MSTest run all tests in one process. However, in this case test config file is NOT picked up. Instead, MSTest.exe.config file is used, which is located in the same directory as MSTest. To resolve this issue, configuration file can be loaded pragmatically like this:


ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = @"path to config file";
Configuration config = 
   ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

解决方案

Kateroh,

My setup looks like this (I'm using msbuild from a TFSbuild.proj):

  1. Build sln

  2. Copy all from output to %TEMP% (black magic :D) Don't know why but mstest is looking for references in %TEMP%.

  3. Run mstest with parms:

"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

where AutomaticBuildTest.testrunconfig is bellow


<?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>


这篇关于MSTest 和 app.config 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆