Reading config settings and AppDomain.CurrentDomain.BaseDirectory fails on unit testing with MS Test

We have a VS 2103 solution with many projects. Some of them load MEF components from a MEF folder under the solution, so it is the same for all projects. The folder is set in app/web.config with a realtive path and all works fine on the development machines.

TC obviously creates the correct structure under the work/<SolutionID> folder, but runs the tests from buildagent/temp folder. This screws up any code trying to read the config file or using AppDomain.CurrentDomain.BaseDirectory because the solution structure is not present on the temp folder.

What is the solution for this ?

6 comments
Comment actions Permalink

Hi Joe,

TeamCity should run tests in the checkout directory by default. Have you specified Working directory in the build step?
Please attach screenshot of the build step settings and build log showing that tests run in the buildAgent/temp folder.

0
Comment actions Permalink

I tried to set the output directory but this does not fix the problem. Instead of Directory.GetCurrentDirectory I tried Assembly.GetExceutingAssembly.CodeBase and both point to the temp folder. The stack trace says it is in the right work directory but the
code sees it in the temp directory:

Test method BFC.IT.Services.Reporting.WindWardService.Tests.UnitTest.RegisterExistingReport threw exception:
System.Exception: Config file does not exist at:C:/TeamCity/buildAgent/temp/buildTmp/SYSTEM_BHPFLDBUILDWV01 2015-04-17 14_32_01/Out/BFC.IT.Services.Reporting.WindWardService/web.config
    at BI.Core.ExtensionMethods.ExceptionIfFalse(Boolean pBoolean, String pErrorMessage)
   at BFC.IT.Services.Reporting.WindWardService.Tests.UnitTestBase.ReplaceReportFolder() in c:\TeamCity\buildAgent\work\6f8c4dcebedd554\BFC.IT.Services.Reporting.WindWardService.Tests\UnitTestBase.cs:line 58
   at BFC.IT.Services.Reporting.WindWardService.Tests.UnitTest.RegisterExistingReport() in c:\TeamCity\buildAgent\work\6f8c4dcebedd554\BFC.IT.Services.Reporting.WindWardService.Tests\UnitTest.cs:line 63

0
Comment actions Permalink

Can you please answer to my last comment and the info I sent you ?

Thanks

0
Comment actions Permalink

Hi Joe,

Sorry for delay. This is a default MSTest (and also VSTest) behavior, as it isolates each test run and any files generated by it. Please use the 'Deployment' feature, provided by MSTest itself. Defining deployment section in '.testsettings' test profile allows certain files to be deployed to the directory where tests are actually executed. Please see the working example:

1) Simple c# dll project with one class and one test method in it (see attachment). Method checks whether there is a file in the same directory as test dll:

var files = Directory.EnumerateFiles(Directory.GetCurrentDirectory(),"*", SearchOption.TopDirectoryOnly).Select(Path.GetFileName);
if (!files.Contains("myRequiredFile.txt"))
{
    Assert.Fail("myRequiredFile.txt was not found");
}

2) 'myRequiredFile.txt' is placed to the directory 'DeploymentSupply' under solution root
3) .testsettings file with the following deployment section:

<Deployment>
    <DeploymentItem filename="DeploymentSupply\myRequiredFile.txt" />
</Deployment>

Deployment path is the path relative to checkout directory. So you don't need to actually know the absolute path. As MSTest is run inside checkout directory, it handles relative paths correctly.
4) Change MSTest settings on TeamCity build step to use created .testsettings file.
deplyment_settings.png
After these steps test succeeded both on TeamCity and locally.

For further reading about deployment see the articles in MSDN:

1) http://msdn.microsoft.com/en-us/library/vstudio/ee256991.aspx#VSTestSettingsDeployment
2) http://msdn.microsoft.com/en-us/library/vstudio/ms182475.aspx



Attachment(s):
DeploymentSample.zip
0
Comment actions Permalink

Alina,

thanks for the reply. I reserached testsettings but it does not solve the problem and the issue IS A TEAMCITY probkem not MS Test. If the tests are run locally, they are reporting the correct location. When they are run in TC they report to run out of the buildagent temp folder not the work folder.

So TC does it differenty than VS.NET. Your solution does not solve the problem when a test alters files of another project. In my case I have tests which alter the config file of a service and look for corrfect behavior. Another test tests service with a FileSystemWatcher where the tests adds files to the watched folder and checks if they are processed correctly.

That can't be done with your approach.

So is there any simple way to set the running directory when starting tests ?

0
Comment actions Permalink

Joe,

MSTest and Visual Studion do not work the same way. In TeamCity the MSTest.exe runner is used. For example see some related links:
https://code.google.com/p/mb-unit/issues/detail?id=357#c6
http://stackoverflow.com/questions/8313134/disabling-mstest-temp-directory-in-teamcity
http://stackoverflow.com/questions/1886716/how-to-get-mstest-to-find-my-test-data-files
http://stackoverflow.com/questions/649106/mstest-copy-file-to-test-run-folder
When you run tests locally, have you tried to run them via command line using MSTest.exe, not in Visual Studio? If you are able to run them via command line, then they should run successfully in TeamCity.

0

Please sign in to leave a comment.