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

namespace CaseStudy
{
	[TestFixture]
	public class PartTest
	{
		[Test]
		public void ConstructorInitialization()
		{
			Part part=new Part();
			Assertion.Assert(part.VendorCost==0, "VendorCost is not zero.");
			Assertion.Assert(part.Taxable==false, "Taxable is not false.");
			Assertion.Assert(part.InternalCost==0, "InternalCost is not zero.");
			Assertion.Assert(part.Markup==0, "Markup is not zero.");
			Assertion.Assert(part.Number=="", "Number is not an empty string.");
		}

		[Test]
		public void SetVendorInfo()
		{
			Part part=new Part();
			part.Number="FIG 4RAC #R11T";
			part.VendorCost=12.50;
			part.Taxable=true;
			part.InternalCost=13.00;
			part.Markup=2.0;
		
			Assertion.Assert(part.Number=="FIG 4RAC #R11T", "Number did not get set.");
			Assertion.Assert(part.VendorCost==12.50, "VendorCost did not get set.");
			Assertion.Assert(part.Taxable==true, "Taxable did not get set.");
			Assertion.Assert(part.InternalCost==13.00, "InternalCost did not get set.");
			Assertion.Assert(part.Markup==2.0, "Markup did not get set.");
		}
	}

	[TestFixture]
	public class VendorTest
	{
		private Vendor vendor;

		[TestSetUp]
		public void VendorSetUp()
		{
			vendor=new Vendor();
		}

		[Test]
		public void ConstructorInitialization()
		{
			Assertion.Assert(vendor.Name=="", "Name is not an empty string.");
			Assertion.Assert(vendor.PartCount==0, "PartCount is not zero.");
		}

		[Test]
		public void VendorName()
		{
			vendor.Name="Jamestown Distributors";
			Assertion.Assert(vendor.Name=="Jamestown Distributors", "Name did not get set.");
			}

		[Test]
		public void AddUniqueParts()
		{
			CreateTestParts();
			Assertion.Assert(vendor.PartCount==2, "PartCount is not 2.");
		}

		[Test]
		public void RetrieveParts()
		{
			CreateTestParts();
			Part part;
			part=vendor.Parts[0];
			Assertion.Assert(part.Number=="BOD-13-25P", "PartNumber is wrong.");
			part=vendor.Parts[1];
			Assertion.Assert(part.Number=="BOD-13-33P", "PartNumber is wrong.");
		}

		[Test, ExpectedException(typeof(DuplicatePartException))]
		public void DuplicateParts()
		{
			Part part=new Part();
			part.Number="Same Part Number";
			vendor.Add(part);
			vendor.Add(part);
		}

		[Test, ExpectedException(typeof(UnassignedPartException))]
		public void UnassignedPartNumber()
		{
			Part part=new Part();
			vendor.Add(part);
		}

		void CreateTestParts()
		{
			Part part1=new Part();
			part1.Number="BOD-13-25P";
			vendor.Add(part1);
		
			Part part2=new Part();
			part2.Number="BOD-13-33P";
			vendor.Add(part2);
		}	
	}

	[TestFixture]
	public class ChargeTest
	{
		[Test]
		public void ConstructorInitialization()
		{
			Charge charge=new Charge();
			Assertion.Assert(charge.Description=="", "Description is not an empty string.");
			Assertion.Assert(charge.Amount==0, "Amount is not zero.");
		}
	
		[Test]
		public void SetChargeInfo()
		{
			Charge charge=new Charge();
			charge.Description="Freight";
			charge.Amount=8.50;
		
			Assertion.Assert(charge.Description=="Freight", "Description is not set.");
			Assertion.Assert(charge.Amount==8.50, "Amount is not set correctly.");
		}
	}

	[TestFixture]
	public class ChargeSlipTest
	{
		private ChargeSlip chargeSlip;

		[TestSetUp]
		public void SetUp()
		{
			chargeSlip=new ChargeSlip();
		}

		[Test]
		public void ConstructorInitialization()
		{
			Assertion.Assert(chargeSlip.Number=="", "Number is not initialized correctly.");
			Assertion.Assert(chargeSlip.PartCount==0, "PartCount is not zero.");
			Assertion.Assert(chargeSlip.ChargeCount==0, "ChargeCount is not zero.");
		}

		[Test]
		public void ChargeSlipNumberAssignment()
		{
			chargeSlip.Number="123456";
			Assertion.Assert(chargeSlip.Number=="123456", "Number is not set correctly.");
			}

