While writing the test for the NUnit fixture loader, I started to have some twisted tests. So twisted, they are worth mentionned in the blog. For instance, the steps I take to test NUnit load are:
Twisted...
using System; using System.IO; using System.Configuration; using System.Reflection; using MbUnit.Core.Remoting; using MbUnit.Core.Framework; using MbUnit.Framework; using MbUnit.Framework.Utils; using System.CodeDom.Compiler; using MbUnit.Core.Reports; using MbUnit.Core.Reports.Serialization; namespace MbUnit.Tests.Core.FrameworkBridges { [TestFixture] [CurrentFixture] public class NUnitBridgeTest { private SnippetCompiler compiler; private ReportCounter counter; private ReportResult result; [SetUp] public void SetUp() { this.compiler = new SnippetCompiler(); string nunitFolder = ConfigurationSettings.AppSettings["NUnitFolder"]; string nunitFrameworkDll = Path.Combine(nunitFolder,@"NUnit.Framework.dll"); this.compiler.Parameters.ReferencedAssemblies.Add(nunitFrameworkDll); this.compiler.LoadFromResource( "MbUnit.Tests.Core.FrameworkBridges.NUnitFixture.cs", Assembly.GetExecutingAssembly() ); string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); this.compiler.Parameters.OutputAssembly = Path.Combine(path, "NUnitBridgeTest.dll"); } [Test] public void TestCaseCount() { LoadAndRunFixture(); Assert.AreEqual(4, counter.RunCount); } [Test] public void SuccessCount() { LoadAndRunFixture(); Assert.AreEqual(2, counter.SuccessCount); } ... private void LoadAndRunFixture() { this.compiler.Parameters.GenerateInMemory = false; this.compiler.Compile(); this.compiler.ShowErrors(Console.Out); Assert.IsFalse(this.compiler.Results.Errors.HasErrors); // load assembly using MbUnit using (TestDomain domain = new TestDomain(this.compiler.Parameters.OutputAssembly)) { domain.ShadowCopyFiles = false; domain.Load(); // running tests domain.TestTree.RunPipes(); result = domain.TestTree.Report.Result; counter = domain.TestTree.GetTestCount(); } } } }
And the loaded fixture is as follows:
using System; using System.IO; using NUnit.Framework; namespace MbUnit.Tests.Core.FrameworkBridges { [TestFixture] public class NUnitFixture { [TestFixtureSetUp] public void TestFixtureSetUp() { Console.Out.Write("TestFixtureSetUp"); } [SetUp] public void SetUp() { Console.Out.Write("SetUp"); } [Test] public void Success() { Console.Out.Write("Success"); } [Test] public void Failure() { Console.Out.Write("Failure"); Assert.Fail(); } [Test] [ExpectedException(typeof(ArgumentNullException))] public void ExpectedException() { Console.Out.Write("ExpectedException"); throw new ArgumentNullException("boom"); } [Test] [Ignore("Because I want")] public void Ignore() { Console.Out.Write("Ignore"); throw new Exception("Ignored test"); } [TearDown] public void TearDown() { Console.Out.Write("TearDown"); } [TestFixtureTearDown] public void FixtureTearDown() { Console.Out.Write("FixtureTearDown"); } } }
Page rendered at Sunday, September 07, 2008 9:28:26 PM UTC
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.