How can I load this file into an NUnit Test?

Pure.Krome picture Pure.Krome · May 21, 2011 · Viewed 32.1k times · Source

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 :)

Answer

Darin Dimitrov picture Darin Dimitrov · May 21, 2011

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(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();
}