Best XUnit Parameterized Tests Tutorial: Selenium Testing

Himanshu Sheth

Posted On: March 18, 2021

11 Min

In this XUnit Parameterized Testing Tutorial, developers can streamline their unit test code by creating reusable, data-driven, and parameterized test methods. The xUnit testing framework in C# supports both parameterized and non-parameterized tests for Selenium Automation, helping improve efficiency and reduce redundancy. Using attributes like Fact and Theory, testers can validate multiple input combinations through InlineData, ClassData, and MemberData for more flexible testing.

For a practical demonstration, we use the Cloud Selenium Grid on LambdaTest to perform scalable, cross browser testing with faster parallel execution. Browser configurations generated using the LambdaTest Capabilities Generator act as dynamic input parameters in xUnit methods. Before exploring this in detail, ensure you’ve covered Running First X-Unit Script, as this guide focuses on implementing efficient xUnit parameterized tests in Selenium.

Overview

xUnit parameterized tests in Selenium testing are used to execute the same test multiple times with different input values, ensuring broader test coverage and reducing code duplication. They make cross-browser and data-driven testing faster, easier, and more efficient.

What is xUnit Parameterized tests with InlineData?

InlineData provides direct, constant input values to a test method at compile time. Each InlineData attribute defines a separate test run, making it ideal for simple, fixed data inputs like browser names or versions in Selenium tests.

What is xUnit Parameterized tests with MemberData?

MemberData retrieves test data from a static property or method that returns an IEnumerable<object[]>. It allows dynamic or complex input sets, making it useful when test data needs to be fetched from external sources or generated programmatically.

What is xUnit Parameterized tests with ClassData?

ClassData uses a class that implements IEnumerable<object[]> to supply test parameters. It’s best for large or structured data sets, letting testers organize and reuse complex test data across multiple Selenium test scenarios.

xUnit Parameterized tests with InlineData

The InlineData attribute is commonly used to supply data to a Theory attribute-based xUnit parameterized test. Every instance of the InlineData attribute creates a separate occurrence of the test method. The type and order of the parameters in the InlineData attribute should match with the values that are passed to the constructor. Let’s look at the demonstration of the same in this xUnit tutorial.

Take this certification to master the fundamentals of Selenium automation testing with C# and prove your credibility as a tester. If you are preparing for an interview you can learn more through Selenium interview questions.

Here’s a short glimpse of the Selenium C# 101 certification from LambdaTest:

Demonstration – [InlineData] Attribute

For demonstrating the usage of the InlineData attribute on parallelized cross browser tests, we perform the following tests on different browser & OS combinations.

Test Case 1 – LamdaTest To-Do App

  1. Navigate to the To-Do app https://lambdatest.github.io/sample-todo-app/ using selected browsers.
  2. Check the first two items.
  3. Add a new item – Adding item to the list.
  4. Click the Add button to add that new item to the list.

Browsers on which cross browser testing is performed are:

Browser
Browser version
Platform/Operating System
Chrome
72.0
Windows 10
Microsoft Edge
18.0
Windows 10
Firefox
70.0
Windows 10
Safari
12.0
macOS Mojave

Test Case 2 & 3 – Google Search for LambdaTest (using a different browser and OS combinations)

  1. Navigate to Google.com.
  2. Search for LambdaTest.
  3. Quit the browser window.

Both the test cases are the same, but the execution will be performed on different web browsers.

Test Case 2

Browser
Browser version
Platform/Operating System
Chrome
72.0
Windows 10
Microsoft Edge
18.0
Windows 10
Firefox
70.0
Windows 10

Test Case 3

Browser
Browser version
Platform/Operating System
Microsoft Edge
18.0
Windows 10
Firefox
70.0
macOS High Sierra
Chrome
72.0
Windows 10
Microsoft Edge
12.0
macOS Mojave

The complete implementation is below:

Code WalkThrough

Step 1 – As the tests are executed in parallel, thread-based parallelism is achieved by setting ‘Max number of Parallel Threads to 4.’

Step 2 – The combination of user-name and access-key are used to access the remote Selenium grid on LambdaTest.

Step 3 – The browser capabilities generated by the LambdaTest capabilities generator are passed to the remote Selenium WebDriver API.

Step 4 – The implementation under IDisposable [i.e., public void Dispose()] is empty as the resources held by the Selenium WebDriver for test execution are released after the corresponding test has completed execution.

Step 5 – The browser + OS combinations that were generated for cross browser testing are used as parameters to the InlineData attribute. In turn, these values are passed to the constructor of InlineData, which then acts as arguments to the test method, i.e., LT_ToDo_Test for test case – 1.

For all the tests, a separate execution of InlineData creates a separate execution of the corresponding test method, i.e., for each test case, an iterative execution is carried out till the time all the input values as part of the InlineData attribute are exhausted.

The execution snapshot is as shown below:

The InlineData should be used in scenarios when the method parameters are constants.

This xUnit Tutorial for beginners and professionals will help you learn how to use xUnit framework with Selenium C# for performing Selenium automation testing.

xUnit Parameterized tests with MemberData

