Click here to Skip to main content
15,881,089 members
Articles / Programming Languages / C#

Advanced Unit Testing, Part II - Core Implementation

Rate me:
Please Sign up or sign in to vote.
4.78/5 (34 votes)
22 Sep 200322 min read 209.2K   2.6K   198  
This article illustrates how a unit test automation framework is implemented and continues the case study developed in Part I.
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


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions