Archive

Archive for July, 2014

Unit testing using NUnit in Visual Studio 2010

July 25, 2014 8 comments

What is Unit testing?

Unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine if they are fit for use. Intuitively, one can view a unit as the smallest testable part of an application. Let me take the example of the ‘Calculator’  for demonstrating the unit testing.

SOFTWARE REQUIREMENTS:

  • Windows 7 as OS
  • Microsoft Visual Studio 2010 as IDE
  • NUnit as Unit Testing Tool

SET-UP INSTRUCTIONS:

  • Create a new Project with C# Class Library and save it (‘Calculator’ in my example).
  • Rename the class ‘Class1.cs‘ to ‘Operator.cs‘.
  • Add the NUnit frameworks into the ‘Solution Explorer‘ by context-clicking on the project name and then select ‘Add Reference’ menu item.
  • When the Add Reference dialog appears, click on ‘Browse‘ and navigate to C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\framework and select nunit.framework.dll.
  • Finally, the solution Explorer should look like this:

Create and writing scenarios for Unit tests:
  • Create scenarios for testing the operators such as Add, Subtract, Multiply, Divide, Square Root and Reciprocal.
  • Copy and paste the following code into the Operator.cs file in VS 2010.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace Calculator
{
 public class Operator
 {

 public static double Add(double a, double b)
 {
 return a + b;
 }

 public static double Subtract(double a, double b)
 {
 return a - b;
 }

 public static double Multiply(double a, double b)
 {
 return a * b;
 }

 public static double Divide(double a, double b)
 {
 return a / b;
 }

 public static double SquareRoot(double a)
 {
 return Math.Sqrt(a);
 }

 public static double Reciprocal(double a)
 {
 return 1 / a;
 }

 [TestFixture]
 public class TestClass
 {

 [Test]
 public void AdditionTest()
 {
 double result = Operator.Add(2, 4);
 Assert.AreEqual(6, result);
 }

 [Test]
 public void SubtractionTest()
 {
 double result = Operator.Subtract(1, 3);
 Assert.AreEqual(-2, result);
 }

 [Test]
 public void MultiplicationTest()
 {
 double result = Operator.Multiply(4, 3);
 Assert.AreEqual(12, result);
 }

 [Test]
 public void DivisionTest()
 {
 double result = Operator.Divide(3, 0);
 Assert.AreEqual(0, result);
 }

 [Test]
 public void SquareRootTest()
 {
 double result = Operator.SquareRoot(-4);
 Assert.AreEqual(2, result);
 }

 [Test]
 public void ReciprocalTest()
 {
 double result = Operator.Reciprocal(0);
 Assert.AreEqual(0, result);
 }
 }
 }
}

—————-

Running the tests using NUnit:
  • In Visual Studio, Go to ‘Projects -> Project Properties’ (‘Calculator Properties’ in my example).
  • Click on the ‘Debug’ tab.
  • Set the ‘Start external program‘ to the location of NUnit exe file (C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\nunit-x86.exe).
  • Build the solution, Go to ‘Build -> Build Solution‘ (hit the F6 key) in Visual Studio.
  • Execute the test, Go to ‘Debug -> Start Debugging’ (hit the F5 key) in Visual Studio. Visual Studio invokes the NUnit application.
  • In NUnit, click on ‘File -> Open Project‘ and choose the location of the Calculator.dll file as shown in the snapshot below.
  • In NUnit, click on the ‘Run‘ button to run the tests. Test is executed using NUnit and the result will be displayed in NUnit GUI window as shown in the snapshot below.

Only 3 tests are passed out of 6. Lets look into each of the failed tests – DivisionTest, ReciprocalTest and SquareRootTest.

Firstly, let us try with DivisionTest. When we divide 3/0, the calculator must display “Cannot divide by zero”. So, let us write an Exception error for Divide method.

public static double Divide(double a, double b)
{
 if (b == 0)
  {
  throw new InvalidOperationException("Cannot divide by zero");
  }
  return a / b;
}

Secondly, let us try with ReciprocalTest. When we reciprocate 0, the calculator must display “Cannot divide by zero”. So, let us write an Exception error for Reciprocal method.

public static double Reciprocal(double a)
{
if (a == 0)
{
throw new InvalidOperationException("Cannot divide by zero");
}
return 1 / a;
}

Thirdly, let us try with SquareRootTest. When we perform the square root operation for -4, the calculator must display “Invalid Input”. So, let us write an Exception error for SquareRoot method.

