Skip to content

DataDriven tests from Xml files

Jakub Raczek edited this page May 8, 2016 · 18 revisions

2. NUnit test framework

Data driven tests from Xml files are not supported with NUnit by default, but you can use DataDrivenHelper class methods from our Test.Automation framework to read xml files and pass test data to your tests.

2.1 Just add xml file with test data to your NUnit project e.g:

<?xml version="1.0" encoding="utf-8"?>
<tests>
  <credential user="tomsmith" password="SuperSecretPassword!" message="You logged into a secure area!"/>
  <credential user="wronguser" password="wrongpassword" message="Your username is invalid!"/>
  <links number="3"/>
</tests>

and set proper path (relative) to that file in App.config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <appSettings>
 <add key="DataDrivenFile" value="\DataDriven\DataDriven.xml" />
 </appSettings>
</configuration>

2.2 Create a class for relation between data driven tests and xml file with test data e.g:

namespace Objectivity.Test.Automation.Tests.NUnit.DataDriven
{
    using System.Collections;

    /// <summary>
    /// DataDriven methods for NUnit test framework
    /// </summary>
    public static class TestData 
    {

        public static IEnumerable Credentials
        {
            get { return DataDrivenHelper.ReadDataDriveFile(ProjectBaseConfiguration.DataDrivenFile, "credential", new[] { "user", "password" }, "credential"); }
        }

        public static IEnumerable LinksSetTestName
        {
            get { return DataDrivenHelper.ReadDataDriveFile(ProjectBaseConfiguration.DataDrivenFile, "links", new[] { "number" }, "Count_links"); }
        }
    }
}

Where calling the method ReadDataDriveFile()

DataDrivenHelper.ReadDataDriveFile(ProjectBaseConfiguration.DataDrivenFile, "links", new[] { "number" }, "Count_links");

from its definitions:

public static IEnumerable<TestCaseData> DataDrivenHelper.ReadDataDriveFile(string folder, string testData, string[] diffParam, [Optional] string testName);

folder - Full path to XML DataDriveFile file

testData - Name of the child element in xml file

diffParam - Values of listed parameters will be used in test case name

testName - Name of the test, use as prefix for test case name

Definition of class DataDrivenHelper can be find here

2.3 Set tests to use test data from xml file e.g:

namespace Objectivity.Test.Automation.Tests.NUnit.Tests
{
    using System.Collections.Generic;

    using global::NUnit.Framework;

    using Objectivity.Test.Automation.Common;
    using Objectivity.Test.Automation.Tests.NUnit.DataDriven;
    using Objectivity.Test.Automation.Tests.PageObjects.PageObjects.TheInternet;

    [TestFixture]
    [Parallelizable(ParallelScope.Fixtures)]
    public class HerokuappTestsNUnit : ProjectTestBase
    {
        [Test]
        [TestCaseSource(typeof(TestData), "Credentials")]
        public void FormAuthenticationPageTest(IDictionary<string, string> parameters)
        {
            new InternetPage(this.DriverContext).OpenHomePage().GoToFormAuthenticationPage();

            var formFormAuthentication = new FormAuthenticationPage(this.DriverContext);
            formFormAuthentication.EnterUserName(parameters["user"]);
            formFormAuthentication.EnterPassword(parameters["password"]);
            formFormAuthentication.LogOn();
            Verify.That(
                this.DriverContext,
                () => Assert.AreEqual(parameters["message"], formFormAuthentication.GetMessage));

        }

        [Test]
        [TestCaseSource(typeof(TestData), "LinksSetTestName")]
        public void CountLinksAtShiftingContentTest(IDictionary<string, string> parameters)
        {
            new InternetPage(this.DriverContext).OpenHomePage().GoToShiftingContentPage();

            var links = new ShiftingContentPage(this.DriverContext);
            Verify.That(this.DriverContext, () => Assert.AreEqual(parameters["number"], links.CountLinks()));
        }
    }
}

Where

[TestCaseSource(typeof(TestData), "Credentials")]

TestData is a a class for relation between data driven tests and xml file from 2.2

"Credentials" is a method from TestData class.

Names of test are set dynamically by the values of test data read from xml file e.g:

2.4 If you want to execute any of your test repeatedly with different test data, just repeat as many time as you need corresponding child element in xml file

<credential user="tomsmith" password="SuperSecretPassword!" message="You logged into a secure area!"/>
<credential user="wronguser" password="wrongpassword" message="Your username is invalid!"/>

2.5 If you don't need to set test case name use overridden method

 public static IEnumerable Links
        {
            get { return DataDrivenHelper.ReadDataDriveFile(ProjectBaseConfiguration.DataDrivenFile, "links"); }
        }

from its definitions:

public static IEnumerable<TestCaseData> DataDrivenHelper.ReadDataDriveFile(string folder, string testData);

folder - Full path to XML DataDriveFile file

testData - Name of the child element in xml file

and set test use that method "Links"

[Test]
[TestCaseSource(typeof(TestData), "Links")]
public void CountLinksAtShiftingContentTest(IDictionary<string, string> parameters)
 {
      new InternetPage(this.DriverContext).OpenHomePage().GoToShiftingContentPage();

      var links = new ShiftingContentPage(this.DriverContext);
      Verify.That(this.DriverContext, () => Assert.AreEqual(parameters["number"], links.CountLinks()));
 }

Names of test remain unchanged e.g:

2.6 Examples of implementation can be find here:

2. MSTest test framework

Data driven tests from Xml files are supported with MSTest by default.

2.1 Just add xml file with test data to your MSTest project e.g:

<?xml version="1.0" encoding="utf-8" ?>
<Rows>
  <Links>
    <number>5</number>
  </Links>
  <credential>
    <user>tomsmith</user>
    <password>SuperSecretPassword!</password>
    <message>You logged into a secure area!</message>
  </credential>
  <credential>
    <user>wronguser</user>
    <password>wrongpassword</password>
    <message>Your username is invalid!</message>
  </credential>
</Rows>

2.2 Set tests to use that file e.g:

namespace Objectivity.Test.Automation.Tests.MsTest.Tests
{
    using System;
    using System.Diagnostics.CodeAnalysis;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using Objectivity.Test.Automation.Common;
    using Objectivity.Test.Automation.Common.Extensions;
    using Objectivity.Test.Automation.Tests.PageObjects.PageObjects.TheInternet;

    /// <summary>
    /// Tests to verify checkboxes tick and Untick.
    /// </summary>
    [TestClass]
    public class HerokuappTestsMsTest : ProjectTestBase
    {
       [DeploymentItem("Objectivity.Test.Automation.MsTests\\DDT.xml"),
        DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
        "|DataDirectory|\\DDT.xml", "credential",
         DataAccessMethod.Sequential), TestMethod]
        public void FormAuthenticationPageTest()
        {
            new InternetPage(this.DriverContext).OpenHomePage().GoToFormAuthenticationPage();

            var formFormAuthentication = new FormAuthenticationPage(this.DriverContext);
            formFormAuthentication.EnterUserName((string)this.TestContext.DataRow["user"]);
            formFormAuthentication.EnterPassword((string)this.TestContext.DataRow["password"]);
            formFormAuthentication.LogOn();
            Assert.AreEqual((string)this.TestContext.DataRow["message"], formFormAuthentication.GetMessage);
        }
    }
}
Clone this wiki locally