Sunteți pe pagina 1din 47

Unit Testing vs Traditional Testing Benefits of Unit Testing Introduction to xUnit (using NUnit) frameworks Advanced Unit Testing

Strategies

Traditional Testing

Test the system as a whole Individual components rarely tested Errors go undetected Isolation of errors difficult to track down

Traditional Testing Strategies


Print Statements Use of Debugger Debugger Expressions Test Scripts

Unit Testing

Each part tested individually All components tested at least once Errors picked up earlier Scope is smaller, easier to fix errors

Unit Testing Ideals


Isolatable Repeatable Automatable Easy to Write

Why Unit Test?


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

Something about nUnit


NUnit is a unit-testing framework for all .Net languages. It has been written entirely in C#.

Important nUnit Links

http://www.nunit.org http://nunit.org/download.html http://sourceforge.net/projects/nunit/

The Input & Output of nUnit

Dll, exe file

nUnit

XML file (Optional)

Processing details on the GUI or Command prompt

The 2 operational modes

GUI mode Console mode (Project Central. net)

Things to do before testing

Download Nunit-2.6.2.msi from http://nunit.org/download.html and install nunit.

Things to do before testing cont.

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 the Solution explorer mode

Right click references and select Add reference

Select Browse

Goto the nunit\bin directory, choose nunit.framework.dll and press open.

Now press ok in the Add reference page

The dll has been added to your reference list.

In your code, add using nunit.framework.

Add testfixture to your program

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

How to write your test code

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; }

Calculator Example cont


[TestFixture] public class calcTest { [Test] public void AdditionTests(){ calculator cal=new calculator(); cal.Addition(5,10);Console.Write("TESTING 5+10\n"); Assert.AreEqual(15,cal.output());

cal.Addition(-5,10);Console.Write("TESTING -5+10\n"); Assert.AreEqual(5,cal.output());

[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());

} }//for testfixture }//for namespace

Build the Project

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.

Location of exe files

Starting the GUI from console


The GUI can be started from the console by executing nunit-gui.exe [inputfilename][options] Ex: nunit-gui.exe nunit.tests.dll run This option will load the dll file and run the test. * Options with their values are separated by an equal, colon or a space.

Starting the GUI from console


nunit-gui.exe [inputfilename][options] Options: /help short format: /? /config=STR project config to load /noload suppress loading of last proj /run Automatically run after load /fixture=STR Fixture to test

GUI MODE

The steps to be followed are


Load the Dll file. Select Run. View the errors if any on the GUI screen. Save the output XML if necessary.

Select open in the NUNIT GUI

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

To save the output in an XML file

Writing in the output file

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

The different options are


Nunit-console [inputfiles][options] Options
/Fixture=STR /config=STR /XML=STR /transform=STR /xmlConsole /output=STR /err=STR /labels /include = STR /exclude = STR /noshadow /thread /wait /nologo /help Fixture to test project configuration to load Name of XML output file name of transform file display XML to the console File to receive test output (short :/out=STR) File to receive test error output Label each test in stdout list of categories to include list of categories to exclude disable shadow option runs thread on a separate thread wait for input before closing console window Do not display the logo Display help (short format: /?)

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.

Now lets Examine the Output file (test passed)

Test Failed

Failure message(1 message)

Test Failure(2 tests)

Writing Unit Tests


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

S-ar putea să vă placă și