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]
	[ProcessTest]
	public class POSequenceTest
	{
		private PurchaseOrder po;
		private Vendor vendor;
		private Part part1;
		private Part part2;
		private Part part3;
		private WorkOrder wo1;
		private WorkOrder wo2;
		private Invoice invoice;
		private Charge charge;

		[Test, Sequence(1)]
		public void POConstructor()
		{
			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, Sequence(2)]
		public void VendorConstructor()
		{
			vendor=new Vendor();
			Assertion.Assert(vendor.Name=="", "Name is not an empty string.");
			Assertion.Assert(vendor.PartCount==0, "PartCount is not zero.");
		}

		[Test, Sequence(3)]
		public void PartConstructor()
		{
			part1=new Part();
			Assertion.Assert(part1.VendorCost==0, "VendorCost is not zero.");
			Assertion.Assert(part1.Taxable==false, "Taxable is not false.");
			Assertion.Assert(part1.InternalCost==0, "InternalCost is not zero.");
			Assertion.Assert(part1.Markup==0, "Markup is not zero.");
			Assertion.Assert(part1.Number=="", "Number is not an empty string.");

			part2=new Part();
			part3=new Part();
		}

		[Test, Sequence(4), Requires("PartConstructor")]
		public void PartInitialization()
		{
			part1.Number="A";
			part1.VendorCost=15;
			Assertion.Assert(part1.Number=="A", "Number did not get set.");
			Assertion.Assert(part1.VendorCost==15, "VendorCost did not get set.");

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

			part3.Number="C";
			part3.VendorCost=25;
		}

		[Test, Sequence(5), Requires("VendorConstructor")]
		[ReverseProcessExpectedException(typeof(InvalidPartException))]
		public void AddVendorParts()
		{
			vendor.Add(part1);
			vendor.Add(part2);
			vendor.Add(part3);

			Assertion.Assert(vendor.Parts[0].Number=="A", "PartNumber is wrong.");
			Assertion.Assert(vendor.Parts[1].Number=="B", "PartNumber is wrong.");
			Assertion.Assert(vendor.Parts[2].Number=="C", "PartNumber is wrong.");
		}

		[Test, Sequence(6)]
		public void WorkOrderConstructor()
		{
			wo1=new WorkOrder();
			Assertion.Assert(wo1.Number=="", "Number not initialized.");
			Assertion.Assert(wo1.ChargeSlipCount==0, "ChargeSlipCount not initialized.");

			wo2=new WorkOrder();
		}

		[Test, Sequence(7), Requires("WorkOrderConstructor")]
		public void WorkOrderInitialization()
		{
			wo1.Number="000001";
			wo2.Number="000002";

			Assertion.Assert(wo1.Number=="000001", "Number not set.");
			Assertion.Assert(wo2.Number=="000002", "Number not set.");
		}

		[Test, Sequence(8), Requires("POConstructor")]
		public void AssignVendorToPO()
		{
			po.Vendor=vendor;
			Assertion.Assert(po.Vendor==vendor, "Vendor not set.");
		}

		[Test, Sequence(9), Requires("POConstructor")]
		[ReverseProcessExpectedException(typeof(InvalidPartException))]
		[ReverseProcessExpectedException(typeof(InvalidWorkOrderException))]
		public void AddPartsToPO()
		{
			po.Add(part1, wo1);
			po.Add(part2, wo1);
			po.Add(part3, wo2);

			WorkOrder _wo2;
			Part p2;
			po.GetPart(0, out p2, out _wo2);
			Assertion.Assert(p2.Number==part1.Number, "Part number does not match.");
			Assertion.Assert(_wo2.Number==wo1.Number, "Work order number does not match.");
		}

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

		[Test, Sequence(11), Requires("InvoiceConstructor")]
		[ReverseProcessExpectedException(typeof(InvalidVendorException))]
		public void InvoiceInitialization()
		{
			invoice.Number="112233";
			Assertion.Assert(invoice.Number=="112233", "Number not set.");

			invoice.Vendor=vendor;
			Assertion.Assert(invoice.Vendor.Name==vendor.Name, "Vendor name not set.");
		}

		[Test, Sequence(12)]
		public void ChargeConstructor()
		{
			charge=new Charge();
			Assertion.Assert(charge.Description=="", "Description is not an empty string.");
			Assertion.Assert(charge.Amount==0, "Amount is not zero.");
		}

		[Test, Sequence(13), Requires("ChargeConstructor")]
		public void ChargeInitialization()
		{
			charge.Description="Freight";
			charge.Amount=10.50;

			Assertion.Assert(charge.Description=="Freight", "Description is not set.");
			Assertion.Assert(charge.Amount==10.50, "Amount is not set correctly.");
		}

		[Test, Sequence(14), Requires("InvoiceConstructor")]
		[ReverseProcessExpectedException(typeof(InvalidChargeException))]
		public void AddChargeToInvoice()
		{
			invoice.Add(charge);
			Assertion.Assert(invoice.ChargeCount==1, "Charge count wrong.");
			Assertion.Assert(charge.Description==invoice.Charges[0].Description, "Charge description does not match.");
		}

		[Test, Sequence(15), Requires("POConstructor")]
		[ReverseProcessExpectedException(typeof(InvalidInvoiceException))]
		public void AddInvoiceToPO()
		{
			po.Invoice=invoice;
			Assertion.Assert(invoice.Number==po.Invoice.Number, "Invoice not set correctly.");
		}

		[Test]
		[Sequence(16)]
		[Requires("POConstructor")]
		[Requires("WorkOrderConstructor")]
		[ReverseProcessExpectedException(typeof(InvalidInvoiceException))]
		public void ClosePO()
		{
			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 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
Web04 | 2.8.140827.1 | Last Updated 9 Oct 2003
Article Copyright 2003 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid