Click here to Skip to main content
15,881,588 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 434.2K   2.6K   206  
Extend Unit Testing So That Entire Processes Can Be Tested
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")]
		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


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