Skip to content

MsTest DataDriven tests from Xml and CSV files

Jakub Raczek edited this page Apr 25, 2019 · 4 revisions

1. MSTest test framework

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

1.1 DataDrive from XML file

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

1.1.2 Set tests to use that file e.g:

namespace Ocaramba.Tests.MsTest.Tests
{
    using System;
    using System.Diagnostics.CodeAnalysis;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using Ocaramba;
    using Ocaramba.Extensions;
    using Ocaramba.Tests.PageObjects.PageObjects.TheInternet;

    [TestClass]
    public class HerokuappTestsMsTest : ProjectTestBase
    {
       [DeploymentItem("Ocaramba.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);
        }
    }
}

1.2 DataDrive from Csv file

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

1.2.2 Set tests to use that file e.g:

        [DeploymentItem("Ocaramba.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));
        }

1.3 Examples of implementation can be found here:

Clone this wiki locally
You can’t perform that action at this time.