Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

c# - MSTest and app.config issue

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 FilesMicrosoft Visual Studio 10.0Common7IDEMSTest.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:sa1278inDebug" />
    </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:sa1278inDebug">
                    <TestDirectory useLoadContext="false" />
                </AssemblyResolution>
            </UnitTestRunConfig>
        </TestTypeSpecific>
        <AgentRule name="LocalMachineDefaultRole">
        </AgentRule>
    </Execution>
</TestSettings>



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...