Scott Hanselman

Using NUnit and a better way to Unit Test with External File Dependancies

June 23, 2004 Comment on this post [2] Posted in NUnit
Sponsored By

Great stuff on using NUnit in conjunction with external files from Patrick Cauldwell.  It's one of those "Doh!" things that I've always MEANT to do, but somehow ended up using Pre- and Post-Build events instead.  I MUCH prefer Patrick's method.  It's much cleaner and it allows the once-external file to LIVE with the test.

        [SetUp] public void SetUp()
        {
            Assembly a = Assembly.GetExecutingAssembly();
            using (Stream s = a.GetManifestResourceStream("MyNameSpace.something.txt"))
            {
                using (StreamReader sr = new StreamReader(s))
                {
                    using (StreamWriter sw = File.CreateText(webConfigPath))
                    {
                        sw.Write(sr.ReadToEnd());
                        sw.Flush();
                    }
                }
            }
        }

        [TearDown] public void TearDown()
        {
            if(File.Exists(webConfigPath))
            {
                File.Delete(webConfigPath);
            }
        }

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service
June 23, 2004 20:53
Great idea. The method for creating the file should be somewhere else so we can simplify the client code. A good place is inside a TestServices class...

[SetUp] public void SetUp()
{
TestServices.CreateTextFile(webConfigPath, "MyNameSpace.something.txt");
}

[TearDown] public void TearDown()
{
TestServices.DeleteFile(webConfigPath);
}

You can also place these methods inside an ancestor of this Test class (assuming your test classes all descend from a common ancestor).
June 24, 2004 5:08
Actually, I do this sort of thing a lot, but have two utils that make it much easier - the source for both is on my coolcode blog (http://coolcode.darrenoakey.info)

TemporaryDirectory: the class gives you a temp dir in which you can work to your hearts content - as soon as the variable gets garbage collected, the temp dir gets cleaned up.

Resources: A much cleaner way of accessing resources..

Here's an example of how to use it - the actual test of the WriteResourceToFile function:

///
/// This tests writing a resource as a file
/// - create a temporary work dir
/// - write the firstSampleResource to a file
/// - read the file back, and check the values
[Test]
public void TestWriteResourcesAsFile()
{
// - create a temporary work dir
TemporaryDirectory newDir = new TemporaryDirectory();

// - write the firstSampleResource to a file
Resources.WriteResourceToFile( newDir, "firstSampleResource.txt");

// - read the file back, and check the values
Assertion.AssertEquals("read the file back, and check the values",
"This is the first sample resource",
CommonFunctions.ReadTextFromFile( newDir.FullPath( "firstSampleResource.txt")));
}



Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.