Click here to Skip to main content
15,884,078 members
Articles / Programming Languages / C#

Advanced Unit Testing, Part III - Testing Processes

Rate me:
Please Sign up or sign in to vote.
4.89/5 (53 votes)
28 Sep 200314 min read 435.2K   2.6K   206  
Extend Unit Testing So That Entire Processes Can Be Tested
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.
*/

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 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 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();
			}

			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;

			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.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.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.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, 352);
			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, 216);
			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, 240);
			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, 264);
			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(368, 216);
			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(368, 240);
			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(368, 264);
			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, 392);
			this.tcStatus.TabIndex = 10;
			// 
			// tpRunner
			// 
			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, 366);
			this.tpRunner.TabIndex = 4;
			this.tpRunner.Text = "Runner";
			// 
			// txtNotRun
			// 
			this.txtNotRun.Location = new System.Drawing.Point(368, 192);
			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, 192);
			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, 366);
			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.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, 368);
			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 = 397;
			// 
			// 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, 366);
			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.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, 368);
			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 = 397;
			// 
			// 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, 366);
			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.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, 368);
			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, 366);
			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.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, 368);
			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 = 397;
			// 
			// 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() 
		{
			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);
			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.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);
						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);
								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);
								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);
			}
		}

		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);

			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;
			testRunner.RunTests();
			UpdateDetailResultLists();
		}

		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])
				{
					ListViewItem lvi=new ListViewItem(new string[]
					{
						ta.TestClass.ToString(),
						ta.TestMethod.ToString(),
						ta.Result
					});
					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


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