170 likes | 466 Views
Pavan Upadhya pavan.upadhya@hp.com. NUnit – A Unit Test Framework for .Net under Mono. Scope. Not a tutorial Gives high level picture May prompt the audience to give it a try if not already done it. Topics. A quick look at Mono. What is NUnit ? Features Writing a Unit Test
E N D
Pavan Upadhya pavan.upadhya@hp.com NUnit – A Unit Test Framework for .Net under Mono
Scope • Not a tutorial • Gives high level picture • May prompt the audience to give it a try if not already done it.
Topics • A quick look at Mono. • What is NUnit ? • Features • Writing a Unit Test • Code Examples • Pros and Cons • Conclusion
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.
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.
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.
Features (Cont.) • NUnit provides a rich set of assertions. • Has both Console and GUI interface. • Can be called from NAnt as a task.
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]
Writing a Unit Test Code (Cont..) • Other Attributes include • SetUp / Teardown • ExpectedException ( typeof( Exception)) • Explicit • Ignore
Example (sample C# source) namespace bank { public class Account { private float balance; public void Deposit(float amount) { balance+=amount; } 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; } } }
Sampletestcode (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); } } }
TestCode (Exception handling) namespace sample { using NUnit.Framework; [TestFixture] public class SuccessTests { [Test] [ExpectedException(typeof(InvalidOperationException))] public void ExpectAnException() { /* ... */ } } }
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.
Cons • C based extensions not available. • Lack of awareness among .Net Community.
Resources • http://www.nunit.org • http://www.xprogramming.com/xpmag/acsUsingNUnit.htm#N74