Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version
Go to top

Advanced Unit Testing, Part II - Core Implementation

, 22 Sep 2003
This article illustrates how a unit test automation framework is implemented and continues the case study developed in Part I.
autp2_demo.zip
UnitTest.suo
Lib
bin
Debug
Lib.dll
Lib.pdb
KALib.csproj.user
obj
Debug
KALib.projdata
Lib.dll
Lib.pdb
Lib.projdata
temp
TempPE
UnitTest
bin
Debug
UnitTest.dll
UnitTest.pdb
obj
Debug
temp
TempPE
UnitTest.dll
UnitTest.pdb
UnitTest.projdata
UnitTest.csproj.user
UTCore
bin
Debug
Lib.dll
Lib.pdb
UnitTest.dll
UnitTest.pdb
UTCore.dll
UTCore.pdb
obj
Debug
temp
TempPE
UTCore.dll
UTCore.pdb
UTCore.projdata
UTCore.csproj.user
UTExample
bin
Debug
UnitTest.dll
UnitTest.pdb
UTExample.dll
UTExample.pdb
obj
Debug
temp
TempPE
UTExample.dll
UTExample.pdb
UTExample.projdata
Stubs
UTExample.csproj.user
UTWinForm
App.ico
bin
Debug
Fail.ico
Ignore.ico
ka.ico
Lib.dll
Lib.pdb
Pass.ico
UnitTest.dll
UnitTest.pdb
Untested.ico
UTCore.dll
UTCore.pdb
UTExample.dll
UTExample.pdb
UTWinForm.exe
UTWinForm.pdb
obj
Debug
temp
TempPE
UTWinForm.exe
UTWinForm.Form1.resources
UTWinForm.pdb
UTWinForm.projdata
UTWinForm.csproj.user
using System;

using System.Diagnostics;
using System.Drawing;

using NUnit.Framework;

// NUnit requires that test methods be public
// NUnit calls TearDown before calling SetUp!!!

namespace NUnitExample
{
	[TestFixture]
	public class TestExample
	{
		private ClassBeingTested cbt=null;

		[SetUp]
		public void Initialize()
		{
			Trace.WriteLine("SetUp");
			cbt=new ClassBeingTested();
		}

		[TearDown]
		public void Terminate()
		{
			Trace.WriteLine("TearDown");
			if (cbt != null)
			{
				cbt.Dispose();
			}
		}

		[Test]
		public void DoATest()
		{
			Trace.WriteLine("DoATest");
			cbt.LoadImage("fish.jpg");
		}

		[Test, Ignore("Reason")]
		public void IgnoreThisTest()
		{
			Trace.WriteLine("IgnoreThisTest");
		}

		[Test, ExpectedException(typeof(Exception))]
		public void ThrowAnException()
		{
			Trace.WriteLine("ThrowAnException");
			throw new Exception("an exception");
		}
	}

	public class ClassBeingTested : IDisposable
	{
		private bool disposed=false;
		private Image img=null;

		// the constructor
		public ClassBeingTested()
		{
			Trace.WriteLine("ClassBeingTested: Constructor");
		}

		// the destructor
		~ClassBeingTested()
		{
			Trace.WriteLine("ClassBeingTested: Destructor");
			// call Dispose with false.  Since we're in the
			// destructor call, the managed resources will be
			// disposed of anyways.
			Dispose(false);
		}

		public void Dispose()
		{
			Trace.WriteLine("ClassBeingTested: Dispose");
			// dispose of the managed and unmanaged resources
			Dispose(true);

			// tell the GC that the Finalize process no longer needs
			// to be run for this object.
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposeManagedResources)
		{
			// process only if mananged and unmanaged resources have
			// not been disposed of.
			if (!this.disposed)
			{
				Trace.WriteLine("ClassBeingTested: Resources not disposed");
				if (disposeManagedResources)
				{
					Trace.WriteLine("ClassBeingTested: Disposing managed resources");
					// dispose managed resources
					if (img != null)
					{
						img.Dispose();
					}
				}
				// dispose unmanaged resources
				Trace.WriteLine("ClassBeingTested: Disposing unmanaged resouces");
				disposed=true;
			}
			else
			{
				Trace.WriteLine("ClassBeingTested: Resources already disposed");
			}
		}

		// loading an image
		public void LoadImage(string file)
		{
			Trace.WriteLine("ClassBeingTested: LoadImage");
			img=Image.FromFile(file);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Marc Clifton

United States United States
Marc is the creator of two open source projets, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website, www.marcclifton.com, where you will find many of his articles and his blog.
 
Marc lives in Philmont, NY.

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 23 Sep 2003
Article Copyright 2003 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid