nunit单元测试
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.
关于将NUnit与Patrick Cauldwell的外部文件结合使用的好东西。 这就是那些“ Doh!” 我一直很想做的事情,但是最终还是使用了Pre-和Post-Build事件。 我更喜欢帕特里克的方法。 它更加干净,并且可以将曾经是外部文件的文件与测试一起使用。
[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);
}
}
[SetUp] public void SetUp()
{
程序集a = Assembly.GetExecutingAssembly();
使用(Stream s = a.GetManifestResourceStream( “ MyNameSpace.something.txt” ))
{
使用(StreamReader sr = new StreamReader(s))
{
使用(StreamWriter sw = File.CreateText(webConfigPath))
{
sw.Write(sr.ReadToEnd());
sw.Flush();
}
}
}
}
[TearDown] public void TearDown()
{
如果(File.Exists(webConfigPath))
{
File.Delete(webConfigPath);
}
}
nunit单元测试