Creating a data-driven test in NUnit

The NUnit framework has been widely used by the Selenium WebDriver community to create test scripts with .NET bindings.

Similar to the JUnit framework, the NUnit framework also supports data-driven testing in the simplest manner. In this recipe, we will create a Selenium WebDriver test using NUnit. We will read the test data from an XML file used in the first recipe.

Getting ready

To begin, follow these steps:

  1. Download and install NUnit from http://www.nunit.org/
  2. Create the test data file in the XML format as follows:
      <testdata>
      <vars height="160" weight="45" bmi="17.6" bmi_category="Underweight" />
      <vars height="168" weight="70" bmi="24.8" bmi_category="Normal" />
      <vars height="181" weight="89" bmi="27.2" bmi_category="Overweight" />
      <vars height="178" weight="100" bmi="31.6" bmi_category="Obesity" />
    </testdata>
  3. Create a new C# class library project and name it BMICalculator
  4. Add a reference to NUnit, WebDriver, .NET binding, System XML, and System.Xml.Linq

How to do it...

Let's create a parameterized test in NUnit. Create a new C# class item with the name BMICalculatorNUnitTest, as shown in the following code example, copy the following code to the newly created class by replacing its contents:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Collections;
using System.Xml.Linq;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Support.UI;

namespace BMICalculator
{
    [TestFixture]
    public class BMICalculatorNUnitTest
    {
        IWebDriver driver;

        [SetUp]
        public void TestSetup()
        {
            // Create a instance of the Firefox driver using IWebDriver Interface
            driver = new FirefoxDriver();
        }

        [TestCaseSource("BmiTestData")]
        public void TestBmiCalculator(string height, string weight, string expectedBmi, string expectedCategory)
        {
            driver.Navigate().GoToUrl("http://cookbook.seleniumacademy.com/bmicalculator.html");

            IWebElement heightElement = driver.FindElement(By.Name("heightCMS"));
            heightElement.SendKeys(height);

            IWebElement weightElement = driver.FindElement(By.Name("weightKg"));
            weightElement.SendKeys(weight);

            IWebElement calculateButton = driver.FindElement(By.Id("Calculate"));
            calculateButton.Click();

            IWebElement bmiElement = driver.FindElement(By.Name("bmi"));
            Assert.AreEqual(expectedBmi, bmiElement.GetAttribute("value"));

            IWebElement bmiCatElement = driver.FindElement(By.Name("bmi_category"));
            Assert.AreEqual(expectedCategory, bmiCatElement.GetAttribute("value"));

        }

        [TearDown]
        public void TestCleanUp()
        {
            // Close the browser
            driver.Quit();
        }

        private IEnumerable BmiTestData
        {
            get { return GetBmiTestData(); }
        }
        private IEnumerable GetBmiTestData()
        {
            var doc = XDocument.Load(@"c:data.xml");
            return
                from vars in doc.Descendants("vars")
                let height = vars.Attribute("height").Value
                let weight = vars.Attribute("weight").Value
                let expectedBmi = vars.Attribute("bmi").Value
                let expectedCategory = vars.Attribute("bmi_category").Value

                select new object[] { height, weight, expectedBmi, expectedCategory };
        }
    }
}

How it works...

While creating a data-driven test in NUnit, we use the TestCaseSource attribute. We will specify the name of the IEnumerable property that will provide test data to this test case with the TestCaseSource attribute, as shown in the following code:

[TestCaseSource("BmiTestData")]
public void TestBmiCalculator(string height, string weight, string expected_bmi, string expected_category)

When we execute the test, NUnit framework will generate test cases by calling the BmiTestData property. This will return an array of arguments as IEnumerable by calling the GetBmiTestData() method. An array of arguments is created by reading an XML file using a LINQ Query in the GetBmiTestData() method. When we open the test in the NUnit GUI, it shows the test cases for all the test data combinations provided in the input XML file, as shown in the following screenshot:

How it works...

With this, we can get the test data from any source, such as CSV, Excel spreadsheet, or a database. The NUnit GUI will show the results when all the tests are executed, as shown in the following screenshot:

How it works...

See also

  • The Creating a data-driven test in MSTEST recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.36.221