Tracing Program Execution & NUnit
by Paul Kimmel

Listing One

using System;
using System.Diagnostics;
namespace TestMe
{
  public class Greetings
  {
    public static string GetText()
    {
      Trace.WriteLine("Greetings.GetText called");
      return "Hello, World";
    }
  }
}


Listing Two
using NUnit.Framework;
using TestMe;

namespace Test
{
  [TestFixture()]
  public class MyTests
  {
     [SetUp()]
     public void Init()
     {
       // pre-test preparation here
     }
    [TearDown()]
    public void Deinit()
    {
      // post-test clean up
    }
    [Test()]
    public void GreetingsTest()
    {
      Assertion.AssertEquals("Invalid text returned", "Hello, World", 
      Greetings.GetText());
    }
  }
}


Listing Three

using System;
using System.Diagnostics;
using NUnit.Framework;
using TestMe;
namespace Test
{
  public class Listener : TraceListener
  {
    public override void Write(string message)
    {
      Console.Write(message);
    }
    public override void WriteLine(string message)
    {
      Console.WriteLine(message);
    }
  }
  [TestFixture()]
  public class MyTests
  {
    private static Listener listener = new Listener();
    [SetUp()]
    public void Init()
    {
      if( !Trace.Listeners.Contains(listener))
        Trace.Listeners.Add(listener);
    }
    [TearDown()]
    public void Deinit()
    {
      Trace.Listeners.Remove(listener);
    }
    [Test()]
    public void GreetingsTest()
    {
      Assertion.AssertEquals("Invalid text returned", "Hello, World", 
      Greetings.GetText());
    }
  }
}






2


