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 IV - Fixture Setup/Teardown, Test Repetition And Performance Tests

, 8 Oct 2003
This article extends the unit testing framework, adding fixture setup/teardown capability and performance (time and memory) measurement/testing.
autp4_demo.zip
UTWinForm
App.ico
bin
Debug
Fail.ico
Ignore.ico
ka.ico
Lib.dll
Lib.pdb
NotRun.ico
Pass.ico
UnitTest.dll
Untested.ico
UTCore.dll
UTExample.dll
UTWinForm.exe
obj
Debug
temp
TempPE
UTWinForm.exe
UTWinForm.Form1.resources
UTWinForm.pdb
UTWinForm.projdata
UTWinForm.csproj.user
Lib
bin
Debug
Lib.dll
KALib.csproj.user
obj
Debug
KALib.projdata
Lib.dll
Lib.pdb
Lib.projdata
temp
TempPE
UnitTest
bin
Debug
UnitTest.dll
obj
Debug
temp
TempPE
UnitTest.dll
UnitTest.pdb
UnitTest.projdata
UnitTest.csproj.user
UTCore
bin
Debug
Lib.dll
UnitTest.dll
UTCore.dll
obj
Debug
temp
TempPE
UTCore.dll
UTCore.pdb
UTCore.projdata
UTCore.csproj.user
UTExample
bin
Debug
UnitTest.dll
UTExample.dll
obj
Debug
temp
TempPE
UTExample.dll
UTExample.pdb
UTExample.projdata
Stubs
UTExample.csproj.user
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

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
Web04 | 2.8.140916.1 | Last Updated 9 Oct 2003
Article Copyright 2003 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid