Search code examples
c#.nettestingtddnunit

How can I load this file into an NUnit Test?


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


Solution

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