How to run JUnit Parameterized Test in Selenium

On This Page What Are JUnit Parameterized Tests in Selenium?June 26, 2026 · 10 min read · Tool Comparison

How to run JUnit Parameterized Test in Selenium

In Selenium, executing the same trial with multiple inputs oftentimes results in code duplication and care overhead. JUnit Parameterized Tests helps overcome this by dynamically injecting test information, cut redundancy.

Overview

JUnit Parameterized Tests in Selenium

JUnit Parameterized Tests in Selenium is a quiz access that lets you run the same test multiple times with various sets of input data. This way you can minimize code duplication and streamline tests.

Why Use JUnit Parameterized Tests in Selenium?

  • Minimizes Code Duplication
  • Maximizes Test CoverageF
  • Simplifies Test Maintenance
  • Facilitates Data-Driven Testing
  • Improves Efficiency

What Are JUnit Parameterized Tests in Selenium?

JUnit Parameterized Tests in Selenium is a testing approach that lets you run the same test multiple clip with diverse sets of stimulus data. This way you can minimize code duplication and streamline tests.

Benefits of JUnit Parameterized Test

To achieve parameterization in, we can always use spreadsheet / to hold data, read it in the automation script and perform read and write functions. However, loading such huge file to act with a small information set would be time-consuming as it takes considerable time to load while executing Selenium hand.

JUnit parameterization comes to rescue against this trouble, providing various inbuiltcontroversy sourcesfor parameterized tests.

Apart from this key benefit, JUnit Parameterized Test also volunteer various other advantages like:

  • Reduce code duplication: You can write a single examination method to manage all variations, instead of writing different test instance for different inputs.
  • Maxmize Test Coverage: Facilitates the substantiation of assorted input scenarios, edge cases and browser configurations.
  • Easy Test Maintanence: Maintenance easier since the changes in test logic has to be update only once.
  • Support for Data-driven testing: Works seamlessly with CSV files, databases and external information sources to drive dynamic test execution.

Also Read:

How to Run JUnit5 Parameterized Test in Selenium?

JUnit5 Parameterized Test helps to run the like test multiple times with different arguments/values. They are announce just like the veritable @ Test method but instead of @ Test annotation @ ParameterizedTest is used.

All supported argument sources are configured utilize annotations from theorg.junit.jupiter.params.provider package.

Adding JUnit5 Dependencies

If it is a Maven undertaking, we need to addjunit-jupiter-paramsdependency under & lt; colony & gt; tag in pom.xml along withjunit-jupiter-engineandjunit-platform-launcher.

& lt; dependencies & gt; & lt; dependency & gt; & lt; groupId & gt; org.seleniumhq.selenium & lt; /groupId & gt; & lt; artifactId & gt; selenium-java & lt; /artifactId & gt; & lt; version & gt; 4.3.0 & lt; /version & gt; & lt; /dependency & gt; & lt; dependency & gt; & lt; groupId & gt; io.github.bonigarcia & lt; /groupId & gt; & lt; artifactId & gt; webdrivermanager & lt; /artifactId & gt; & lt; version & gt; 5.2.1 & lt; /version & gt; & lt; /dependency & gt; & lt; dependency & gt; & lt; groupId & gt; org.junit.jupiter & lt; /groupId & gt; & lt; artifactId & gt; junit-jupiter-engine & lt; /artifactId & gt; & lt; version & gt; 5.9.0 & lt; /version & gt; & lt; scope & gt; test & lt; /scope & gt; & lt; /dependency & gt; & lt; dependency & gt; & lt; groupId & gt; org.junit.platform & lt; /groupId & gt; & lt; artifactId & gt; junit-platform-launcher & lt; /artifactId & gt; & lt; version & gt; 1.7.0 & lt; /version & gt; & lt; scope & gt; test & lt; /scope & gt; & lt; /dependency & gt; & lt; dependency & gt; & lt; groupId & gt; org.junit.jupiter & lt; /groupId & gt; & lt; artifactId & gt; junit-jupiter-params & lt; /artifactId & gt; & lt; version & gt; 5.9.0 & lt; /version & gt; & lt; scope & gt; test & lt; /scope & gt; & lt; /dependency & gt; & lt; /dependencies & gt;

Make sure to use Java version 8 or above and add JUnit5 library to the project build path before go with the program.

Talk to an Expert

Four widely used JUnit5 Argument Sources

Here are the four popular JUnit 5 JUnit5 Argument Sources:

  • @ ValueSource
  • @ EnumSource
  • @ CsvSource
  • @ CsvFileSource
  • For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

Read More:

How to compose Parameterized Test in JUnit5:

  1. First declare @ ParameterizedTest notation to the test method.
  2. Declare any one argument source that will provide the arguments for each exam invocation.
  3. Consume the arguments in tryout method

Example:

