Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#

Advanced Unit Testing, Part IV - Fixture Setup/Teardown, Test Repetition And Performance Tests

Rate me:
Please Sign up or sign in to vote.
4.70/5 (43 votes)
8 Oct 200321 min read 207.2K   2.3K   187  
This article extends the unit testing framework, adding fixture setup/teardown capability and performance (time and memory) measurement/testing.
using System;
using System.Collections;
using System.Diagnostics;
using System.Threading;

using KALib;

namespace UTCore
{
	public delegate void TestNotificationDelegate(UTCore.TestAttribute ta);
	public delegate void TestIterationDelegate(UTCore.TestAttribute ta);
	public delegate void RunnerCompletedDelegate();

	public class TestRunner
	{
		public enum ProcDir
		{
			RunProcessForward,
			RunProcessReverse,
		}

		private AssemblyCollection assemblyCollection;
		private ArrayList testFixtureList;
		public event TestNotificationDelegate testNotificationEvent;
		public event TestIterationDelegate testIterationEvent;
		public event RunnerCompletedDelegate completedEvent;
		private int numTests;
		private static ProcDir processDirection;

		public AssemblyCollection AssemblyCollection
		{
			get {return assemblyCollection;}
		}

		public ArrayList TestFixtures
		{
			get {return testFixtureList;}
		}

		public int NumTests
		{
			get {return numTests;}
		}

		public static ProcDir ProcessDirection
		{
			get {return processDirection;}
			set {processDirection=value;}
		}

		public TestRunner()
		{
			assemblyCollection=new AssemblyCollection();
			testFixtureList=new ArrayList();
		}

		public void LoadAssembly(string file)
		{
			AssemblyItem ai=new AssemblyItem();
			ai.Load(file);
			ai.LoadNamespaces();
			ai.LoadClasses();
			ai.LoadMethods();
			assemblyCollection.Add(file, ai);
		}

		public void ParseAssemblies()
		{
			numTests=0;

			foreach (AssemblyItem ai in assemblyCollection.Values)
			{
				foreach (NamespaceItem ni in ai.NamespaceCollection.Values)
				{
					foreach (ClassItem ci in ni.ClassCollection.Values)
					{
						TestFixture tf=new TestFixture();
						foreach (object attr in ci.Attributes)
						{
							// verify that attribute class is "UnitTest"
							string attrStr=attr.ToString();
							attrStr=StringHelpers.RightOfRightmostOf(attrStr, '.');
							Trace.WriteLine("Class: "+ci.ToString()+", Attribute: "+attrStr);
							try
							{
								Type t=Type.GetType("UTCore."+attrStr);
								TestUnitAttribute tua=Activator.CreateInstance(t) as TestUnitAttribute;
								tua.Initialize(ci, null, attr);
								tua.SelfRegister(tf);
							}
							catch(Exception e)
							{
								Trace.WriteLine("Exception adding attribute: "+e.Message);
								Trace.WriteLine("Attribute "+attrStr+" is unknown");
							}
						}

						if (tf.HasTestFixture)
						{
							foreach(MethodItem mi in ci.MethodCollection.Values)
							{
								foreach (object attr in mi.Attributes)
								{
									// verify that attribute class is "UnitTest"
									string attrStr=attr.ToString();
									attrStr=StringHelpers.RightOfRightmostOf(attrStr, '.');
									Trace.WriteLine("Method: "+mi.ToString()+", Attribute: "+attrStr);
									try
									{
										Type t=Type.GetType("UTCore."+attrStr);
										TestUnitAttribute tua=Activator.CreateInstance(t) as TestUnitAttribute;
										tua.Initialize(ci, mi, attr);
										tua.SelfRegister(tf);
									}
									catch(Exception e)
									{
										Trace.WriteLine("Attribute "+attrStr+"is unknown: "+e.Message);
									}
								}
							}
							testFixtureList.Add(tf);
							numTests+=tf.NumTests;
						}
					}
				}
			}
		}

		public void RunTests()
		{
			ThreadStart runTests=new ThreadStart(RunTestsThread);
			Thread t=new Thread(runTests);
			t.Start();
		}

		public void RunTestsThread()
		{
			foreach(TestFixture tf in testFixtureList)
			{
				tf.RunTests(testNotificationEvent, testIterationEvent);
			}
			completedEvent();
		}
	}
}

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