		[Test, ExpectedException(typeof(BadChargeSlipNumberException))]
		public void BadChargeSlipNumber()
		{
			chargeSlip.Number="12345";			// must be six digits or letters
		}

		[Test]
		public void AddPart()
		{
			Part part=new Part();
			part.Number="VOD-13-33P";
			chargeSlip.Add(part);
			Assertion.Assert(chargeSlip.PartCount==1, "PartCount is wrong.");
		}

		[Test]
		public void AddCharge()
		{
			Charge charge=new Charge();
			charge.Description="Freight";
			charge.Amount=10.50;		
			chargeSlip.Add(charge);
			Assertion.Assert(chargeSlip.ChargeCount==1, "ChargeCount is wrong.");
		}

		[Test]
		public void RetrievePart()
		{
			Part part=new Part();
			part.Number="VOD-13-33P";
			chargeSlip.Add(part);
			Part p2=chargeSlip.Parts[0];
			Assertion.Assert(p2.Number==part.Number, "Part numbers do not match.");
		}

		[Test]
		public void RetrieveCharge()
		{
			Charge charge=new Charge();
			charge.Description="Freight";
			charge.Amount=10.50;		
			chargeSlip.Add(charge);
			Charge c2=chargeSlip.Charges[0];
			Assertion.Assert(c2.Description==charge.Description, "Descriptions do not match.");
		}

		[Test, ExpectedException(typeof(UnassignedPartException))]
		public void AddUnassignedPart()
		{
			Part part=new Part();
			chargeSlip.Add(part);
		}

		[Test, ExpectedException(typeof(UnassignedChargeException))]
		public void UnassignedCharge()
		{
			Charge charge=new Charge();
			chargeSlip.Add(charge);
		}
	}

	[TestFixture]
	public class WorkOrderTest
	{
		private WorkOrder workOrder;

		[TestSetUp]
		public void WorkOrderSetUp()
		{
			workOrder=new WorkOrder();
		}

		[Test]
		public void ConstructorInitialization()
		{
			Assertion.Assert(workOrder.Number=="", "Number not initialized.");
			Assertion.Assert(workOrder.ChargeSlipCount==0, "ChargeSlipCount not initialized.");
		}

		[Test]
		public void WorkOrderNumber()
		{
			workOrder.Number="112233";
			Assertion.Assert(workOrder.Number=="112233", "Number not set.");
			}

		[Test, ExpectedException(typeof(BadWorkOrderNumberException))]
		public void BadWorkOrderNumber()
		{
			workOrder.Number="12345";
		}

		[Test]
		public void AddChargeSlip()
		{
			ChargeSlip chargeSlip=new ChargeSlip();
			chargeSlip.Number="123456";
			workOrder.Add(chargeSlip);
			Assertion.Assert(workOrder.ChargeSlipCount==1, "ChargeSlip not added.");
		}

		[Test]
		public void RetrieveChargeSlip()
		{
			ChargeSlip chargeSlip=new ChargeSlip();
			chargeSlip.Number="123456";
			workOrder.Add(chargeSlip);
			ChargeSlip cs2=workOrder.ChargeSlips[0];
			Assertion.Assert(chargeSlip.Number==cs2.Number, "ChargeSlip numbers do not match.");
		}

		[Test, ExpectedException(typeof(DuplicateChargeSlipException))]
		public void DuplicateChargeSlip()
		{
			ChargeSlip chargeSlip=new ChargeSlip();
			chargeSlip.Number="123456";
			workOrder.Add(chargeSlip);
			workOrder.Add(chargeSlip);
		}

		[Test, ExpectedException(typeof(UnassignedChargeSlipException))]
		public void UnassignedChargeSlipNumber()
		{
			ChargeSlip chargeSlip=new ChargeSlip();
			workOrder.Add(chargeSlip);
		}
	}

	[TestFixture]
	public class InvoiceTest
	{
		private Invoice invoice;

		[TestSetUp]
		public void InvoiceSetUp()
		{
			invoice=new Invoice();
		}

		[Test]
		public void ConstructorInitialization()
		{
			Assertion.Assert(invoice.Number=="", "Number not initialized.");
			Assertion.Assert(invoice.ChargeCount==0, "ChargeCount not initialized.");
			Assertion.Assert(invoice.Vendor==null, "Vendor not initialized.");
		}

		[Test]
		public void InvoiceNumber()
		{
			invoice.Number="112233";
			Assertion.Assert(invoice.Number=="112233", "Number not set.");
		}

