Best XUnit Parameterized Tests Tutorial: Selenium Testing

Himanshu Sheth

Posted On: March 18, 2021

view count295789 Views

Read time10 Min Read

When a developer comes up with unit tests, there are multiple ways through which they try to optimize the test code. Reduction of code size by reusing the existing code is one way of achieving the same. While working with frameworks like xUnit, Test fixtures can be streamlined by devising data-driven tests or xUnit parameterized tests, thereby taking advantage of code reusability.

Like other popular test frameworks in C#, the xUnit framework also supports parameterized and non-parameterized tests for Selenium testing. The Fact attribute in xUnit denotes a test method that does not take any input arguments. On the other hand, the Theory attribute specifies a test that can take input parameters, and the method can be tested against different input combinations.

You can refer to our earlier blogs on the xUnit framework if you want to get started with automated browser testing using that framework. xUnit Parameterized tests can be created using Theory tests, which can be used along with attributes like InlineData, ClassData, and MemberData to pass data into the test methods.

For a demonstration of xUnit parameterized tests for cross browser testing, we use the Cloud Selenium Grid on LambdaTest. LambdaTest is a cloud-based cross browser testing platform using which automated browser tests can be performed on 2000+ real browsers and operating systems online. A valid combination of user-name and access-key is required to access LambdaTest’s Selenium Grid, which can be obtained from the LambdaTest Profile Page.

Apart from scalability, the other advantage of cloud-based cross browser testing is that tests can be executed in parallel resulting in faster test execution and improved test coverage. Browser capabilities generated using the LambdaTest capabilities generator serve as the input arguments to the xUnit parameterized tests methods.

In this ongoing xUnit tutorial, we have already seen how to run the first script in xUnit. In this tutorial we will see some of the ways to implement xUnit parameterized tests using the Theory attribute and other popular attributes that enable parameterization:

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.

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 Profile Author Profile Author Profile

Author’s Profile

Himanshu Sheth

Himanshu Sheth is a seasoned technologist and blogger with more than 15+ years of diverse working experience. He currently works as the 'Lead Developer Evangelist' and 'Senior Manager [Technical Content Marketing]' at LambdaTest. He is very active with the startup community in Bengaluru (and down South) and loves interacting with passionate founders on his personal blog (which he has been maintaining since last 15+ years).

Blogs: 132



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free