How can I load this file into an NUnit Test?

后端 未结 4 1662
温柔的废话
温柔的废话 2020-12-23 19:19

I have the following IntegrationTest project structure ...

\"enter

If i wish t

4条回答
  •  猫巷女王i
    2020-12-23 20:13

    This question is currently answered, but for googlers searching for other possibilities:

    If you get a DirectoryNotFoundException because the test is looking in C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common... rather than in bin\Debug\..., it means your test adapter is executing from a path that isn't your test project output directory.

    To solve this, you can get that bin\Debug\... directory by looking for the directory of the test DLL like so:

    using System.IO;
    using System.Reflection;
    
    // Get directory of test DLL
    var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    
    // dir is now "C:\...\bin\Debug" or wherever the executable is running from
    

    I threw that in a TestHelpers static class in the test project so that I can use it in every test that needs to load external files.

    Code is courtesy of this answer.

提交回复
热议问题