The Surefire Plugin is the engine that actually runs your tests. When you type mvn test, Maven reaches the test phase and hands control to Surefire. Surefire finds your test classes, runs them through TestNG (or JUnit), and generates reports. Without Surefire, mvn test does nothing.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<!-- Run a specific testng.xml suite -->
<suiteXmlFiles>
<suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>
<!-- Pass system properties to your test code -->
<systemPropertyVariables>
<browser>${browser}</browser>
<base.url>${base.url}</base.url>
<headless>${headless}</headless>
</systemPropertyVariables>
</configuration>
</plugin>The suiteXmlFile property is parameterized with ${suiteXmlFile}. That means you can change which testng.xml runs from the command line — without editing pom.xml.
# Run all tests using the default testng.xml
mvn clean test
# Run a specific TestNG suite file
mvn clean test -DsuiteXmlFile=testng-smoke.xml
# Run a single test class (bypasses testng.xml)
mvn clean test -Dtest=LoginTest
# Run a single test method
mvn clean test -Dtest=LoginTest#testValidLogin
# Run multiple specific classes
mvn clean test -Dtest=LoginTest,TransferTest,DashboardTest
# Run tests matching a pattern
mvn clean test -Dtest=*Login*
# Run a specific TestNG group
mvn clean test -Dgroups=smoke
# Exclude a broken group
mvn clean test -DexcludedGroups=broken
# Skip tests entirely (just compile and package)
mvn clean package -DskipTestsWhen you use -Dtest=LoginTest, Surefire bypasses testng.xml completely and runs the class directly. This means any testng.xml configuration (groups, parameters, listeners) is ignored. Use -DsuiteXmlFile to run a specific suite with all configuration intact.
System properties passed with -D are available in your Java test code via System.getProperty(). This is how you make tests configurable from the command line.
public class BaseTest {
protected WebDriver driver;
@BeforeMethod
public void setUp() {
// Read browser from command line: mvn test -Dbrowser=firefox
String browser = System.getProperty("browser", "chrome");
String headless = System.getProperty("headless", "false");
switch (browser.toLowerCase()) {
case "firefox":
FirefoxOptions ffOptions = new FirefoxOptions();
if (headless.equals("true")) ffOptions.addArguments("--headless");
driver = new FirefoxDriver(ffOptions);
break;
case "edge":
EdgeOptions edgeOptions = new EdgeOptions();
if (headless.equals("true")) edgeOptions.addArguments("--headless");
driver = new EdgeDriver(edgeOptions);
break;
default:
ChromeOptions chromeOptions = new ChromeOptions();
if (headless.equals("true")) chromeOptions.addArguments("--headless");
driver = new ChromeDriver(chromeOptions);
}
// Read base URL: mvn test -Dbase.url=https://testerrank.com/banking
String baseUrl = System.getProperty("base.url", "https://www.testerrank.com/banking");
driver.get(baseUrl);
}
}<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<!-- Run test methods in parallel -->
<parallel>methods</parallel>
<threadCount>4</threadCount>
<!-- Fork a separate JVM for test execution -->
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
</configuration>| Parallel Mode | What Runs in Parallel | When to Use |
|---|---|---|
| methods | Individual @Test methods | Independent test methods with ThreadLocal driver |
| classes | Entire test classes | Independent test classes — most common for QA |
| tests | <test> tags in testng.xml | Cross-browser testing (Chrome in one, Firefox in another) |
| suites | Suite files | Multiple testng.xml files |
| none (default) | Nothing — sequential | When tests depend on each other |
Parallel execution requires thread-safe code. Your WebDriver instance MUST be ThreadLocal — not a shared static variable. If test A closes the browser while test B is clicking a button, both fail. This is the #1 cause of "tests pass individually but fail when run together."
Q: What is the Maven Surefire Plugin? How do you configure it?
A: The Maven Surefire Plugin is responsible for running tests during the test phase of the Maven lifecycle. We configure it in the build > plugins section of pom.xml. Key configurations: suiteXmlFiles to specify which testng.xml to run, systemPropertyVariables to pass browser and environment settings, and parallel/threadCount for parallel execution. It integrates with TestNG to discover and run test classes, and generates reports in target/surefire-reports.
Key Point: Surefire is the plugin that runs your tests. Configure it in pom.xml with suiteXmlFiles, system properties, and parallel settings.