		[Test]
		public void InvoiceVendor()
		{
			Vendor vendor=new Vendor();
			vendor.Name="Nantucket Parts";
			invoice.Vendor=vendor;
			Assertion.Assert(invoice.Vendor.Name==vendor.Name, "Vendor name not set.");
		}

		[Test]
		public void AddCharge()
		{
			Charge charge=new Charge();
			charge.Description="Freight";
			invoice.Add(charge);
			Assertion.Assert(invoice.ChargeCount==1, "Charge count wrong.");
		}

		[Test]
		public void RetrieveCharge()
		{
			Charge charge=new Charge();
			charge.Description="123456";
			invoice.Add(charge);
			Charge c2=invoice.Charges[0];
			Assertion.Assert(charge.Description==c2.Description, "Charge description does not match.");
		}

		[Test, ExpectedException(typeof(UnassignedChargeException))]
		public void UnassignedChargeNumber()
		{
			Charge charge=new Charge();
			invoice.Add(charge);
		}
	}

	[TestFixture]
	public class CustomerTest
	{
		private Customer customer;

		[TestSetUp]
		public void CustomerSetUp()
		{
			customer=new Customer();
		}

		[Test]
		public void ConstructorInitialization()
		{
			Assertion.Assert(customer.Name=="", "Name not initialized.");
			Assertion.Assert(customer.WorkOrderCount==0, "WorkOrderCount not initialized.");
		}

		[Test]
		public void CustomerName()
		{
			customer.Name="Marc Clifton";
			Assertion.Assert(customer.Name=="Marc Clifton", "Name not set.");
		}

		[Test]
		public void AddWorkOrder()
		{
			WorkOrder workOrder=new WorkOrder();
			workOrder.Number="123456";
			customer.Add(workOrder);
			Assertion.Assert(customer.WorkOrderCount==1, "Work order not added.");
		}

		[Test]
		public void RetrieveWorkOrder()
		{
			WorkOrder workOrder=new WorkOrder();
			workOrder.Number="123456";
			customer.Add(workOrder);
			WorkOrder wo2=customer.WorkOrders[0];
			Assertion.Assert(workOrder.Number==wo2.Number, "WorkOrder numbers do not match.");
		}

		[Test, ExpectedException(typeof(UnassignedWorkOrderException))]
		public void UnassignedWorkOrderNumber()
		{
			WorkOrder workOrder=new WorkOrder();
			customer.Add(workOrder);
		}
	}

	[TestFixture]
	public class PurchaseOrderTest
	{
		private PurchaseOrder po;
		private Vendor vendor;

		[TestSetUp]
		public void PurchaseOrderSetUp()
		{
			po=new PurchaseOrder();
			vendor=new Vendor();
			vendor.Name="West Marine";
			po.Vendor=vendor;
		}

		[Test]
		public void ConstructorInitialization()
		{
			PurchaseOrder po=new PurchaseOrder();
			Assertion.Assert(po.Number=="", "Number not initialized.");
			Assertion.Assert(po.PartCount==0, "PartCount not initialized.");
			Assertion.Assert(po.ChargeCount==0, "ChargeCount not initialized.");
			Assertion.Assert(po.Invoice==null, "Invoice not initialized.");
			Assertion.Assert(po.Vendor==null, "Vendor not initialized.");
		}

		[Test]
		public void PONumber()
		{
			po.Number="123456";
			Assertion.Assert(po.Number=="123456", "Number not set.");
		}

		[Test]
		public void AddPart()
		{
			WorkOrder workOrder=new WorkOrder();
			workOrder.Number="123456";
			Part part=new Part();
			part.Number="112233";
			vendor.Add(part);
			po.Add(part, workOrder);
			WorkOrder wo2;
			Part p2;
			po.GetPart(0, out p2, out wo2);
			Assertion.Assert(p2.Number==part.Number, "Part number does not match.");
			Assertion.Assert(wo2.Number==workOrder.Number, "Work order number does not match.");
		}

		[Test, ExpectedException(typeof(PartNotFromVendorException))]
		public void AddPartNotFromVendor()
		{
			WorkOrder workOrder=new WorkOrder();
			workOrder.Number="123456";
			Part part=new Part();
			part.Number="131133";
			po.Add(part, workOrder);
		}

		[Test, ExpectedException(typeof(DifferentVendorException))]
		public void AddInvoiceFromDifferentVendor()
		{
			Vendor vendor1=new Vendor();
			vendor1.Name="ABC Co.";
			po.Vendor=vendor1;
			Invoice invoice=new Invoice();
			invoice.Number="123456";
			Vendor vendor2=new Vendor();
			vendor2.Name="XYZ Inc.";
			invoice.Vendor=vendor2;
			po.Invoice=invoice;
		}