public static double SquareRoot(double a)
{
if (a < 0)
{
throw new InvalidOperationException("Invalid Input");
}
return Math.Sqrt(a);
}

Now, add valid numbers to each of the failed tests, build the code and run the test using NUnit. Final contents for the ‘Operator.cs‘ is as shown below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace Calculator
{
 public class Operator
 {

 public static double Add(double a, double b)
 {
 return a + b;
 }

 public static double Subtract(double a, double b)
 {
 return a - b;
 }

 public static double Multiply(double a, double b)
 {
 return a * b;
 }

 public static double Divide(double a, double b)
 {
 if (b == 0)
   {
   throw new InvalidOperationException("Cannot divide by zero");
   }
   return a / b;
 }

 public static double SquareRoot(double a)
 {
 if (a < 0)
   {
   throw new InvalidOperationException("Invalid Input");
   }
   return Math.Sqrt(a);
 }

 public static double Reciprocal(double a)
 {
 if (a == 0)
   {
   throw new InvalidOperationException("Cannot divide by zero");
   }
   return 1 / a;
 }

 [TestFixture]
 public class TestClass
 {

 [Test]
 public void AdditionTest()
 {
 double result = Operator.Add(2, 4);
 Assert.AreEqual(6, result);
 }

 [Test]
 public void SubtractionTest()
 {
 double result = Operator.Subtract(1, 3);
 Assert.AreEqual(-2, result);
 }

 [Test]
 public void MultiplicationTest()
 {
 double result = Operator.Multiply(4, 3);
 Assert.AreEqual(12, result);
 }

 [Test]
 public void DivisionTest()
 {
 double result = Operator.Divide(4, 2);
 Assert.AreEqual(2, result);
 }

 [Test]
 public void SquareRootTest()
 {
 double result = Operator.SquareRoot(4);
 Assert.AreEqual(2, result);
 }

 [Test]
 public void ReciprocalTest()
 {
 double result = Operator.Reciprocal(2);
 Assert.AreEqual(0.5, result);
 }
 }
 }
}

Advertisements

Setup Jenkins on Windows and integrating it with NAnt

WHAT IS CONTINUOUS INTEGRATION?

Continuous integration (CI) implements continuous processes of applying quality control — small pieces of effort, applied frequently. Continuous integration aims to improve the quality of software, and to reduce the time taken to deliver it, by replacing the traditional practice of applying quality control after completing all development. I was interested in integrating the NAnt build tool with the Continuous Integration Tool and I choose Jenkins, which is an open source continuous integration tool written in Java.

PRINCIPLES OF CONTINUOUS INTEGRATION (Reference from Wikipedia):

  • Maintain a code repository: This practice advocates the use of a revision control system for the project’s source code. All artifacts required to build the project should be placed in the repository. In this practice and in the revision control community, the convention is that the system should be buildable from a fresh checkout and not require additional dependencies.
  • Automate the build: A single command should have the capability of building the system. Many build-tools, such as make, have existed for many years. Other more recent tools like Ant, Maven, MSBuild, OpenMake Meister or IBM Rational Build Forge are frequently used in continuous integration environments. Automation of the build should include automating the integration, which often includes deployment into a production-like environment.
  • Make the build self-testing: Once the code is built, all tests should run to confirm that it behaves as the developers expect it to behave.
  • Everyone commits to the baseline every day: By committing regularly, every committer can reduce the number of conflicting changes. Checking in a week’s worth of work runs the risk of conflicting with other features and can be very difficult to resolve. Early, small conflicts in an area of the system cause team members to communicate about the change they are making. Committing all changes at least once a day (once per feature built) is generally considered part of the definition of Continuous Integration. In addition performing a nightly build is generally recommended.
  • Every commit (to baseline) should be built: The system should build commits to the current working version in order to verify that they integrate correctly. A common practice is to use Automated Continuous Integration, although this may be done manually. For many, continuous integration is synonymous with using Automated Continuous Integration where a continuous integration server or daemon monitors the version control system for changes, then automatically runs the build process.
  • Keep the build fast: The build needs to complete rapidly, so that if there is a problem with integration, it is quickly identified.
  • Test in a clone of the production environment: Having a test environment can lead to failures in tested systems when they deploy in the production environment, because the production environment may differ from the test environment in a significant way. However, building a replica of a production environment is cost prohibitive. Instead, the pre-production environment should be built to be a scalable version of the actual production environment to both alleviate costs while maintaining technology stack composition and nuances.
  • Make it easy to get the latest deliverables: Making builds readily available to stakeholders and testers can reduce the amount of rework necessary when rebuilding a feature that doesn’t meet requirements. Additionally, early testing reduces the chances that defects survive until deployment. Finding errors earlier also, in some cases, reduces the amount of work necessary to resolve them.
  • Everyone can see the results of the latest build: It should be easy to find out whether the build breaks and, if so, who made the relevant change.
  • Automate deployment: Most CI systems allow the running of scripts after a build finishes. In most situations, it is possible to write a script to deploy the application to a live test server that everyone can look at. A further advance in this way of thinking is Continuous Deployment, which calls for the software to be deployed directly into production, often with additional automation to prevent defects or regressions.

