65

I have the following IntegrationTest project structure ...

enter image description here

If i wish to use that test data 126.txt in an NUnit Test, how do I load that plain txt file data?

NOTE: The file is -linked- and I'm using c# (as noted by the image).

cheers :)

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647

4 Answers4

87

You could specify in the properties of the file to be copied to the output folder and inside the unit test:

string text = File.ReadAllText(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestData", "126.txt"));

As an alternative you could embed this file as a resource into the test assembly and then:

var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream("ProjectName.Tests.IntegrationTests.TestData.126.txt"))
using (var reader = new StreamReader(stream))
{
    string text = reader.ReadToEnd();
}
Fredrik Haglund
  • 2,578
  • 2
  • 17
  • 19
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • of course :) wikid :) totoally forgot about that. I also edited the path to include the folder to be technically correct. Cheers mate. Much appreciated as always. – Pure.Krome May 21 '11 at 13:11
  • 2
    For _alternative_ way you need to set file's `Build Action` as `EmbeddedResource`, not just `Resource`. I was confused due to `WPF` treats it in other way: [link](https://stackoverflow.com/questions/9419611/how-to-refer-to-embedded-resources-from-xaml) – Brains Aug 08 '17 at 11:20
  • 10
    The first method does not work when the test is being executed by ReSharper's test runner. It tries to load the file from "AppData\Local\JetBrains\Installations\....". If using nunit the solution is to use "TestContext.CurrentContext.TestDirectory" to the correct directory first. – emirhosseini Feb 15 '18 at 15:22
  • Best solution! Thanks! – Lyubomir Velchev Nov 23 '18 at 12:25
  • 7
    Concatenating paths in .Net should resort to using helper methods such as Path.Combine and not string concatenation to avoid introducing errors into an application and/or system. – Tore Aurstad Jun 04 '19 at 10:25
49

If you do not want the files as ManifestResources, but just as file on the system. See Trouble with NUnit when determining the assembly's directory for more info and this answer in particular

Also interesting is the info from NUnit https://bugs.launchpad.net/nunit-vs-adapter/+bug/1084284/comments/3

But here is the quick info:

Path.Combine(TestContext.CurrentContext.TestDirectory, @"Files\test.pdf")

Where Files\test.PDF is just a file in your test project, with build action content and copy to output directory copy if newer

All credits go out to the people in the other post, but took me a while to find that answer, and that is the reason why i am adding the answer to this post.

Paolo Fulgoni
  • 5,208
  • 3
  • 39
  • 55
Ben Croughs
  • 2,566
  • 1
  • 20
  • 30
22

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.

kdbanman
  • 10,161
  • 10
  • 46
  • 78
6

Found a gotcha with using TestContext.CurrentContext.TestDirectory. This works perfectly in a Setup, but when I call it from within a method supplied to a TestCaseSource, the static method is called before all other code, and returns the following:

C:\Users\<username>\.nuget\packages\nunit\3.10.1\lib\netstandard2.0

However, using TestContext.CurrentContext.WorkDirectory gives the desired result in both places:

C:\SVN\MyApp\trunk\MyApp.Tests\bin\Debug\netcoreapp2.1
monty
  • 1,543
  • 14
  • 30