Skip to content

DataDriven tests from Xml files

Jakub Raczek edited this page Jan 18, 2018 · 18 revisions

1. 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.

1.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>

Make sure that file properties "Copy to Output Directory" is set to "Copy always" and proper path (relative) is set 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>

1.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 found here

1.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:

1.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!"/>

1.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:

1.6 Examples of implementation can be found here:

2. MSTest test framework

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

2.1 DataDrive from XML file

2.1.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.1.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);
        }
    }
}

2.2 DataDrive from Csv file

2.2.1 Just add csv file (ANSI coding) e.g. DDT.csv with test data to your MSTest project e.g:

user, password, message
tomsmith,SuperSecretPassword!,You logged into a secure area!
wronguser,wrongpassword,Your username is invalid!

2.2.2 Set tests to use that file e.g:

        [DeploymentItem("Objectivity.Test.Automation.MsTests\\DDT.csv")]
        [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\DDT.csv", "DDT#csv", DataAccessMethod.Sequential)]
        [TestMethod]
        public void FormAuthenticationPageCsvDataDrivenTest()
        {
            var formFormAuthentication = new InternetPage(this.DriverContext)
                .OpenHomePage()
                .GoToFormAuthenticationPage();

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