gmoerkerken gmoerkerken - 1 month ago 25
C# Question

Path to file in VSTS

I'm looking to run a batch file programmatically in C#, as part of a test (being run in MS TEST), which I have working on my local machine. The batch file I want to run is in the repository in a support folder that also contains source code. In VSTS the tests are run like this :


"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" "C:\International\InternationalAppAutomation\International.IntegrationTests.UI\International.IntegrationTests.UI\bin\Debug\International.IntegrationTests.UI.dll" "C:\International\InternationalAppAutomation\International.IntegrationTests.UI\International.IntegrationTests.UI\obj\Debug\International.IntegrationTests.UI.dll" /TestCaseFilter:"TestCategory=Explore" /Settings:"C:\International\InternationalAppAutomation\International.IntegrationTests.UI\International.IntegrationTests.UI\QA.testsettings" /logger:trx /TestAdapterPath:"C:\International\InternationalAppAutomation"


and since I am using relative paths to run the batch file it fails because VSTS runs from a different location than my local machine does. What is the recommended solution to this problem?

Note - I cannot put this batch file in the test output folder because it is very large and we don't want to copy it for each new test run.

Answer

If you want to use it in the test execution, you probably need to set is as a [DeploymentItem] using the attribute on your test class or method, which ensures it's placed in the test execution directory.

If that's not an option, use the environment variables to find your source control root. The build agent will automatically set the variable %BUILD_SOURCESDIRECTORY%. You or your batch file should be able to use that to find the location. Alternatively use my VSTS variables task to set a specific variable with the path to the file. each variable will be available as an environment variable in subsequent tasks. You can use the existing variables to resolve the path to your file.

If you're still on XAML builds, a similar variable is available %TF_BUILD_SOURCESDIRECTORY%.

See:

Depending on how you resolve files, you may then need to tell your batch file the current test output directory or the working directory. These are easily retrieved from the C# side of things and can then be stored in the environment or passed as a parameter when executing the batch file.

By adding the proper TestContext property to your test class, you can retrieve these directories from:

TestContext.DeploymentDirectory 
TestContext.TestResultsDirectory 

See:

Comments