Quantcast
Channel: Biz IT Solution » Methodologies
Viewing all articles
Browse latest Browse all 8

Unit Testing

$
0
0

unit testing


What is Unit Testing?
  • A ‘Unit’ is defined as being the smallest testable component of an application. e.g. function level testing.
  • In Systems where Object Oriented Designs are used, the Class is the smallest testable component.
  • Unit tests provide a strict set of test inputs into a function being test and returns an expected result.

 

Test case
A Test Case can be defined as an input and output contract, that a particular function being tested must adhere to. For example:

Dim tc as new TalkingClass
Dim speech as String
speech = tc.Say(“Hello”)

If tc.Say(“Hello”) returns “Hello” then, no matter what changes have occurred internally as a result of code optimization or bug fixes, Say(“Hello”) must always return “Hello”.

 

Why should unit test be done?

  • Unit testing promotes refactoring of code.
  • Unit tests accurately define what conditions and functionality a piece of code or application must produce irrelevant of any changes that have occurred.
  • Unit testing promotes test driven development (TDD).
  • Facilitates Change

 
Best Practices for Unit Testing

  • Keep each unit test as fine grained as possible. By breaking down large unit test into the smallest pieces of functionality in a particular project / system, it makes it much simpler to track down bugs and issues.
  • Ensure Unit tests test the functional requirements for the particular function / class.
  • Refactor code when ever you feel it can be improved.
  • Use meaningful name for test methods
  • Use test fixtures to group related unit tests.
  • Write unit tests before or during the implementation of code. This make your tests a lot more successful as the functional requirements will be fresh in your mind.

 

Code Driven Testing

Code driven testing is a general approach to test automation.  This approach uses a testing framework tool that usually interface to classes, modules, or libraries which then allows  the code to conduct unit tests  with a variety of input arguments to determine whether various sections of the code are acting as expected under various circumstances. One such tool used by the majority of Developers is NUnit.

 

How to use NUnit?
Overview

  • NUnit is a unit-testing framework for all languages using the .Net framework.
  • NUnit makes it easy to create and organise tests. Also provides an easy way view test results and / or details of failures.
  • Supports .Net 1.1 and .Net 2.0
  • Gives the coder a rough estimate how fast their code is, as NUnit returns the time it took to execute a particular piece of code. Most relevant to larger and more complex functions.
  • Go to http://www.nunit.org/ and download the latest version of NUnit (*.msi).

 

Setting up NUnit 

  • Run and install NUnit setup file and follow the instruction on the pop up window screen.
  • Open up an existing project or solution file in Visual Studio 2005/2008
  • Add a new project and select a class library template. This is where your new Unit Testing will reside
  • Add the nunit.framework reference to your unit test project
  • Open the new unit test class code (*.cs or *.vb file) and begin writing your test case for each function/method. See below
  • Compile your code to generate the dll
  • Go to start->All Programs->NUnit vx.x.x and click on NUnit GUI icon
  • Go to File-> open project and select the dll for the unit testing project
  • Click the run button to run the test and see the results

 using NUnit.Framework; 

namespace  UnitTest{

 [TestFixture] 

public class  nUnitTest {

[Test] 
  public void Testxxx(){ 

    objectname objvar = new objectname(); //Insert the correct object name 

   int intvar = objvar.getDatePart(“30/12/2009″, 1, ‘/’); //Write the test case for each function that returns the actual value 

   Assert.AreEqual(12, intvar); //Test the function by comparing its output result with the expected result (i.e. 12)

}

}

}

 

You can download the sample code (VS 2008) here. Open the UnitTest.sln solution file to start viewing the code.

 


 Please email itinfotechnology@gmail.com to Biz IT Solution who will response to your email ASAP, if you have further questions about this topic. Otherwise leave your comments below by clicking the “Submit Comment” button.

Viewing all articles
Browse latest Browse all 8

Trending Articles