NUnit - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

NUnit

Description:

NUnit uses the 'attribute' feature of .NET to identify tests. Features ... Every Test Class must have the attribute [TestFixture] ... – PowerPoint PPT presentation

Number of Views:305
Avg rating:3.0/5.0
Slides: 17
Provided by: foss
Category:
Tags: attribute | nunit

less

Transcript and Presenter's Notes

Title: NUnit


1
NUnit A Unit Test Framework for .Net under Mono
  • Pavan Upadhya
  • pavan.upadhya_at_hp.com

2
Scope
  • Not a tutorial
  • Gives high level picture
  • May prompt the audience to give it a try if not
    already done it.

3
Topics
  • A quick look at Mono.
  • What is NUnit ?
  • Features
  • Writing a Unit Test
  • Code Examples
  • Pros and Cons
  • Conclusion

4
Mono
  • The Mono project is sponsored by Novell
  • It is designed to implement the .NET Development
    Framework standard across various platforms.
  • It is open source code.

5
What is NUnit?
  • NUnit is a unit-testing framework for all .Net
    languages.
  • It is written entirely in C.
  • NUnit is much the same as all the Extreme
    Programming test frameworks (xUnits)
  • NUnit was initially ported from JUnit.

6
Features
  • NUnit is simple to understand and easy to learn.
  • Object Oriented.
  • NUnit is flexible - Test code can be in any .Net
    language.
  • NUnit uses the "attribute" feature of .NET to
    identify tests.

7
Features (Cont.)
  • NUnit provides a rich set of assertions.
  • Has both Console and GUI interface.
  • Can be called from NAnt as a task.

8
Writing a Unit Test Code
  • Every Class must have a corresponding Test Class
    named TestClassName
  • One test method per public method, named
    testMethodName.
  • In each test method, test good results and
    failures
  • Every Test Class must have the attribute
    TestFixture
  • Each Test Method must have the attribute Test

9
Writing a Unit Test Code (Cont..)
  • Other Attributes include
  • SetUp / Teardown
  • ExpectedException ( typeof( Exception))
  • Explicit
  • Ignore

10
Example (sample C source)
  • namespace bank
  • public class Account
  • private float balance
  • public void Deposit(float amount)
  • balanceamount
  • public void Withdraw(float amount)
  • balance-amount
  • public void TransferFunds(Account destination,
    float amount)
  • destination.Deposit(amount)
  • Withdraw(amount)
  • public float Balance
  • get return balance

11
Sample test code (in C)
  • namespace bank
  • using NUnit.Framework
  • TestFixture
  • public class TestAccount
  • Account source, destination
  • TestFixtureSetUp
  • public void init()
  • Account source new Account()
  • Account destination new Account()
  • Test
  • public void TestTransferFunds()
  • source.Deposit(200.00F)
  • destination.Deposit(150.00F)
  • source.TransferFunds(destination, 100.00F)
  • Assert.AreEqual(250.00F, destination.Balance)
  • Assert.AreEqual(100.00F, source.Balance)

12
TestCode (Exception handling)
  • namespace sample
  • using NUnit.Framework
  • TestFixture
  • public class SuccessTests
  • Test
  • ExpectedException(typeof(InvalidOperationExcep
    tion))
  • public void ExpectAnException()
  • / ... /

13
Pros
  • NUnit is Open Source and highly extensible.
  • Platform independent.
  • Simple and easy to learn syntax.
  • When integrated with NAnt, can be used for
    incremental projects.

14
Cons
  • C based extensions not available.
  • Lack of awareness among .Net Community.

15
Resources
  • http//www.nunit.org
  • http//www.xprogramming.com/xpmag/acsUsingNUnit.ht
    mN74

16
Questions
Write a Comment
User Comments (0)
About PowerShow.com