@ DisplayName (`` Verifying lookup functionality in Google. Search data is fetched from @ ValueSource '') @ ParameterizedTest (name = `` index= & gt; str= '' {0} '' '') @ ValueSource (strings = {`` Selenium '', `` JUnit4 '', `` JUnit5 ''}) void testSearch (String str) {srchBox.sendKeys (str + '' \n ''); // Printing the title of the Search page String title = driver.getTitle (); System.out.println (`` The title is: `` + title); Assertions.assertTrue (title.contains (str)); driver.close ();}

@ ValueSource

  • @ ValueSource is the uncomplicated argument source provided by and it is used for simple literal values such as primitives and Strings.
  • It is used for providing a single argumentation per parameterized test conjury.
  • We can not pass null as an argument, yet for Strings and class.
  • It supports literals like little, byte, int, long, float, double and char.

Let us enforce all four argument sources in a to validate search event in Google.

In the below programme, 3 values are passed as search data via@ ValueSourceto the trial methodtestSearch (). Later, it is asserted via the JUnit5 Assertions class that the page title contains the string value passed.

significance org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; significance org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import io.github.bonigarcia.wdm.WebDriverManager; @ DisplayName (`` Pass the method argument render by the @ ValueSource annotation '') class ValueSourceTest {WebDriver driver; WebElement srchBox; @ BeforeEach public emptiness init () {WebDriverManager.chromedriver () .setup (); driver = new ChromeDriver (); driver.get (`` https: //www.google.com/ ''); driver.manage () .window () .maximize (); System.out.println (`` Search start now ''); //Search string in google srchBox= driver.findElement (By.cssSelector (`` input.gLFyf ''));} @ DisplayName (`` Verifying search functionality in Google. Search data is fetched from @ ValueSource '') @ ParameterizedTest (name = `` index= & gt; str= '' {0} '' '') @ ValueSource (twine = {`` Selenium '', `` JUnit4 '', `` JUnit5 ''}) void testSearch (String str) {srchBox.sendKeys (str + '' \n ''); // Printing the rubric of the Search page String rubric = driver.getTitle (); System.out.println (`` The title is: `` + title); Assertions.assertTrue (title.contains (str)); driver.close ();}}

testSearch () method is parametrized with the@ ParameterizedTest and @ ValueSourceannotation.

JUnit5 exam runner executestestSearch ()method three times, each time assigning different values from the@ ValueSource array.

This means Google Chrome will launch three times, and each time it will search with 3 different datum and asseverate it too.

@ EnumSource

  • To run a test method with different values from an Enumeration we can use @ EnumSource.
  • The test method will be invoked for each Enum constant at a time.
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; signification org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import io.github.bonigarcia.wdm.WebDriverManager; @ DisplayName (`` Pass the method parameters render by the @ EnumSource annotation '') public category EnumSourceTest {WebDriver driver; WebElement srchBox; enum SearchData {Selenium, JUnit4, JUnit5;} @ BeforeEach public void init () {WebDriverManager.chromedriver () .setup (); driver = new ChromeDriver (); driver.get (`` https: //www.google.com/ ''); driver.manage () .window () .maximize (); System.out.println (`` Search begins now ''); //Search in goole srchBox= driver.findElement (By.cssSelector (`` input.gLFyf ''));} @ DisplayName (`` Verifying hunting functionality in Google. Search information is get from @ EnumSource '') @ ParameterizedTest (gens = `` index= & gt; data= '' {0} '' '') @ EnumSource (SearchData.class) void testSearch (Object data) {srchBox.sendKeys (data + '' \n ''); // Printing the title of the new page String title = driver.getTitle (); System.out.println (`` The title is: `` + rubric); Assertions.assertTrue (title.equals (data)); driver.close ();}}

JUnit5 exam runner executestestSearch ()method three times, each time assigning different value from the@ EnumSourceraiment which is enumSearchData

The assertion will fail as we hold maintain that the page title is adequate to the string value passed which is not true.

Assertions.assertTrue (title.equals (data));

If we want to specify the enum value that are passed to our test method, we can specify the enum values by setting the value of the@ EnumSourceannotation & # 8217; s names attribute.

@ DisplayName (`` Verifying hunt functionality in Google. Search information is fetched from @ EnumSource '') @ ParameterizedTest (name = `` index= & gt; data= '' {0} '' '') @ EnumSource (value=SearchData.class, names = {`` JUnit5 ''}) void testSearch (Object data) {srchBox.sendKeys (data + '' \n ''); // Printing the rubric of the new page String title = driver.getTitle (); System.out.println (`` The title is: `` + title); Assertions.assertTrue (title.equals (data)); driver.close ();}

This time JUnit5 test runner will run the testSearch method only once with value as “ JUnit5 ” and ignoring early 2 values.

Also Read:

@ CsvSource

  • With @ ValueSource and @ EnumSourcewe can pass only a individual disceptation to the test method but there are many scenarios where we like to pass multiple disceptation.
  • If we need to pass multiple but limited arguments to the test method, it can be achieve by using@ CsvSource.
  • We postulate to configure the test data by use an array of Strings that needs to be comma-separated. An empty, quote value (”) results in an empty String.
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; importation org.junit.jupiter.params.provider.CsvSource; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; meaning org.openqa.selenium.WebElement; significance org.openqa.selenium.chrome.ChromeDriver; import io.github.bonigarcia.wdm.WebDriverManager; @ DisplayName (`` Pass the method parameters provided by the @ CsvSource annotation '') public class CSVSourceTest {WebDriver driver; WebElement srchBox; @ BeforeEach public vacuum init () {WebDriverManager.chromedriver () .setup (); driver = new ChromeDriver (); driver.get (`` https: //www.google.com/ ''); driver.manage () .window () .maximize (); System.out.println (`` Search begins now ''); //Search in goole srchBox= driver.findElement (By.cssSelector (`` input.gLFyf ''));} @ DisplayName (`` Verifying search functionality in Google. Search data is fetched from @ CsvSource '') @ ParameterizedTest (gens = `` index= & gt; str1= '' {0} '', str2= '' {1} '' '') @ CsvSource ({'' Selenium, Testing '', '' JUnit4, Testing '', '' JUnit5, Testing '',}) void testSearch (String str1, String str2) {srchBox.sendKeys (str1 + str2 + '' \n ''); // Printing the title of the new page String title = driver.getTitle (); System.out.println (`` The title is: `` + title); Assertions.assertTrue (title.contains (str1)); driver.close ();}}

JUnit5 tryout contrabandist executestestSearch ()method 3 times, each time assigning 2 argument values from the@ CsvSource array.

@ CsvFileSource

  • @ CsvFileSourceis similar to@ CsvSourceexcept we can read the CSV tokens from a CSV file instead of reading from inline tokens.
  • If we require to pass multiple arguments to the test method it won ’ t look good to place all the data in the test file. Hence, we can use@ CsvFileSourcefor such a large set of data.
  • Like @ CsvSource, it let comma-separated value as arguments.
  • The argument values from each line must use the like order as the method parameters of our test method.
  • To use @ CsvFileSourceargument, let us first make the CSV file and add all the required data.
  • Add this file under the“ src/test/resources ”folder. We can create this folder by right clicking on the project on eclipse/ any java editor, chink on New- & gt; Source brochure and give name as “src/test/resources
  • Finally, footnote the exam method with the@ CsvFileSourceand configure the location of the CSV File.
import org.junit.jupiter.api.Assertions; spell org.junit.jupiter.api.BeforeEach; importation org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvFileSource; import org.openqa.selenium.By; significance org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import io.github.bonigarcia.wdm.WebDriverManager; @ DisplayName (`` Pass the method parameters cater by the @ CsvFileSource annotation '') public class CSVFileSourceTest {WebDriver driver; WebElement srchBox; @ BeforeEach public vacuum init () {WebDriverManager.chromedriver () .setup (); driver = new ChromeDriver (); driver.get (`` https: //www.google.com/ ''); driver.manage () .window () .maximize (); System.out.println (`` Search begins now ''); //Search in goole srchBox= driver.findElement (By.cssSelector (`` input.gLFyf ''));} @ DisplayName (`` Verifying search functionality in Google. Search data is fetched from @ CsvFileSource '') @ ParameterizedTest (gens = `` index= & gt; str1= '' {0} '', str2= '' {1} '', str3= '' {2} '' '') @ CsvFileSource (resources = `` /testdata.csv '') void testSearch (String str1, String str2, String str3) {srchBox.sendKeys (str1 + str2 +str3+ '' \n ''); // Printing the title of the new page String rubric = driver.getTitle (); System.out.println (`` The title is: `` + title); Assertions.assertTrue (title.contains (str1)); driver.close ();}}

JUnit5 test contrabandist executestestSearch ()method 3 clip, each time assigning 3 argument values from the@ CsvFileSource array.

Read More:

Best Practices for Using Parameterized Tests in Selenium

Here are some better practices to follow while using parameterized tests in Selenium:

  • Use Proper Inputs: Use test inputs that include real-world scenarios, edge cases and boundary value to drive comprehensive testing.
  • Organize Test Data: Store test data in external files or use @ CsvSource, @ CsvFileSource, or @ MethodSource to enhance maintainability. It & # 8217; s best to avoid hardcoding values.
  • Readability: Use descriptive method names to show what is being tested. Keep a integrated and clear test logic for best readability.
  • Avoid Excessive Paramterization: Break the test into smaller test lawsuit if unreasonable parameters get it difficult to translate.
  • Utilize Data-driven Testing: Leverage TestNG & # 8217; s during complex scenario.

Conclusion

In this article, we hold con what parameterization is and why it is necessary to implement that in any automation model. We also hear how to create parameterized tests in Selenium script using various JUnit5 argument sources.

Refer to the to hear more about executing JUnit 5 Tests on Real device

With likewise coming into the picture, instead of running tests sequentially, opt for for simultaneous test execution. By running JUnit Tests on of multiple real browser-device combinations simultaneously, important time and effort can be reduced.

Tags
100,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free