Click here to Skip to main content
15,880,392 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 206.8K   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.Diagnostics;
using System.Threading;

using KALib;

namespace UTCore
{
	public abstract class TestFixtureRunner
	{
		protected TestFixture tf;

		public abstract void RunTests();
		public abstract bool ExceptionConfirmed(Exception e, TestAttribute ta);

		protected TestFixtureRunner(TestFixture tf)
		{
			this.tf=tf;
		}

		protected TestAttribute.TestState RunTest(object instance, TestAttribute ta)
		{
			if (!ta.IgnoreTest)
			{
				SortableList runTimeList=new SortableList();
				for (int i=0; i<ta.TestMethod.RepeatCount; i++)
				{
					ta.TestMethod.CurrentRep=i;
					try
					{
						ta.Result="";
						tf.TestSetUp.Invoke(instance);
						Thread.Sleep(0);	// let other threads execute
						ta.Invoke(instance);
						// If we get here, the test did not throw an exception.
						// Was it supposed too?
						if (ta.ExpectedExceptionType != null)
						{
							Trace.WriteLine("***Fail***: "+ta.TestMethod.ToString()+" Expected exception not encountered");
							ta.State=TestAttribute.TestState.Fail;
							ta.Result="Expected exception "+ta.ExpectedExceptionType.ToString()+" not encountered.";
						}

							// did we meet the min. operations per second requirement?
						else if ( (ta.TestMethod.MinOPS > 0) && (1.0/ta.dTime < ta.TestMethod.MinOPS) )
						{
							Trace.WriteLine("***Fail***: "+ta.TestMethod.ToString()+" Min. operations/sec not met.");
							ta.State=TestAttribute.TestState.Fail;
							ta.Result="Min. operations/sec not met.";
						}
							// did we meet the max. memory allowed?
						else if ( (ta.TestMethod.MaxK > 0) && (ta.TestMethod.MemoryUsed > ta.TestMethod.MaxK*1000) )
						{
							Trace.WriteLine("***Fail***: "+ta.TestMethod.ToString()+" Max. memory exceeded");
							ta.State=TestAttribute.TestState.Fail;
							ta.Result="Max. memory utilization exceeded.";
						}

						else
						{
							Trace.WriteLine("***Pass***: "+ta.TestMethod.ToString());
							ta.State=TestAttribute.TestState.Pass;
						}
					}

					catch(UnitTest.AssertionException e)
					{
						Trace.WriteLine("***Fail***: "+ta.TestMethod.ToString()+" Exception="+e.Message);
						ta.State=TestAttribute.TestState.Fail;
						ta.Result="Assertion failed: "+e.Message;
					}

					catch(Exception e)
					{
						if (ExceptionConfirmed(e, ta))
						{
							Trace.WriteLine("***Pass***: "+ta.TestMethod.ToString()+" Exception="+e.Message);
							ta.State=TestAttribute.TestState.Pass;
						}
						else
						{
							Trace.WriteLine("***Fail***: "+ta.TestMethod.ToString()+" Exception="+e.Message);
							ta.State=TestAttribute.TestState.Fail;
							ta.Result="Exception occurred: "+e.Message;
						}
					}
					finally
					{
						runTimeList.Add(ta.dTime);
						tf.TestTearDown.Invoke(instance);
						if (ta.TestMethod.RepeatCount > 1)
						{
							// only fire this event when we're really running a test repeatedly
							tf.IterationEvent(ta);
						}
					}

					// if all the repetitions succeeded, then take the average execution time, ignoring the
					// the best and worst times.  (This also ignores samples under 3).
					double total=0;
					for (int j=1; j<runTimeList.Count-1; j++)
					{
						total+=(double)runTimeList[j];
					}
					ta.dTime=(total==0.0 ? ta.dTime : ta.dTime)/runTimeList.Count;
					runTimeList.Clear();

					if (ta.TestMethod.RepeatDelay > 0)
					{
						Thread.Sleep(ta.TestMethod.RepeatDelay);
					}

				}
			}
			else
			{
				Trace.WriteLine("***Ignore***: "+ta.TestMethod.ToString());
				ta.Result=ta.IgnoreReason;
				ta.State=TestAttribute.TestState.Ignore;
			}
			tf.NotificationEvent(ta);
			return ta.State;
		}
	}
}

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