220 likes | 734 Views
Introduction to Unit Testing using NUnit 2.0. NUnit V2.0. NUnit 2.0 Unit testing framework for .NET Open source, written in C# http://www.nunit.org/ Mainly for component/server-side testing Not for GUI testing Assert-driven testing. NUnit Demo. Click Payroll.exe Click Run
E N D
NUnit V2.0 • NUnit 2.0 • Unit testing framework for .NET • Open source, written in C# • http://www.nunit.org/ • Mainly for component/server-side testing • Not for GUI testing • Assert-driven testing
NUnit Demo • Click Payroll.exe • Click Run • Green bar means code is clean • Rerun unit tests after code changes
Starting NUnit • Nunit has both GUI and console interfaces • NUnit Tutorial: http://www.conestogac.on.ca/~mtanuan/winoop03/tutorials/NunitTutorial.htm • NUnit Guide: http://www.conestogac.on.ca/~mtanuan/winoop03/assignments/NunitGuideToAssignments.htm
Nunit-gui.exe.config • In the config file: C:\Program Files\NUnit V2.0\bin\nunit-gui.exe.config • Add startup option for .NET v1.1 • Otherwise, Error: Invalid PInvoke Metadata Format <?xml version="1.0" encoding="Windows-1252"?> <configuration> <startup> <supportedRuntime version="v1.1.4322" /> </startup> <appSettings> <!-- User application and configured property settings go here.--> <!-- Example: <add key="settingName" value="settingValue"/> --> <add key="toolTip.ShowAlways" value="False" /> <add key="shadowfiles.path" value="%temp%\nunit20\ShadowCopyCache" /> </appSettings> </configuration>
Sample Unit Test Code [TestFixture] public class EmployeeTypePDTest { // instance variables private EmployeeTypePD testEmployeeTypePD; private Random randomNumber = new Random(); public void TestConstructor() { string expected, actual; // test constructor with parameters int id = 101; string desc = "My Employee"; double min = 6.50; double max = 10.50; expected = id.ToString()+desc+min.ToString()+max.ToString(); // call the constructor method testEmployeeTypePD = new EmployeeTypePD(id, desc, min, max); // get and compare actual = testEmployeeTypePD.GetEmployeeTypeId().ToString() + testEmployeeTypePD.GetDescription() + testEmployeeTypePD.GetMinRate().ToString() + testEmployeeTypePD.GetMaxRate().ToString(); Assertion.AssertEquals("Sample: ", expected, actual); } }
NUnit Attributes • [TestFixture] • Public class that contains test methods • [Test] • Public void method without parameters • Contains the test case code • Optional • Others (not required for Assignment 3) • [SetUp] • [TearDown] • [ExpectedException] • [Ignore]
Unit Testing: Coding Tips • Method Call Coverage • All public methods must be called at least once • Indirect calls allowed • E.g., EmployeeTypePD.Find() calls EmployeeTypeDA.Find() • Assertions: • Use AssertEquals(string, object, object) method • Consider use of Random class • Advanced (not in Assignment 3): • Check boundary conditions and failure scenarios (forced-error tests)
Test Suite with Varied Inputs • To run the same test case methods with varied inputs. • Create abstract class that contains all the test case methods • Add a method to accept and set the input values • Create child classes that sets varied input values.