Click here to Skip to main content
11,484,162 members (71,234 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
Add your own
alternative version

Advanced Unit Testing, Part II - Core Implementation

, 22 Sep 2003 165.3K 2.3K 194
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.Collections;
using System.Diagnostics;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using KALib;
using UTCore;

namespace UTWinForm
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TreeView tvUnitTests;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem mnuFile;
		private System.Windows.Forms.MenuItem mnuOpen;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem mnuExit;
		private System.Windows.Forms.OpenFileDialog ofdAssembly;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private string assemblyFilename;
		private UTCore.TestRunner testRunner;
		private System.Windows.Forms.Button btnRun;
		private System.Windows.Forms.ProgressBar pbarTestProgress;
		private System.Windows.Forms.Label lblNumTests;
		private Hashtable testToTreeMap;
		private System.Windows.Forms.Label lblPass;
		private System.Windows.Forms.Label lblIgnore;
		private System.Windows.Forms.Label lblFail;
		private System.Windows.Forms.TextBox txtPassCount;
		private System.Windows.Forms.TextBox txtIgnoreCount;
		private System.Windows.Forms.TextBox txtFailCount;
		private int[] testCounts=new int[4];

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			testToTreeMap=new Hashtable();
			ImageList tvImageList=new ImageList();
			tvImageList.Images.Add(Image.FromFile("Untested.ico"));
			tvImageList.Images.Add(Image.FromFile("Pass.ico"));
			tvImageList.Images.Add(Image.FromFile("Ignore.ico"));
			tvImageList.Images.Add(Image.FromFile("Fail.ico"));
			tvUnitTests.ImageList=tvImageList;

			pbarTestProgress.Step=1;
			pbarTestProgress.Minimum=0;

			XmlRegistry reg=new XmlRegistry("config.xml");
			XmlRegistryKey regKey=reg.RootKey;
			XmlRegistryKey lastSelectionKey=regKey.GetSubKey("LastSelections", true);
			assemblyFilename=lastSelectionKey.GetStringValue("FileName", "");
			if (assemblyFilename != "")
			{
				LoadAssembly();
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.tvUnitTests = new System.Windows.Forms.TreeView();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.mnuFile = new System.Windows.Forms.MenuItem();
			this.mnuOpen = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.mnuExit = new System.Windows.Forms.MenuItem();
			this.ofdAssembly = new System.Windows.Forms.OpenFileDialog();
			this.btnRun = new System.Windows.Forms.Button();
			this.pbarTestProgress = new System.Windows.Forms.ProgressBar();
			this.lblNumTests = new System.Windows.Forms.Label();
			this.lblPass = new System.Windows.Forms.Label();
			this.lblIgnore = new System.Windows.Forms.Label();
			this.lblFail = new System.Windows.Forms.Label();
			this.txtPassCount = new System.Windows.Forms.TextBox();
			this.txtIgnoreCount = new System.Windows.Forms.TextBox();
			this.txtFailCount = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// tvUnitTests
			// 
			this.tvUnitTests.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left)));
			this.tvUnitTests.ImageIndex = -1;
			this.tvUnitTests.Location = new System.Drawing.Point(8, 8);
			this.tvUnitTests.Name = "tvUnitTests";
			this.tvUnitTests.SelectedImageIndex = -1;
			this.tvUnitTests.Size = new System.Drawing.Size(272, 392);
			this.tvUnitTests.TabIndex = 0;
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.mnuFile});
			// 
			// mnuFile
			// 
			this.mnuFile.Index = 0;
			this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					this.mnuOpen,
																					this.menuItem2,
																					this.mnuExit});
			this.mnuFile.Text = "&File";
			// 
			// mnuOpen
			// 
			this.mnuOpen.Index = 0;
			this.mnuOpen.Text = "&Open...";
			this.mnuOpen.Click += new System.EventHandler(this.mnuOpen_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 1;
			this.menuItem2.Text = "-";
			// 
			// mnuExit
			// 
			this.mnuExit.Index = 2;
			this.mnuExit.Text = "E&xit";
			this.mnuExit.Click += new System.EventHandler(this.mnuExit_Click);
			// 
			// ofdAssembly
			// 
			this.ofdAssembly.DefaultExt = "dll";
			this.ofdAssembly.Filter = "Assemblies|*.dll";
			// 
			// btnRun
			// 
			this.btnRun.Location = new System.Drawing.Point(288, 48);
			this.btnRun.Name = "btnRun";
			this.btnRun.Size = new System.Drawing.Size(72, 24);
			this.btnRun.TabIndex = 1;
			this.btnRun.Text = "&Run";
			this.btnRun.Click += new System.EventHandler(this.btnRun_Click);
			// 
			// pbarTestProgress
			// 
			this.pbarTestProgress.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.pbarTestProgress.Location = new System.Drawing.Point(288, 80);
			this.pbarTestProgress.Name = "pbarTestProgress";
			this.pbarTestProgress.Size = new System.Drawing.Size(272, 24);
			this.pbarTestProgress.TabIndex = 2;
			// 
			// lblNumTests
			// 
			this.lblNumTests.Location = new System.Drawing.Point(288, 16);
			this.lblNumTests.Name = "lblNumTests";
			this.lblNumTests.Size = new System.Drawing.Size(120, 16);
			this.lblNumTests.TabIndex = 3;
			// 
			// lblPass
			// 
			this.lblPass.Location = new System.Drawing.Point(296, 123);
			this.lblPass.Name = "lblPass";
			this.lblPass.Size = new System.Drawing.Size(40, 16);
			this.lblPass.TabIndex = 4;
			this.lblPass.Text = "Pass:";
			// 
			// lblIgnore
			// 
			this.lblIgnore.Location = new System.Drawing.Point(296, 147);
			this.lblIgnore.Name = "lblIgnore";
			this.lblIgnore.Size = new System.Drawing.Size(40, 16);
			this.lblIgnore.TabIndex = 5;
			this.lblIgnore.Text = "Ignore:";
			// 
			// lblFail
			// 
			this.lblFail.Location = new System.Drawing.Point(296, 171);
			this.lblFail.Name = "lblFail";
			this.lblFail.Size = new System.Drawing.Size(40, 16);
			this.lblFail.TabIndex = 6;
			this.lblFail.Text = "Fail:";
			// 
			// txtPassCount
			// 
			this.txtPassCount.Location = new System.Drawing.Point(344, 120);
			this.txtPassCount.Name = "txtPassCount";
			this.txtPassCount.ReadOnly = true;
			this.txtPassCount.Size = new System.Drawing.Size(72, 20);
			this.txtPassCount.TabIndex = 7;
			this.txtPassCount.Text = "";
			// 
			// txtIgnoreCount
			// 
			this.txtIgnoreCount.Location = new System.Drawing.Point(344, 144);
			this.txtIgnoreCount.Name = "txtIgnoreCount";
			this.txtIgnoreCount.ReadOnly = true;
			this.txtIgnoreCount.Size = new System.Drawing.Size(72, 20);
			this.txtIgnoreCount.TabIndex = 8;
			this.txtIgnoreCount.Text = "";
			// 
			// txtFailCount
			// 
			this.txtFailCount.Location = new System.Drawing.Point(344, 168);
			this.txtFailCount.Name = "txtFailCount";
			this.txtFailCount.ReadOnly = true;
			this.txtFailCount.Size = new System.Drawing.Size(72, 20);
			this.txtFailCount.TabIndex = 9;
			this.txtFailCount.Text = "";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(576, 414);
			this.Controls.Add(this.txtFailCount);
			this.Controls.Add(this.txtIgnoreCount);
			this.Controls.Add(this.txtPassCount);
			this.Controls.Add(this.lblFail);
			this.Controls.Add(this.lblIgnore);
			this.Controls.Add(this.lblPass);
			this.Controls.Add(this.lblNumTests);
			this.Controls.Add(this.pbarTestProgress);
			this.Controls.Add(this.btnRun);
			this.Controls.Add(this.tvUnitTests);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.Text = "MUTE";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Trace.WriteLine("********* PROGRAM BEGIN **********");
			Application.Run(new Form1());
			Trace.WriteLine("********* PROGRAM END ************");
		}

		private void mnuOpen_Click(object sender, System.EventArgs e)
		{
			XmlRegistry reg=new XmlRegistry("config.xml");
			XmlRegistryKey regKey=reg.RootKey;
			XmlRegistryKey lastSelectionKey=regKey.GetSubKey("LastSelections", true);
			ofdAssembly.FileName=lastSelectionKey.GetStringValue("FileName", "");
			if (ofdAssembly.ShowDialog()==DialogResult.OK)
			{
				assemblyFilename=ofdAssembly.FileName;
				lastSelectionKey.SetValue("FileName", assemblyFilename);
				LoadAssembly();
			}
		}

		private void LoadAssembly()
		{
			testRunner=new UTCore.TestRunner();
			testRunner.LoadAssembly(assemblyFilename);
			testRunner.ParseAssemblies();
			testRunner.testNotificationEvent+=new TestNotificationDelegate(TestCompleted);
			PopulateTreeView();
			lblNumTests.Text=testRunner.NumTests.ToString()+" tests.";
		}

		private void mnuExit_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		}

		// *****
		private void PopulateTreeView()
		{
			tvUnitTests.Nodes.Clear();
			TreeNode tnAssembly;
			foreach(AssemblyItem ai in testRunner.AssemblyCollection.Values)
			{
				// create a node for the assembly
				tnAssembly=tvUnitTests.Nodes.Add(ai.FullName);
			
				// collect the namespaces
				UniqueCollection namespaces=new UniqueCollection();
				foreach (TestFixture tf in testRunner.TestFixtures)
				{
					if (tf.Assembly.FullName==tnAssembly.Text)
					{
						namespaces.Add(tf.Namespace, tf);
					}
				}

				foreach (DictionaryEntry item in namespaces)
				{
					// create a node for the namespace
					string ns=item.Key.ToString();
//					TestFixture tf=item.Value as TestFixture;
					TreeNode tnNamespace=tnAssembly.Nodes.Add(ns);
					tnNamespace.ImageIndex=0;
					tnNamespace.SelectedImageIndex=0;

					// collect the classes
					UniqueCollection classes=new UniqueCollection();
					foreach (TestFixture tf in testRunner.TestFixtures)
					{
						if (tf.Namespace==ns)
						{
							foreach (TestAttribute ta in tf.Tests)
							{
								classes.Add(ta.TestClass.ToString(), tf);
							}
						}
					}

					foreach (DictionaryEntry itemClass in classes)
					{
						// create a node for class
						string className=itemClass.Key.ToString();
						TestFixture tf=itemClass.Value as TestFixture;
						TreeNode tnClass=tnNamespace.Nodes.Add(className);
						tnClass.ImageIndex=0;
						tnClass.ImageIndex=0;

						// collect the methods
						UniqueCollection methods=new UniqueCollection();

						foreach(TestAttribute ta in tf.Tests)
						{
							methods.Add(ta.TestMethod.ToString(), ta);
						}

						foreach (DictionaryEntry method in methods)
						{
							string methodName=method.Key.ToString();
							TreeNode tnMethod=tnClass.Nodes.Add(methodName);
							tnMethod.ImageIndex=0;
							tnMethod.SelectedImageIndex=0;
							testToTreeMap.Add(method.Value, tnMethod);
							tnMethod.Tag=method.Value;					// currently not used
						}
					}
				}
			}
			tvUnitTests.ExpandAll();
		}

		private void TestCompleted(TestAttribute ta)
		{
			TreeNode tn=testToTreeMap[ta] as TreeNode;
			if (tn != null)
			{
				int testState=Convert.ToInt32(ta.State);
				tn.ImageIndex=testState;
				tn.SelectedImageIndex=testState;
				++testCounts[testState];
				UpdateTreeParent(tn.Parent, testState);
				UpdateTestCounts();
				++pbarTestProgress.Value;
			}
		}

		private void UpdateTreeParent(TreeNode tn, int testState)
		{
			if (tn != null)
			{
				int currentState=tn.ImageIndex;
				if (testState > currentState)
				{
					tn.ImageIndex=testState;
					tn.SelectedImageIndex=testState;
					UpdateTreeParent(tn.Parent, testState);
				}
			}
		}

		private void UpdateTestCounts()
		{
			txtPassCount.Text=testCounts[1].ToString();
			txtIgnoreCount.Text=testCounts[2].ToString();
			txtFailCount.Text=testCounts[3].ToString();
		}

		private void btnRun_Click(object sender, System.EventArgs e)
		{
			pbarTestProgress.Value=0;
			pbarTestProgress.Maximum=testRunner.NumTests;
			testCounts[0]=0;
			testCounts[1]=0;
			testCounts[2]=0;
			testCounts[3]=0;
			testRunner.RunTests();
		}
	}
}

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 projects, 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 | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 23 Sep 2003
Article Copyright 2003 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid