NUnit
NUnit
Strategies
Traditional Testing
Test the system as a whole Individual components rarely tested Errors go undetected Isolation of errors difficult to track down
Unit Testing
Each part tested individually All components tested at least once Errors picked up earlier Scope is smaller, easier to fix errors
Faster Debugging Faster Development Better Design Excellent Regression Tool Reduce Future Cost
Unit Tests
Simple Standalone Classes High Level Classes Database based Classes Integrated Framework Classes
nUnit
Add nunit.frameworks.dll to your project references. This dll resides in nunit\bin folder. The next few screen shots show how to add this dll file if your project has been opened as a classlibrary.
Select Browse
TestFixture Example
[TestFixture] public class calcTest{ [Test] public void AdditionTests(){ calculator cal=new calculator(); cal.Addition(5,10); //CASE 1 Assert.AreEqual(15,cal.output()); cal.Addition(-5,10); // CASE 2 Assert.AreEqual(15,cal.output());
[Test] public void DivisionTests(){ calculator cal=new calculator(); cal.Division(10,2); // CASE 1 Assert.AreEqual(5,cal.output()); cal.Division(10,0); //CASE 2 Assert.AreEqual(2,cal.output());
} }//for testfixture
Suppose you want to test the functions in a class, your testfixture(test stub) should look somewhat like this
using NUnit.Framework; Namespace sample1{ public class sampleclass(){ public void func1(){} }//for sampleclass [TestFixture] public class sampleTest(){ [Test] public void test1(){ testcase1; testcase2; }//for test1() }//for sampleTest textfixture }//for namespace
Calculator Example
using System; using NUnit.Framework; namespace ClassLibrary1 { public class calculator { private int result; public void Addition(int num1,int num2){ result=num1+num2; } public int output(){ return result; } public void Division(int num1,int num2){ if(num2==0){ throw new DivideByZeroException(); } result = num1/num2; }
[Test] public void DivisionTests(){ calculator cal=new calculator(); cal.Division(10,2); Console.Write("TESTING 10div2\n"); Assert.AreEqual(5,cal.output()); cal.Division(10,0); Console.Write("TESTING 10div0\n"); Assert.AreEqual(0,cal.output());
After the testfixture is complete, build the project. This will create a dll file which will reside in projectname\bin\debug folder.
LOCATION of dll
In this example I am copying the dll file from projectname\bin\debug folder to nunit\bin folder for my convenience. But this is not a requirement when we are testing and the dll can be run from projectname\bin\debug folder.
GUI MODE
Load the Dll file. Select Run. View the errors if any on the GUI screen. Save the output XML if necessary.
Select the dll which you want to test(In this case, we have moved the dll to the nunit\bin folder)
Runs Perfectly
Tests Fail
In the GUI mode, the user must specify that he/she wants to save the output. No xml output file is automatically created on the users behalf. But in Console mode, a XML file is created even if not specified by the user.
Console mode
The command to execute the exe in console mode is nunit-console.exe [filename] [option] Example: nunit-console.exe classlibrary1.dll XML=ourfile.xml
Runs Perfectly
Test Fails
Output file
As mentioned earlier, in console mode the xml file is automatically created as testresult.xml in the nunit\bin folder. The user can change the name of the output file by saying -XML = newfilename.xml as shown in the previous slide.
Test Failed
Avoid setup in constructor Define tests correctly Minimize side-effects of unit tests Keep tests small and fast Automate all processes Write effective exception handling code Add a test case for every bug exposed Refactor, refactor, refactor