24

How do I find the equivalent of a path to the executing assembly when running tests under MS Test in VS 2010? I need to know where the test is running from, so that I can set a relative path to a data file that it needs.

I am trying to find the same sort of path to an executing test that I would get if I used System.Reflection.Assembly.GetEntryAssembly().Location on an executing assembly. Thanks for your help.

David Veeneman
  • 18,912
  • 32
  • 122
  • 187

3 Answers3

25

You can use TestContext.DeploymentDirectory to get the test deployment directory. The test configuration allows you to automatically deploy files for tests.

Gijs
  • 79
  • 6
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
  • BTW, thanks for the 'automatically deploy files' link. I'm learning MS Test, and that was a very helpful link. I'm starting to see things that MS Test can do that other frameworks can't. – David Veeneman Apr 19 '11 at 01:22
  • 3
    This is only true if MSTest deployment is in effect. If no test uses `DeploymentItemAttribute`, or if `False` is included in your `.runsettings` file, MSTest deployment does not take place and your tests will execute directly from the build output folder. Using `AppDomain.CurrentDomain.BaseDirectory` as Nikita suggested below works in both cases, as does `Environment.CurrentDirectory` (which I don't like as much as it's easy to change). – Ohad Schneider Jul 16 '17 at 17:09
  • In MSTest2 there is no DeploymentDirectory property. Consider other answers – Michael Freidgeim Sep 30 '20 at 04:05
11

This question is not specific to MsTest. You can use the same solution you would use in any other .NET application.

An answer from a similar question:

string directory = AppDomain.CurrentDomain.BaseDirectory;
Community
  • 1
  • 1
Nikita R.
  • 7,245
  • 3
  • 51
  • 62
1

Just get the assembly for the current test.

See How to get the assembly (System.Reflection.Assembly) for a given type in .Net?

Community
  • 1
  • 1
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73