		[Test, ExpectedException(typeof(UnassignedWorkOrderException))]
		public void UnassignedWorkOrderNumber()
		{
			WorkOrder workOrder=new WorkOrder();
			Part part=new Part();
			part.Number="112233";
			po.Add(part, workOrder);
		}

		[Test, ExpectedException(typeof(UnassignedPartException))]
		public void UnassignedPartNumber()
		{
			WorkOrder workOrder=new WorkOrder();
			workOrder.Number="123456";
			Part part=new Part();
			po.Add(part, workOrder);
		}

		[Test, ExpectedException(typeof(UnassignedInvoiceException))]
		public void UnassignedInvoiceNumber()
		{
			Invoice invoice=new Invoice();
			po.Invoice=invoice;
		}

		[Test]
		public void ClosePO()
		{
			WorkOrder wo1=new WorkOrder();
			WorkOrder wo2=new WorkOrder();

			wo1.Number="000001";
			wo2.Number="000002";

			Part p1=new Part();
			Part p2=new Part();
			Part p3=new Part();

			p1.Number="A";
			p1.VendorCost=15;

			p2.Number="B";
			p2.VendorCost=20;

			p3.Number="C";
			p3.VendorCost=25;

			vendor.Add(p1);
			vendor.Add(p2);
			vendor.Add(p3);

			po.Add(p1, wo1);
			po.Add(p2, wo1);
			po.Add(p3, wo2);

			Charge charge=new Charge();
			charge.Description="Freight";
			charge.Amount=10.50;

			Invoice invoice=new Invoice();
			invoice.Number="1234";
			invoice.Vendor=vendor;
			invoice.Add(charge);

			po.Invoice=invoice;

			po.Close();

			// one charge slip should be added to both work orders
			Assertion.Assert(wo1.ChargeSlipCount==1, "First work order: ChargeSlipCount not 1.");
			Assertion.Assert(wo2.ChargeSlipCount==1, "Second work order: ChargeSlipCount not 1.");

			ChargeSlip cs1=wo1.ChargeSlips[0];
			ChargeSlip cs2=wo2.ChargeSlips[0];

			// three charges should exist for charge slip #1: two parts and one freight charge
			Assertion.Assert(cs1.PartCount + cs1.ChargeCount==3, "Charge slip 1: doesn't have three charges.");

			// the freight for CS1 should be 10.50 * (15+20)/(15+20+25) = 6.125
			Assertion.Assert(cs1.Charges[0].Amount==6.125, "Charge slip 1: charge not the correct amount.");

			// two charges should exist for charge slip #2: one part and one freight charge
			Assertion.Assert(cs2.PartCount + cs2.ChargeCount==2, "Charge slip 2: doesn't have two charges.");

			// the freight for CS2 should be 10.50 * 25/(15+20+25) = 4.375  (also = 10.50-6.125)
			Assertion.Assert(cs2.Charges[0].Amount==4.375, "Charge slip 2: charge not the correct amount.");

			// while we have a unit test that verifies that parts are added to charge slips correctly,
			// we don't have a unit test to verify that the purchase order Close process does this
			// correctly.

			Part cs1p1=cs1.Parts[0];
			Part cs1p2=cs1.Parts[1];
			if (cs1p1.Number=="A")
			{
				Assertion.Assert(cs1p1.VendorCost==15, "Charge slip 1, vendor cost not correct for part A.");
			}
			else if (cs1p1.Number=="B")
			{
				Assertion.Assert(cs1p1.VendorCost==20, "Charge slip 1, vendor cost not correct for part B.");
			}
			else
			{
				throw(new IncorrectChargeSlipException());
			}

			Assertion.Assert(cs1p1.Number != cs1p2.Number, "Charge slip part numbers are not unique.");

			if (cs1p2.Number=="A")
			{
				Assertion.Assert(cs1p2.VendorCost==15, "Charge slip 1, vendor cost is not correct for part A.");
			}
			else if (cs1p2.Number=="B")
			{
				Assertion.Assert(cs1p2.VendorCost==20, "Charge slip 1, vendor cost is not correct for part B.");
			}
			else
			{
				throw(new IncorrectChargeSlipException());
			}

			Assertion.Assert(cs2.Parts[0].Number=="C", "Charge slip 2, part number is not correct.");
			Assertion.Assert(cs2.Parts[0].VendorCost==25, "Charge slip 2, vendor cost is not correct for part C.");		
		}
	}
}

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