Unlike the InlineData attribute that can be used to provide simple compile-time constants as parameters to the test method, the MemberData attribute has more flexibility.

It can be used to fetch data for the Theory attribute using a static method that is local to the test class, using a method from another class, or passing a complex object type.

For a demonstration of parameterization in xUnit using the MemberData attribute, we will load the browser + OS combinations from a method on a different class. In this xUnit tutorial, we will look at the demonstration of the same.

Demonstration – [MemberData] Attribute

For demonstrating the usage of the MemberData attribute on parallelized cross browser tests, we perform the following tests on different browser & OS combinations.

Test Case 1 – LamdaTest To Do App

  1. Navigate to the to-do app https://lambdatest.github.io/sample-todo-app/ using selected browsers.
  2. Check the first two items.
  3. Add a new item – Adding item to the list.
  4. Click the Add button to add that new item to the list.

Browsers on which cross-browser testing is performed are:

Browser
Browser version
Platform/Operating System
Chrome
72.0
Windows 10
Microsoft Edge
18.0
Windows 10
Firefox
70.0
Windows 10
Safari
12.0
macOS Mojave

Test Case 2 – Google Search for LambdaTest (using a different browser and OS combinations)

  1. Navigate to Google.com.
  2. Search for LambdaTest.
  3. Quit the browser window.

Browsers on which cross-browser testing is performed are:

Browser
Browser version
Platform/Operating System
Microsoft Edge
18.0
Windows 10
Firefox
70.0
macOS High Sierra
Safari
12.0
macOS Mojave

The complete implementation is below:

Code WalkThrough

Steps 1-4 – These steps remain the same as those mentioned in the Code WalkThrough of the example demonstrating the InlineData attribute. The implementation of the core logic related to the test cases remains the same. The major change is the addition of MemberData for parameterization.

Step 5 – The data is loaded from a property or method of some other type. For a scenario that involves cross browser testing, this approach works perfectly fine as the number of browser combinations can be added without much effort.

A CB_Data property is added which returns an IEnumerable<object[]>. CB_Data and CB_Data_2 properties of LT_ToDo_Test_DataSource are used in the MemberData attribute for the two test cases.

Shown below is the execution snapshot:

automation testing

xUnit Parameterized tests with ClassData

Like MemberData, ClassData is another attribute that is used to pass values to Theory that are not constants. For providing parameters via ClassData, a class has to be created that inherits from IEnumerable<object[]>. A private list of objects containing the browser + OS combinations is then passed to the Theory attribute.

A GetEnumerator method has to be created on the list Enumerator. Finally, the Class is passed to the ClassData attribute, where the data returned from the class populate the test parameters.

If any of these steps are missed out, the xUnit framework throws an error.

Let’s understand implementing the same in this xUnit tutorial.

Demonstration – [ClassData] Attribute

For demonstrating the usage of the ClassData attribute on parallelized cross browser tests, the same set of tests demonstrating the usage of MemberData is used. You can refer to the test cases in the Demonstration – [MemberData] attribute section before looking into the below implementation.

Code WalkThrough

Steps 1-4 – These steps remain the same as those mentioned in the Code WalkThrough of the example demonstrating the InlineData attribute.

Step 5 – Class named LT_ToDo_Test_DataSource is created which inherits the IEnumerable<object[]>. A private list of objects containing the browser combinations is created along with the implementation of the GetEnumerator method.

Classes LT_ToDo_Test_DataSource & LT_ToDo_Test_DataSource_2 are used for providing parameters via ClassData for test case 1 & test case 2 respectively.

The execution snapshot demonstrating parameterization in xUnit using ClassData attribute is below:

demonstrating parameterization

parameterization

Note: Experience reliable automated testing with Selenium Testing Tool on a cloud grid of 3000+ browsers and operating systems.

Wrapping it up

In this Selenium xUnit tutorial, we have seen how test parameterization is widely used for testing that involves a large number of scenarios. It helps reduce clutter in the test code as there is less duplication and redundancy in the test code. For simple xUnit parameterized tests, the InlineData attribute is ideal, but for more complex scenarios, ClassData and MemberData are more suitable.

These attributes are very handy when performing exhaustive cross browser tests as most of the tests will involve numerous combinations of web browsers and platforms. The advantages of parameterization in xUnit can be multiplied if the automated browser tests are performed on cloud based cross browser testing platforms like LamdaTest as tests can be parallelized.

Author

Himanshu Sheth is the Director of Marketing (Technical Content) at LambdaTest, with over 8 years of hands-on experience in Selenium, Cypress, and other test automation frameworks. He has authored more than 130 technical blogs for LambdaTest, covering software testing, automation strategy, and CI/CD. At LambdaTest, he leads the technical content efforts across blogs, YouTube, and social media, while closely collaborating with contributors to enhance content quality and product feedback loops. He has done his graduation with a B.E. in Computer Engineering from Mumbai University. Before LambdaTest, Himanshu led engineering teams in embedded software domains at companies like Samsung Research, Motorola, and NXP Semiconductors. He is a core member of DZone and has been a speaker at several unconferences focused on technical writing and software quality.

Blogs: 131

linkedintwitter