TestFixture SetUp and TearDown

I have finally got round to trying NUnit 2.1 with NUnit Add-In. I am relieved to report that it works out of the box with the current version of NUnit Add-In (the one you’re probably using). All you have to do is start using the ‘NUnit.Framework’ assembly from the 2.1 release. If you’re interested to see the code that binds to pretty much any version of NUnit, you can find it here. There’s obviously some reflection in there, but the more interesting bit is the RealProxy. I had to use that to deal with the evolving test listener interface. The reason I started using 2.1 was for the TestFixtureSetUp/TearDown attributes. I didn’t particularly want to be firing up new instance of Visual Studio for every little add-in unit test. With the new attributes I can do the set-up and the start of the test run and tear-down at the end. This goes for whether I’m running an individual test or all tests in an assembly. Here is an example of how to use the new attributes, plus some other stuff for fun.. ;o)
namespace Tests
{
 using System;
 using System.IO;
 using System.Diagnostics;
 using System.Runtime.InteropServices;
 using NUnit.Framework;
 using EnvDTE;

 [TestFixture]
 public class TestVisualStudio
 {
  [TestFixtureSetUp]
  public void TestFixtureSetUp()
  {
   _dte = new DTEClass();
   _dte.MainWindow.Top = 50;
   _dte.MainWindow.Left = 50;
   _dte.MainWindow.Height = 200;
   _dte.MainWindow.Width = 1024;
  }
  private DTE _dte;

  [TestFixtureTearDown]
  public void TestFixtureTearDown()
  {
   _dte.Quit();
   Marshal.ReleaseComObject(_dte);
  }

  [Test]
  public void TestNUnitAddInEscapes()
  {
   Debug.WriteLine(_dte, "_com");
   Debug.WriteLine("=====================");
   Debug.WriteLine(_dte, "_verbose");
  }

  [Test]
  public void TestShowMeTheCode()
  {
   _dte.MainWindow.Visible = true;
   System.Diagnostics.StackFrame frame = new System.Diagnostics.StackFrame(0, true);
   string fileName = frame.GetFileName();
   int lineNumber = frame.GetFileLineNumber() - 1;  // make it zero based
   string[] lines = new StreamReader(fileName).ReadToEnd().Split(new char[] {'\n'});
   string code = lines[lineNumber];
   Debug.WriteLine(code);
   _dte.StatusBar.Text = code;
   System.Threading.Thread.Sleep(5000);
   _dte.MainWindow.Visible = false;
  }
 }
}

3 Comments

Comments have been disabled for this content.