SOFTWARE REQUIREMENTS:

  • Windows 7 as OS
  • Microsoft Visual Studio 2010 as IDE
  • NAnt as Build tool
  • NUnit as Unit Testing Tool
  • Selenium as a Test Automation Tool
  • Jenkins as Continuous Integration Tool
  • Java Installed (v1.7)
Java version Check:
  • Context-click ‘Computer‘, select ‘Properties‘ option.
  • Add the Java path in the ‘Path‘ System variables under ‘Advanced system settings ->Environment Variables‘.
  • Open the Command Prompt and type java -version and hit the ‘Enter’ key. Command Prompt will display following info:
 java version “1.7.0_05”
Java <TM> SE Runtime Environment <build 1.7.0_05-b05>
Java HotSpot<TM> Client VM <build 23.1-b03, mixed mode, sharing>
Jenkins Installation:
  • Download the Jenkins installer (jenkins.war) from the location: http://jenkins-ci.org/
  • Place the jenkins.war file in the location: C:\Development\Tools\Jenkins
  • Context-click ‘Computer‘, select ‘Properties‘ option. Then, Go to ‘Advanced system settings ->Environment Variables‘.
  • Add a new System variable by Variable name ‘JENKINS_HOME’ and set the Variable value to ‘C:\Development\Tools\Jenkins\.jenkins’
  • Open the Command Prompt and navigate to the location: C:\Development\Tools\Jenkins
  • Type java -jar jenkins.war and hit the ‘Enter‘ key. Jenkins is fully up and running. I have used the Environment Variable because when I run the command again, Jenkins will look for this Environment Variable and will automatically know to run.
  • All the Jenkins files will be extracted within the folder ‘.jenkins’ and will be saved in the location: ‘C:\Development\Tools\Jenkins\’ as shown below:
  • Launch a browser. Open the URL: http://localhost:8080/ and hit the ‘Enter‘ key.
  • Browser displays the Jenkins Home page as shown below:
Configuring NAnt with Jenkins:
  • Open the URL: http://localhost:8080/
  • Click on the ‘Manage Jenkins‘ link in the LHS of Jenkins Dashboard. It puts up the ‘Manage Jenkins‘ page as shown below:
  • Click on the ‘Manage Plugins‘ link. It puts up the ‘Plugin Manager‘ page.
  • Click on the ‘Available‘ tab, check the ‘NAnt Plugin‘ option under the ‘Build Tools‘ group and hit the ‘Download now and install after restart‘ button.
  • Restart the system and the NAnt plugin is installed.
  • Open the URL: http://localhost:8080/
  • Go to ‘Manage Jenkins’ -> ‘Manage Plugins’ -> ‘Installed‘ tab. NAnt plugin is listed in the ‘Installed‘ list as shown below:
  • Now, go to the Jenkins Dashboard screen and click on the ‘New Job‘ in the LHS links.
  • Add a job name (‘SampleSolution.Build‘ in my example), set the ‘Build a free-style software project checkbox and click on the ‘OK‘ button.
  • Under the ‘Build‘ section, click on the ‘Add build step‘ dropdown menu and select ‘Execute NAnt build‘.
  • I will be referencing my previous post, where I have integrated the NAnt Build with my Selenium Automation script. Add the location of NAnt: C:\Development\Work\SampleSolution\src\SampleSolution.Build\Nant.build in the ‘NAnt Build File’ field.
  • Click on the ‘Apply‘ button and then on the ‘Save’ button.
  • Go to the Jenkins Dashboard screen and you can see a new job added to the task list as shown below:
 
Running the Build task:
  • Hover the mouse on the new build task (i.e., SampleSolution.Build‘ in my case) and click on the ‘Build Now‘ option.
  • Build is executed, tests are executed in GUI mode and the build execution result will be displayed on the Jenkins Dashboard.

%d bloggers like this: