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

May 15, 2011

.NET API Testing

Installed .NET 4.0 and C# VS 2010 Express Edition. Tried the below example for API testing.
  • Have not used any design patterns in this case#
  • Simple API Testing
  • Methods for Reading Input
  • Executing Results
  • Reporting Results (Create File with time stamp)
Disadvantages of not using a Framework ex-TestNG / Nunit
  • Trying to write methods for everything (Input parsing, logging, reporting, Running Tests)
Anyways this is a good learning exercise for me

Below is input test data XML format
  <?xml version="1.0" encoding="utf-8" ?>
     - <TestData>
- <    TestCase>
      <Input1>1</Input1>
      <Input2>2</Input2>
      <Expected>2</Expected>
  </TestCase>
   - <TestCase>
     <Input1>10</Input1>
     <Input2>2</Input2>
     <Expected>20</Expected>
  </TestCase>
   - <TestCase>
     <Input1>3</Input1>
     <Input2>2</Input2>
     <Expected>6</Expected>
  </TestCase>
  </TestData>

Below is example code I tried.

using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
namespace TestSuite
{
    class TestDesign
    {
        //Read input for Test cases from the XML
        public void AssignTestData()
        {
            int TestData1=0;
            int TestData2=0;
            int ExpectedResult=0;
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("D:\\TestData.xml");
                XmlNodeList TestDataList = doc.GetElementsByTagName("TestCase");
                foreach (XmlNode node in TestDataList)
                {
                 XmlElement TestData = (XmlElement) node;
                 TestData1 = Convert.ToInt32(TestData.GetElementsByTagName("Input1")[0].InnerText);
                 TestData2 = Convert.ToInt32(TestData.GetElementsByTagName("Input2")[0].InnerText);
                 ExpectedResult = Convert.ToInt32(TestData.GetElementsByTagName("Expected")[0].InnerText);
                 Console.WriteLine("Test Data is " + TestData1 + "," + TestData2 + " Expected is " + ExpectedResult + "\n");
                 //Run the tests after getting input test data
                 RunTests(TestData1, TestData2, ExpectedResult);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:" +"\n");
                Console.WriteLine(e.Message);
                Console.ReadKey();
            }
        }

        public void RunTests(int TestInput1, int TestInput2, int Expected)
        {
            try
            {
                int ActualResult;
                ActualResult = SumofTwoNumbers(TestInput1, TestInput2);
                if (ActualResult == Expected)
                {
                    Console.WriteLine("Test Case Successful for Data: " + TestInput2 + "," + TestInput2
                         + " Expected Result " + Expected +"\n");
                }
                else
                {
                    Console.WriteLine("Test Case Failed for Data: " + TestInput2 + "," + TestInput2
     + " Expected Result" + Expected +" Actual Result " + ActualResult +"\n");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadKey();
            }
        }
       
        public void ReportResults()
        {
            //Email Results
        }
       
        public int SumofTwoNumbers(int InputA, int InputB)
        {
            return (InputA * InputB);

        }
    }
    class Program
    {
        public static void Main()
        {
            TestDesign TestRun1 = new TestDesign();
            TestRun1.AssignTestData();
            Console.ReadKey();
//          TestRun1.ReportResults();
        }
    }
}

Results reporting function is not implemented, so it is commented code :). I would try the same example using Nunit in coming days.

More Reads
C# XmlReader Program
Top 10 Mistakes in Unit Testing
Reading XML Files Directly - Page 1
NUnit "Quick and Dirty" Tutorial
Unit Testing with NUnit 
NUnit Best Practices 
Nunit Blogs
Read / Write XML in Memory Stream
API Testing framework
Happy Reading!!

No comments: