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

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.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using KALib;
using UTCore;

/*
	Things to do in Part III:
	
	Extend the unit tests:
		Add fixture-wide setup/teardown
		Test sequencing
		Repeat test
		Performance test
		Inspection of private members
		Memory utilization test
		Automatic Dipose call
		Uniqueness test
		non-uniqueness test
		Boundary testing (array indices)
		Not-found items (hashtable indices)
		
	Extend the GUI:
		tabs to report test results
		add "not-tested" to support sequencing
	
	The first interesting thing to do add is the idea of test sequencing and
	testing sequence permutations.  For this, I'm going to take the purchase order
	close test and write a sequence of tests for it:
	
	Then, we'll see what errors MUTE comes up with when we run the sequence in different
	permutations.  Some of the things needed will be:
	
	AllowableExceptionAttribute: test passes if it throws the specified exception
	SequenceNumberAttribute: a value that determines the default ordering of tests in the sequence
	PermuteSequenceNumberAttribute: a value that determines the default ordering of tests and is permutable
	RunPermutationsAttribute: an attribute on the test fixture class, telling MUTE to run permutations on methods with a SequenceNumber attribute
		
	The basic sequence will be:
	
	1.	assign a vendor
	2.	add parts (which are the same as the parts added to the vendor's list) to the PO associated with a pre-defined workorder
	3.	create an invoice and assign the vendor to it
	4.	add charges to the invoice
	5.	add the invoice to the PO
	6.	close the PO
	
	Some of the things we want to look for are:
	
	1.	Adding parts to the PO before the vendor is assigned
	2.	Adding the invoice to the PO before adding charges
	3.	Closing the PO before the invoice is attached
	
	This will require a test fixture wide setup:
	1.	create an empty purchase order
	2.	create a vendor
	3.	create some parts
	4.	add parts to the vendor
	5.	create a couple work orders
	
	Which will require some additional attributes:
	
	FixtureSetUp
	FixtureTearDown
	
	It would be good to rename the "SetUp" and "TearDown" attributes to "TestSetUp" and "TestTearDown" so it's clearer as to what these do.
	
	Alternatively, we can simply create some unit tests for these features that are part of the sequence but
	exclude them from the permutation testing.
	
	If a test generates an exception in a sequence, and it is an allowable exception, then I think the test should pass for that
	sequence.
	
	When we do this, we have to verify that there are no duplicate sequence numbers.
	
	Part IV:
	
	1.	Add FixtureSetUp and FixtureTearDown attributes
		Demonstrate this by creating 100,000 parts in the Vendor class.  Use this demo for #2
		Discuss why this is separate from simply putting the functionality in the class constructor.
			a.	symmetry
			b.	so you can have a tear down process as well
	
	2.	Measure processing time, operations per second
		Demonstrate this in two ways:
			a.	profiling all tests
			b.	profiling a sequence (poor man's profiler)
			c.	failing if the process time exceeds an "operations per second" minimum (this is a new attribute)
				Use the new "find the part by walking through the collection" function to demonstrate testing for poor performing functions
			
	3.	Measure memory utilization
		Demonstrate:
			a.	Before and after memory utilization
			b.	failing if the process has "memory leaks", in other words, doesn't dispose of managed and/or unmanaged resources
		
	4.	Additional attributes:
		a.	Run a test x # of repetitions.  Benefits:
			1)	long term testing of things like network performance
			2)	allows tester to validate physical problems, like a link breaking
			3)  integrated with performance testing to get min, max, and average times
			
	What about mock objects?
	What about scripting?
			
*/

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 ArrayList allTreeNodes;
		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 System.Windows.Forms.TabControl tcStatus;
		private System.Windows.Forms.TabPage tpPass;
		private System.Windows.Forms.TabPage tpNotRun;
		private System.Windows.Forms.TabPage tpIgnore;
		private System.Windows.Forms.TabPage tpFail;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ColumnHeader chFixture;
		private System.Windows.Forms.ColumnHeader chTest;
		private System.Windows.Forms.ColumnHeader chResults;
		private System.Windows.Forms.TabPage tpRunner;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.ListView lvIgnoredResults;
		private System.Windows.Forms.ColumnHeader columnHeader4;
		private System.Windows.Forms.ColumnHeader columnHeader5;
		private System.Windows.Forms.ColumnHeader columnHeader6;
		private System.Windows.Forms.ListView lvFailedResults;
		private System.Windows.Forms.ColumnHeader columnHeader7;
		private System.Windows.Forms.ColumnHeader columnHeader8;
		private System.Windows.Forms.ColumnHeader columnHeader9;
		private System.Windows.Forms.TextBox txtNotRun;

		private int[] testCounts=new int[5];
		private System.Windows.Forms.ListView lvPassResults;
		private System.Windows.Forms.ListView lvNotRunResults;
		private System.Windows.Forms.RadioButton rbFwdSeq;
		private System.Windows.Forms.GroupBox gbSeq;
		private System.Windows.Forms.RadioButton rbRevSeq;
		private System.Windows.Forms.ColumnHeader columnHeader11;
		private System.Windows.Forms.ColumnHeader columnHeader10;
		private System.Windows.Forms.ColumnHeader columnHeader12;
		private System.Windows.Forms.ColumnHeader columnHeader13;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox tbRepNumber;
		private System.Windows.Forms.ProgressBar pbarIteration;
		private System.Windows.Forms.ColumnHeader columnHeader14;
		private System.Windows.Forms.ColumnHeader columnHeader15;
		private ArrayList[] testResults=new ArrayList[4];

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

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			for (int i=0; i<4; i++)
			{
				testResults[i]=new ArrayList();
			}

			allTreeNodes=new ArrayList();
			testToTreeMap=new Hashtable();
			ImageList tvImageList=new ImageList();
			tvImageList.Images.Add(Image.FromFile("Untested.ico"));
			tvImageList.Images.Add(Image.FromFile("NotRun.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;

			pbarIteration.Step=1;
			pbarIteration.Minimum=0;

			XmlRegistry reg=new XmlRegistry("config.xml");
			XmlRegistryKey regKey=reg.RootKey;
			XmlRegistryKey lastSelectionKey=regKey.GetSubKey("LastSelections", true);
			assemblyFilename=lastSelectionKey.GetStringValue("FileName", "");
			rbFwdSeq.Checked=lastSelectionKey.GetBooleanValue("rbFwd", true);
			rbRevSeq.Checked=lastSelectionKey.GetBooleanValue("rbRev", false);
			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.tcStatus = new System.Windows.Forms.TabControl();
			this.tpRunner = new System.Windows.Forms.TabPage();
			this.pbarIteration = new System.Windows.Forms.ProgressBar();
			this.tbRepNumber = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.txtNotRun = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.gbSeq = new System.Windows.Forms.GroupBox();
			this.rbFwdSeq = new System.Windows.Forms.RadioButton();
			this.rbRevSeq = new System.Windows.Forms.RadioButton();
			this.tpNotRun = new System.Windows.Forms.TabPage();
			this.lvNotRunResults = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.tpPass = new System.Windows.Forms.TabPage();
			this.lvPassResults = new System.Windows.Forms.ListView();
			this.chFixture = new System.Windows.Forms.ColumnHeader();
			this.chTest = new System.Windows.Forms.ColumnHeader();
			this.chResults = new System.Windows.Forms.ColumnHeader();
			this.columnHeader11 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader12 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader14 = new System.Windows.Forms.ColumnHeader();
			this.tpIgnore = new System.Windows.Forms.TabPage();
			this.lvIgnoredResults = new System.Windows.Forms.ListView();
			this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
			this.tpFail = new System.Windows.Forms.TabPage();
			this.lvFailedResults = new System.Windows.Forms.ListView();
			this.columnHeader7 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader8 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader9 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader10 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader13 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader15 = new System.Windows.Forms.ColumnHeader();
			this.tcStatus.SuspendLayout();
			this.tpRunner.SuspendLayout();
			this.gbSeq.SuspendLayout();
			this.tpNotRun.SuspendLayout();
			this.tpPass.SuspendLayout();
			this.tpIgnore.SuspendLayout();
			this.tpFail.SuspendLayout();
			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, 360);
			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(472, 72);
			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, 144);
			this.pbarTestProgress.Name = "pbarTestProgress";
			this.pbarTestProgress.Size = new System.Drawing.Size(344, 24);
			this.pbarTestProgress.TabIndex = 2;
			// 
			// lblNumTests
			// 
			this.lblNumTests.Location = new System.Drawing.Point(296, 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, 244);
			this.lblPass.Name = "lblPass";
			this.lblPass.Size = new System.Drawing.Size(56, 16);
			this.lblPass.TabIndex = 4;
			this.lblPass.Text = "Passed:";
			// 
			// lblIgnore
			// 
			this.lblIgnore.Location = new System.Drawing.Point(296, 268);
			this.lblIgnore.Name = "lblIgnore";
			this.lblIgnore.Size = new System.Drawing.Size(56, 16);
			this.lblIgnore.TabIndex = 5;
			this.lblIgnore.Text = "Ignored:";
			// 
			// lblFail
			// 
			this.lblFail.Location = new System.Drawing.Point(296, 292);
			this.lblFail.Name = "lblFail";
			this.lblFail.Size = new System.Drawing.Size(56, 16);
			this.lblFail.TabIndex = 6;
			this.lblFail.Text = "Failed:";
			// 
			// txtPassCount
			// 
			this.txtPassCount.Location = new System.Drawing.Point(352, 240);
			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(352, 264);
			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(352, 288);
			this.txtFailCount.Name = "txtFailCount";
			this.txtFailCount.ReadOnly = true;
			this.txtFailCount.Size = new System.Drawing.Size(72, 20);
			this.txtFailCount.TabIndex = 9;
			this.txtFailCount.Text = "";
			// 
			// tcStatus
			// 
			this.tcStatus.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.tcStatus.Controls.Add(this.tpRunner);
			this.tcStatus.Controls.Add(this.tpNotRun);
			this.tcStatus.Controls.Add(this.tpPass);
			this.tcStatus.Controls.Add(this.tpIgnore);
			this.tcStatus.Controls.Add(this.tpFail);
			this.tcStatus.Location = new System.Drawing.Point(8, 8);
			this.tcStatus.Name = "tcStatus";
			this.tcStatus.SelectedIndex = 0;
			this.tcStatus.Size = new System.Drawing.Size(648, 400);
			this.tcStatus.TabIndex = 10;
			// 
			// tpRunner
			// 
			this.tpRunner.Controls.Add(this.pbarIteration);
			this.tpRunner.Controls.Add(this.tbRepNumber);
			this.tpRunner.Controls.Add(this.label2);
			this.tpRunner.Controls.Add(this.tvUnitTests);
			this.tpRunner.Controls.Add(this.txtFailCount);
			this.tpRunner.Controls.Add(this.txtNotRun);
			this.tpRunner.Controls.Add(this.label1);
			this.tpRunner.Controls.Add(this.lblPass);
			this.tpRunner.Controls.Add(this.lblIgnore);
			this.tpRunner.Controls.Add(this.lblFail);
			this.tpRunner.Controls.Add(this.txtPassCount);
			this.tpRunner.Controls.Add(this.txtIgnoreCount);
			this.tpRunner.Controls.Add(this.btnRun);
			this.tpRunner.Controls.Add(this.pbarTestProgress);
			this.tpRunner.Controls.Add(this.lblNumTests);
			this.tpRunner.Controls.Add(this.gbSeq);
			this.tpRunner.Location = new System.Drawing.Point(4, 22);
			this.tpRunner.Name = "tpRunner";
			this.tpRunner.Size = new System.Drawing.Size(640, 374);
			this.tpRunner.TabIndex = 4;
			this.tpRunner.Text = "Runner";
			// 
			// pbarIteration
			// 
			this.pbarIteration.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.pbarIteration.Location = new System.Drawing.Point(288, 176);
			this.pbarIteration.Name = "pbarIteration";
			this.pbarIteration.Size = new System.Drawing.Size(344, 24);
			this.pbarIteration.TabIndex = 17;
			// 
			// tbRepNumber
			// 
			this.tbRepNumber.Location = new System.Drawing.Point(472, 232);
			this.tbRepNumber.Name = "tbRepNumber";
			this.tbRepNumber.ReadOnly = true;
			this.tbRepNumber.Size = new System.Drawing.Size(72, 20);
			this.tbRepNumber.TabIndex = 16;
			this.tbRepNumber.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(472, 220);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(72, 16);
			this.label2.TabIndex = 15;
			this.label2.Text = "Repetition #:";
			// 
			// txtNotRun
			// 
			this.txtNotRun.Location = new System.Drawing.Point(352, 216);
			this.txtNotRun.Name = "txtNotRun";
			this.txtNotRun.ReadOnly = true;
			this.txtNotRun.Size = new System.Drawing.Size(72, 20);
			this.txtNotRun.TabIndex = 12;
			this.txtNotRun.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(296, 220);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(64, 16);
			this.label1.TabIndex = 11;
			this.label1.Text = "Not Run:";
			// 
			// gbSeq
			// 
			this.gbSeq.Controls.Add(this.rbFwdSeq);
			this.gbSeq.Controls.Add(this.rbRevSeq);
			this.gbSeq.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.gbSeq.Location = new System.Drawing.Point(288, 40);
			this.gbSeq.Name = "gbSeq";
			this.gbSeq.Size = new System.Drawing.Size(112, 88);
			this.gbSeq.TabIndex = 14;
			this.gbSeq.TabStop = false;
			this.gbSeq.Text = "Run Processes";
			// 
			// rbFwdSeq
			// 
			this.rbFwdSeq.Location = new System.Drawing.Point(16, 24);
			this.rbFwdSeq.Name = "rbFwdSeq";
			this.rbFwdSeq.Size = new System.Drawing.Size(72, 24);
			this.rbFwdSeq.TabIndex = 13;
			this.rbFwdSeq.Text = "Forward";
			// 
			// rbRevSeq
			// 
			this.rbRevSeq.Location = new System.Drawing.Point(16, 48);
			this.rbRevSeq.Name = "rbRevSeq";
			this.rbRevSeq.Size = new System.Drawing.Size(72, 24);
			this.rbRevSeq.TabIndex = 15;
			this.rbRevSeq.Text = "Reverse";
			// 
			// tpNotRun
			// 
			this.tpNotRun.Controls.Add(this.lvNotRunResults);
			this.tpNotRun.Location = new System.Drawing.Point(4, 22);
			this.tpNotRun.Name = "tpNotRun";
			this.tpNotRun.Size = new System.Drawing.Size(640, 374);
			this.tpNotRun.TabIndex = 1;
			this.tpNotRun.Text = "NotRun";
			// 
			// lvNotRunResults
			// 
			this.lvNotRunResults.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.lvNotRunResults.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																							  this.columnHeader1,
																							  this.columnHeader2,
																							  this.columnHeader3});
			this.lvNotRunResults.FullRowSelect = true;
			this.lvNotRunResults.GridLines = true;
			this.lvNotRunResults.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this.lvNotRunResults.Location = new System.Drawing.Point(0, -1);
			this.lvNotRunResults.Name = "lvNotRunResults";
			this.lvNotRunResults.Size = new System.Drawing.Size(640, 376);
			this.lvNotRunResults.TabIndex = 1;
			this.lvNotRunResults.View = System.Windows.Forms.View.Details;
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "Fixture";
			this.columnHeader1.Width = 96;
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "Test";
			this.columnHeader2.Width = 125;
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "Results";
			this.columnHeader3.Width = 389;
			// 
			// tpPass
			// 
			this.tpPass.Controls.Add(this.lvPassResults);
			this.tpPass.Location = new System.Drawing.Point(4, 22);
			this.tpPass.Name = "tpPass";
			this.tpPass.Size = new System.Drawing.Size(640, 374);
			this.tpPass.TabIndex = 0;
			this.tpPass.Text = "Pass";
			// 
			// lvPassResults
			// 
			this.lvPassResults.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.lvPassResults.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																							this.chFixture,
																							this.chTest,
																							this.chResults,
																							this.columnHeader11,
																							this.columnHeader12,
																							this.columnHeader14});
			this.lvPassResults.FullRowSelect = true;
			this.lvPassResults.GridLines = true;
			this.lvPassResults.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this.lvPassResults.Location = new System.Drawing.Point(0, 0);
			this.lvPassResults.Name = "lvPassResults";
			this.lvPassResults.Size = new System.Drawing.Size(640, 376);
			this.lvPassResults.TabIndex = 0;
			this.lvPassResults.View = System.Windows.Forms.View.Details;
			// 
			// chFixture
			// 
			this.chFixture.Text = "Fixture";
			this.chFixture.Width = 96;
			// 
			// chTest
			// 
			this.chTest.Text = "Test";
			this.chTest.Width = 125;
			// 
			// chResults
			// 
			this.chResults.Text = "Results";
			this.chResults.Width = 157;
			// 
			// columnHeader11
			// 
			this.columnHeader11.Text = "Time (avg)";
			this.columnHeader11.Width = 89;
			// 
			// columnHeader12
			// 
			this.columnHeader12.Text = "Ops/Sec.";
			this.columnHeader12.Width = 84;
			// 
			// columnHeader14
			// 
			this.columnHeader14.Text = "Memory";
			// 
			// tpIgnore
			// 
			this.tpIgnore.Controls.Add(this.lvIgnoredResults);
			this.tpIgnore.Location = new System.Drawing.Point(4, 22);
			this.tpIgnore.Name = "tpIgnore";
			this.tpIgnore.Size = new System.Drawing.Size(640, 374);
			this.tpIgnore.TabIndex = 2;
			this.tpIgnore.Text = "Ignore";
			// 
			// lvIgnoredResults
			// 
			this.lvIgnoredResults.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.lvIgnoredResults.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																							   this.columnHeader4,
																							   this.columnHeader5,
																							   this.columnHeader6});
			this.lvIgnoredResults.FullRowSelect = true;
			this.lvIgnoredResults.GridLines = true;
			this.lvIgnoredResults.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this.lvIgnoredResults.Location = new System.Drawing.Point(0, -1);
			this.lvIgnoredResults.Name = "lvIgnoredResults";
			this.lvIgnoredResults.Size = new System.Drawing.Size(640, 376);
			this.lvIgnoredResults.TabIndex = 1;
			this.lvIgnoredResults.View = System.Windows.Forms.View.Details;
			// 
			// columnHeader4
			// 
			this.columnHeader4.Text = "Fixture";
			this.columnHeader4.Width = 96;
			// 
			// columnHeader5
			// 
			this.columnHeader5.Text = "Test";
			this.columnHeader5.Width = 125;
			// 
			// columnHeader6
			// 
			this.columnHeader6.Text = "Results";
			this.columnHeader6.Width = 397;
			// 
			// tpFail
			// 
			this.tpFail.Controls.Add(this.lvFailedResults);
			this.tpFail.Location = new System.Drawing.Point(4, 22);
			this.tpFail.Name = "tpFail";
			this.tpFail.Size = new System.Drawing.Size(640, 374);
			this.tpFail.TabIndex = 3;
			this.tpFail.Text = "Fail";
			// 
			// lvFailedResults
			// 
			this.lvFailedResults.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.lvFailedResults.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																							  this.columnHeader7,
																							  this.columnHeader8,
																							  this.columnHeader9,
																							  this.columnHeader10,
																							  this.columnHeader13,
																							  this.columnHeader15});
			this.lvFailedResults.FullRowSelect = true;
			this.lvFailedResults.GridLines = true;
			this.lvFailedResults.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this.lvFailedResults.Location = new System.Drawing.Point(0, -1);
			this.lvFailedResults.Name = "lvFailedResults";
			this.lvFailedResults.Size = new System.Drawing.Size(640, 376);
			this.lvFailedResults.TabIndex = 1;
			this.lvFailedResults.View = System.Windows.Forms.View.Details;
			// 
			// columnHeader7
			// 
			this.columnHeader7.Text = "Fixture";
			this.columnHeader7.Width = 96;
			// 
			// columnHeader8
			// 
			this.columnHeader8.Text = "Test";
			this.columnHeader8.Width = 125;
			// 
			// columnHeader9
			// 
			this.columnHeader9.Text = "Results";
			this.columnHeader9.Width = 163;
			// 
			// columnHeader10
			// 
			this.columnHeader10.Text = "Time (avg)";
			this.columnHeader10.Width = 87;
			// 
			// columnHeader13
			// 
			this.columnHeader13.Text = "Ops/Sec.";
			this.columnHeader13.Width = 82;
			// 
			// columnHeader15
			// 
			this.columnHeader15.Text = "Memory";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(664, 414);
			this.Controls.Add(this.tcStatus);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.Text = "MUTE";
			this.tcStatus.ResumeLayout(false);
			this.tpRunner.ResumeLayout(false);
			this.gbSeq.ResumeLayout(false);
			this.tpNotRun.ResumeLayout(false);
			this.tpPass.ResumeLayout(false);
			this.tpIgnore.ResumeLayout(false);
			this.tpFail.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		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);
			testRunner.testIterationEvent+=new TestIterationDelegate(IterationCompleted);
			testRunner.completedEvent+=new RunnerCompletedDelegate(RunnerCompleted);
			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);
				allTreeNodes.Add(tnAssembly);
			
				// 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);
					allTreeNodes.Add(tnNamespace);
					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.TestList)
							{
								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);
						allTreeNodes.Add(tnClass);
						tnClass.ImageIndex=0;
						tnClass.ImageIndex=0;

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

						foreach(TestAttribute ta in tf.TestList)
						{
							methods.Add(ta.TestMethod.ToString(), ta);
							if (tf.IsProcessTest)
							{
								sl.Add(ta.TestMethod.Sequence, ta.TestMethod.ToString());
							}
						}

						if (tf.IsProcessTest)
						{
							foreach (DictionaryEntry sortedMethodItem in sl)
							{
								string methodName=sortedMethodItem.Value as string;
								TreeNode tnMethod=tnClass.Nodes.Add(methodName);
								tnMethod.ImageIndex=0;
								tnMethod.SelectedImageIndex=0;
								testToTreeMap.Add(methods[methodName], tnMethod);
								allTreeNodes.Add(tnMethod);
								tnMethod.Tag=methods[methodName];			// currently not used
							}
						}
						else
						{
							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);
								allTreeNodes.Add(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;
				testResults[testState-1].Add(ta);
			}
			pbarIteration.Value=0;
			tbRepNumber.Text="";
		}

		private void IterationCompleted(TestAttribute ta)
		{
			pbarIteration.Maximum=ta.TestMethod.RepeatCount;
			++pbarIteration.Value;
			string s=ta.TestMethod.CurrentRep.ToString()+" / "+ta.TestMethod.RepeatCount.ToString();
			tbRepNumber.Text=s;
		}

		private void RunnerCompleted()
		{
			UpdateDetailResultLists();
		}

		private void ClearTestResults()
		{
			pbarTestProgress.Value=0;
			pbarTestProgress.Maximum=testRunner.NumTests;
			testCounts[0]=0;
			testCounts[1]=0;
			testCounts[2]=0;
			testCounts[3]=0;
			testCounts[4]=0;
			TestRunner.ProcessDirection=rbFwdSeq.Checked ? TestRunner.ProcDir.RunProcessForward : TestRunner.ProcDir.RunProcessReverse;

			for (int i=0; i<4; i++)
			{
				testResults[i].Clear();
			}

			lvNotRunResults.Items.Clear();
			lvPassResults.Items.Clear();
			lvIgnoredResults.Items.Clear();
			lvFailedResults.Items.Clear();

			foreach (TreeNode tn in allTreeNodes)
			{
				tn.ImageIndex=0;
				tn.SelectedImageIndex=0;
			}
		}

		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()
		{
			txtNotRun.Text=testCounts[1].ToString();
			txtPassCount.Text=testCounts[2].ToString();
			txtIgnoreCount.Text=testCounts[3].ToString();
			txtFailCount.Text=testCounts[4].ToString();
		}

		private void btnRun_Click(object sender, System.EventArgs e)
		{
			XmlRegistry reg=new XmlRegistry("config.xml");
			XmlRegistryKey regKey=reg.RootKey;
			XmlRegistryKey lastSelectionKey=regKey.GetSubKey("LastSelections", true);
			lastSelectionKey.SetValue("rbFwd", rbFwdSeq.Checked);
			lastSelectionKey.SetValue("rbRev", rbRevSeq.Checked);

			ClearTestResults();
			testRunner.RunTests();
		}

		private void UpdateDetailResultLists()
		{
			ListView[] views=new ListView[4] {lvNotRunResults, lvPassResults, lvIgnoredResults, lvFailedResults};
			for (int i=0; i<4; i++)
			{
				foreach (TestAttribute ta in testResults[i])
				{
					int n=(int)(1.0/ta.dTime);
					string opsPerSec=n.ToString();
					ListViewItem lvi=new ListViewItem(new string[]
					{
						ta.TestClass.ToString(),
						ta.TestMethod.ToString(),
						ta.Result,
						ta.Time,
						opsPerSec,
						ta.TestMethod.MemoryUsed.ToString(),
					});
					views[i].Items.Add(lvi);
				}
			}
		}
	}
}

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

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