1 / 9

Introduction to Unit Testing using NUnit 2.0

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

dafydd
Télécharger la présentation

Introduction to Unit Testing using NUnit 2.0

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Unit Testing using NUnit 2.0

  2. 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

  3. NUnit Demo • Click Payroll.exe • Click Run • Green bar means code is clean • Rerun unit tests after code changes

  4. 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

  5. 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>

  6. 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); } }

  7. 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]

  8. 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)

  9. 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.

More Related