"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;
Showing posts with label Nunit Tests. Show all posts
Showing posts with label Nunit Tests. Show all posts

June 22, 2011

Stored Procedure Testing using Nunit

In this post we will look at stored procedure testing using Nunit. We will be
  • Reusing stored procedure created for SSRS report link
  • Create Table Earnings and procedure SaleCity
  • Earlier we looked into TST codeplex tool
This example is In continuation with previous Nunit post. Stored Proc test approach I learnt from book Expert SQL Server 2008 Development.
Step 1 - Created VS 2008 Class Library
Step 2 - Added Reference to  DLL nunit.framework.dll in the project
Step 3 - Copy below code in the Class1.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using NUnit.Framework;
namespace ClassLibrary1
{
    [TestFixture]
    public class Class1
    {
        [Test]
        public void TestSaleCity()
        {

            // Set up a command object
            SqlCommand comm = new SqlCommand();

            // Set up the connection
            comm.Connection = new SqlConnection(
            @"server=.\SQLExpress; trusted_connection=true; database=AppDB");

            // Specify Procedure Name
            comm.CommandText = "dbo.SaleCity";
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.AddWithValue("@City", "Chennai");

            // Create a DataSet for storing the results
            DataSet ds = new DataSet();

            // Define a DataAdapter to fill a DataSet
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = comm;
            try
            {
                // Fill the dataset
                adapter.Fill(ds);

            }
            catch
            {
                Assert.Fail("Exception occurred!");
            }

            // Validate Actual Results
            Assert.IsTrue(ds.Tables.Count == 1,
            "Result set count != 1");
            DataTable dt = ds.Tables[0];

            // There must be exactly four columns returned
            Assert.IsTrue(
            dt.Columns.Count == 4,
            "Column count != 4");

            // There must be columns City, SaleValue, SaleDate and Name
            Assert.IsTrue(
            dt.Columns.IndexOf("City") > -1,
            "Column City does not exist");

            Assert.IsTrue(
            dt.Columns.IndexOf("SaleValue") > -1,
            "Column SaleValue does not exist");
           
            Assert.IsTrue(
            dt.Columns.IndexOf("SaleDate") > -1,
            "Column SaleDate does not exist");
           
            Assert.IsTrue(
            dt.Columns.IndexOf("Name") > -1,
            "Column Name does not exist");

            // There must be more than one row returned
            Assert.IsTrue(
            dt.Rows.Count >= 1,
            "Result rows returned");
        }

    }
}
Step 4 - Build the Project
Step 5 - Using Nunit Open the Debug folder, You will see ClassLibrary1 DLL. Below is the snapshot of results
More Reads
Happy Reading!! 

May 21, 2011

.NET API Testing - Part II - Nunit Tests

In continuation with previous post we are now going to write Nunit Tests for earlier post.
1. Downloaded Nunit from link
2. Created a Test Project in VS 2008
3. Add Reference to DLL nunit.framework.dll in the project
4. Copy below modifed code in UnitTest1.cs

public class TestDesign
{
    public int SumofTwoNumbers(int InputA, int InputB)
    {
        return (InputA + InputB);
    }
}

namespace APITest
{
    using NUnit.Framework;

    [TestFixture]
    public class TestExample
    {
        [Test]
        //Test Method
        public void TestAddAPI()
        {
            TestDesign TD = new TestDesign();
            Assert.AreEqual(30,TD.SumofTwoNumbers(10, 20));
            Assert.AreEqual(33, TD.SumofTwoNumbers(10, 23));
        }

        //Data Driven Testing
        [TestCase(10, 10, Result=20)]
        [TestCase(15, 15, Result=30)]
        [TestCase(20, 20, Result=40)]
        public int TestAddAPI(int a, int b)
        {
            TestDesign TD = new TestDesign();
            return TD.SumofTwoNumbers(a, b);
        }
    }
}
5. Build the Project
6. Using Nunit Open the Debug folder, You will see Test Project DLL. Below is the snapshot of results

More Reads - Nunit Quick Start
A Test Harness with WatiN, TestDriven.NET, NUnit with Visual Studio 2008 (Team System)
Getting started with NUnit and BizUnit
Automated Testing with Selenium 2 and NUnit


Happy Reading!!