Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the working directory for MS Test projects

how can I set the working directory for MS Tests projects, like I can do it for normal application projects? My Component that is tested need to access some config files that are also used by other applications. So far I have to copy these files the bin/debug folder of my test project, as these files are expected to be in the working directory.

Is it possible to set a working directory for test projects?

like image 794
Thomas Avatar asked Mar 21 '13 09:03

Thomas


People also ask

Where do Runsettings files go?

runsettings . To autodetect the run settings file, place it at the root of your solution.

What is the working directory in Visual Studio?

Working Directory is the location which Specifies the working directory of the program being debugged. It is the default place in which a program is looking up it's files. Normally the working directory is the directory the application is launched from \bin\debug by default.

How can a test be configured in MSTest v2?

To create a test, you have to write a method with the attribute [TestAttribute] in a class decorated with the [TestClass] attribute. The TestClass attribute may seem superfluous, but we'll see in another post its importance 😃 You can use the methods of the Assert class to validate the behavior of the method under test.


2 Answers

For those who use Visual Studio 2012, if you set the output directory of the test project under Properties|Build|OutputPath then that will become the working directory when you run the tests from Visual Studio (since it is using Vstest.console.exe). If you don't want to have your test dlls, etc output to the same directory as your other files and you don't want to have the other files as content in your test project (as suggested by Schaliasos) then you can set the path on startup in a method that has the [AssemblyInitialize] attribute. You do this by assigning System.Environment.CurrentDirectory as usual. You will probably want to have the working directory as a setting or read from a configuration file.

Edit

As pointed out by Rohit.Net, when you choose Run All Tests the default path would be under TestResults. The same applies when using a runsettings file. The output folder will be the default working directory when you are not using DeploymentItems and: -

  • You right click the test(s) in Visual Studio and choose to run / debug, or
  • You run from the command line using vstest.console.exe.
like image 56
acarlon Avatar answered Sep 19 '22 04:09

acarlon


The working directory for test projects is created at the time you start running your tests.

If you run them locally this is created in a folder TestResults inside your project folder. If you run them in remote machines a different folder is created in each test agent in the following path:

C:\Users{userRunsTheTest}\AppData\Local\VSEQT\QTAgent\{GuidForThisRun}

In order your files to be deployed in these folder and have access to them you have to:

  1. Set the Copy to Output Directory = Copy Always
  2. In the test that needs these files set them as DeploymentItems.
like image 43
chaliasos Avatar answered Sep 22 '22 04:09

chaliasos