-
Notifications
You must be signed in to change notification settings - Fork 131
NUnit DataDriven tests from Xml, CSV and Excel files
Data driven tests from Xml files are not supported with NUnit by default, but you can use DataDrivenHelper class methods from our Ocaramba framework to read xml files and pass test data to your tests.
<?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>
namespace Ocaramba.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
namespace Ocaramba.Tests.NUnit.Tests
{
using System.Collections.Generic;
using global::NUnit.Framework;
using Ocaramba;
using Ocaramba.Tests.NUnit.DataDriven;
using Ocaramba.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 1.1.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:
In The example the name “FormAuthenticationPageExcelTest” is an original name of the test. That name is change according to test data in Excel file to the “credentialExcel_user_password”
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!"/>
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:
Data driven tests from Excel files are not supported with NUnit by default, but you can use DataDrivenHelper class methods from our Ocaramba framework to read Excel xlsx files and pass test data to your tests. You need to install first NPOI. Test data is passed to the test as parameters (key - name of parameter and key value). You can store test data for several tests at separate Excel sheets. Data in first row at each sheet is used for name of parameters - keys.
1.2.1 Just add Excel xlsx file with test data to your NUnit project e.g
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="DataDrivenFileXlsx" value="\DataDriven\DataDriven.xlsx"/>
...
</appSettings>
</configuration>
namespace Ocaramba.Tests.NUnit.DataDriven
{
using System.Collections;
/// <summary>
/// DataDriven methods for NUnit test framework
/// </summary>
public static class TestData
{
public static IEnumerable CredentialsExcel
{
get { return DataDrivenHelper.ReadXlsxDataDriveFile(ProjectBaseConfiguration.DataDrivenFileXlsx, "credential", new[] { "user", "password" }, "credentialExcel"); }
}
public static IEnumerable LinksExcel
{
get { return DataDrivenHelper.ReadXlsxDataDriveFile(ProjectBaseConfiguration.DataDrivenFileXlsx, "links"); }
}
}
}
Where calling the method ReadXlsxDataDriveFile()
DataDrivenHelper.ReadXlsxDataDriveFile(ProjectBaseConfiguration.DataDrivenFileXlsx, "credential", new[] { "user", "password" }, "credentialExcel");
from its definitions:
public static IEnumerable<TestCaseData> ReadXlsxDataDriveFile(string path, string sheetName, [Optional] string[] diffParam, [Optional] string testName)
path - Full path to Excel DataDriveFile file
sheetName - Name of the sheet at xlsx file.
diffParam - Optional values of listed parameters will be used in test case name.
testName - Optional name of the test, use as prefix for test case name.
Definition of class DataDrivenHelper can be found here
namespace Ocaramba.Tests.NUnit.Tests
{
using System.Collections.Generic;
using global::NUnit.Framework;
using Ocaramba;
using Ocaramba.Tests.NUnit.DataDriven;
using Ocaramba.Tests.PageObjects.PageObjects.TheInternet;
[TestFixture]
[Parallelizable(ParallelScope.Fixtures)]
public class HerokuappTestsNUnit : ProjectTestBase
{
[Test]
[TestCaseSource(typeof(TestData), "CredentialsExcel")]
public void FormAuthenticationPageExcelTest(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), "LinksExcel")]
public void CountLinksAndSetTestNameAtShiftingContentExcelTest(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), "CredentialsExcel")]
TestData is a a class for relation between data driven tests and xlsx file from 1.2.2
"CredentialsExcel" is a method from TestData class.
Names of test are set dynamically by the values of test data read from xlsx file e.g:
In the example the name “FormAuthenticationPageExcelTest” is an original name of the test. That name is change according to test data in Excel file to the “credentialExcel_user_password” Only parameters set in diffParam will be used in test case name.
as many time as you need corresponding row in xlsx file
public static IEnumerable LinksExcel
{
get { return DataDrivenHelper.ReadXlsxDataDriveFile(ProjectBaseConfiguration.DataDrivenFileXlsx, "links"); }
}
and set test use that method "LinksExcel"
[Test]
[TestCaseSource(typeof(TestData), "LinksExcel")]
public void CountLinksAndSetTestNameAtShiftingContentExcelTest(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()));
}
Name of test will be set from name of Excel sheet plus row number.
Data driven tests from CSV files are not supported with NUnit by default, but you can use DataDrivenHelper class methods from our Ocaramba framework to read CSV files and pass test data to your tests. Test data is passed to the test as parameters (key - name of parameter and key value). Data in first row is used for name of parameters - keys.
1.3.1 Just add CSV file with test data to your NUnit project e.g
Make sure that file properties "Copy to Output Directory" is set to "Copy always" and proper path (relative) is set to that file in TestData.cs file.
public static IEnumerable CredentialsCSV()
{
var path = TestContext.CurrentContext.TestDirectory;
path = string.Format(CultureInfo.CurrentCulture, "{0}{1}", path, @"\DataDriven\TestDataCsv.csv");
return DataDrivenHelper.ReadDataDriveFileCsv(path, new[] { "user", "password" }, "credentialCsv");
}
namespace Ocaramba.Tests.NUnit.DataDriven
{
using System.Collections;
/// <summary>
/// DataDriven methods for NUnit test framework
/// </summary>
public static class TestData
{
public static IEnumerable CredentialsCSV()
{
var path = TestContext.CurrentContext.TestDirectory;
path = string.Format(CultureInfo.CurrentCulture, "{0}{1}", path, @"\DataDriven\TestDataCsv.csv");
return DataDrivenHelper.ReadDataDriveFileCsv(path, new[] { "user", "password" }, "credentialCsv");
}
}
}
Where calling the method ReadDataDriveFileCsv()
DataDrivenHelper.ReadDataDriveFileCsv(path, new[] { "user", "password" }, "credentialCsv");
from its definitions:
public static IEnumerable<TestCaseData> ReadDataDriveFileCsv(string file, string[] diffParam, string testName)
file - Full path to Excel DataDriveFile 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
namespace Ocaramba.Tests.NUnit.Tests
{
using System.Collections.Generic;
using global::NUnit.Framework;
using Ocaramba;
using Ocaramba.Tests.NUnit.DataDriven;
using Ocaramba.Tests.PageObjects.PageObjects.TheInternet;
[TestFixture]
[Parallelizable(ParallelScope.Fixtures)]
public class HerokuappTestsNUnit : ProjectTestBase
{
[Test]
[TestCaseSource(typeof(TestData), "CredentialsCSV")]
public void CSVTest(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));
}
}
Where
[TestCaseSource(typeof(TestData), "CredentialsCSV")]
TestData is a a class for relation between data driven tests and xlsx file from 1.3.2
"CredentialsCSV" is a method from TestData class.
Names of test are set dynamically by the values of test data read from CSV file and testName property (testName + diffParam) e.g: creadentialCsv_test111_test222
as many time as you need corresponding row in CSV file
- Home
- Getting started
- Parallel tests execution
- MsTest DataDriven tests from Xml and CSV files
- NUnit DataDriven tests from Xml, CSV and Excel files
- Comparing files by NUnit DataDriven tests
- Visual Testing
- Screen shots: full desktop, selenium. PageSource saving
- Verify-asserts without stop tests
- Downloading files
- Helpers
- Override browser profile preferences, install browser extensions, Headless mode
- Debugging Test.Automation framework
- Logging
- Performance measures
- Webdriver Extends
- More common locators
- Selenium-Grid-support
- Advanced Browser Capabilities and Options
- AngularJS synchronization
- Update App.config or appsettings.json
- Cross browser parallel test execution with testing-Cloud-Providers\SeleniumGrid
- Verifying Javascript Errors from browser
- Enabling Performance Log for Chrome
- Azure DevOps Support
- Edge browser Support
- Downloading and running Selenium Grid with Powershell
- Run Ocaramba tests with Docker container
- HTTP auth in Internet explorer
- ExtentReports Support