Introduction to Unit Testing using NUnit 2.0 - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Introduction to Unit Testing using NUnit 2.0

Description:

testEmployeeTypePD = new EmployeeTypePD(id, desc, min, max); // get and compare ... Check boundary conditions and failure scenarios (forced-error tests) ... – PowerPoint PPT presentation

Number of Views:676
Avg rating:3.0/5.0
Slides: 10
Provided by: Prefer413
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Unit Testing using NUnit 2.0


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/mtanu
    an/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
  • lt?xml version"1.0" encoding"Windows-1252"?gt
  • ltconfigurationgt
  • ltstartupgt ltsupportedRuntime version"v1.1.4322"
    /gt lt/startupgt
  • ltappSettingsgt
  • lt!-- User application and configured
    property settings go here.--gt
  • lt!-- Example ltadd key"settingName"
    value"settingValue"/gt --gt
  • ltadd key"toolTip.ShowAlways" value"False"
    /gt
  • ltadd key"shadowfiles.path"
    value"temp\nunit20\ShadowCopyCache" /gt
  • lt/appSettingsgt
  • lt/configurationgt

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()descmin.ToString()max
    .ToString()
  • // call the constructor method
  • testEmployeeTypePD new EmployeeTypePD(id,
    desc, min, max)
  • // get and compare
  • actual testEmployeeTypePD.GetEmployeeTypeId().
    ToString()
  • testEmployeeTypePD.GetDescription()

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.
Write a Comment
User Comments (0)
About PowerShow.com