From Code to Click—Mastering TestNG and Maven Integration Like a Pro!
Look—I’ve seen automation frameworks die horrible, slow deaths just because someone thought, “Eh, I’ll manually add JARs… who needs Maven?”
Wrong. You need Maven. You need TestNG. And you need them to work together like best buds.
This setup isn’t just for “best practice” points on your resume. It’s about:
Reusability
Scalability
Zero dependency hell
Automation on Jenkins, Docker, CI/CD—you name it
A junior in my team once hard-coded the test class path into the testng.xml
, ran everything from Eclipse, and swore it was fine.
Then Jenkins showed up, punched that fragile setup in the face, and refused to run a single test.
Let’s not be that guy. Let’s do it right.
pom.xml
)Before we dive in, here’s your prep checklist. You don’t want to get halfway through and realize, “Wait—I don’t even have Maven installed.”
✅ Java JDK (8+ is fine)
✅ Maven installed (mvn -v
to check)
✅ IDE (Eclipse or IntelliJ – both work)
✅ A little TestNG knowledge (just enough to not cry when you see an XML file)
✅ Coffee, obviously
Let’s get into the real stuff.
If you’re using Windows:
console.log( 'Code is Poetry' );
Or download from Apache Maven, unzip, set M2_HOME
, and update your PATH
.
Run this to check:
mvn -v
See version info? You’re golden.
In your terminal or IDE:
mvn archetype:generate -DgroupId=com.quantum.test -DartifactId=TestNGMavenDemo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Then cd TestNGMavenDemo
.
Your project structure should look like this:
TestNGMavenDemo/
├── pom.xml
├── src/
│ ├── main/java/
│ └── test/java/
pom.xml
Crack open that pom.xml
like a fortune cookie. Replace the <dependencies>
section with:
org.testng
testng
7.9.0
test
Pro Tip: Don’t hardcode versions from 2014. Keep it fresh. Use Maven Central to check for the latest.
Create a test class at src/test/java/com/quantum/test/SampleTest.java
:
package com.quantum.test;
import org.testng.annotations.Test;
public class SampleTest {
@Test
public void sanityCheck() {
System.out.println("✅ Sanity test is running!");
assert true;
}
}
testng.xml
Place this file in the project root:
mvn test
Done. You’ve officially wired up TestNG with Maven locally.
Let’s get real.
Forgetting testng.xml
path in pom.xml
→ no tests ran, team blamed Jenkins.
Wrong package names in the XML file → TestNG silently ignores your tests. 😑
Not marking test scope in dependency → build bloats like crazy.
Debugging with System.out.println
only → yeah, don’t.
A working setup is like a car that starts.
A great setup? It’s more like a Tesla that auto-parks.
Here’s what you should aim for:
Tests auto-discover with Maven goals
Works on anyone’s machine
Jenkins can execute it
Logging + Reporting baked in
No hardcoded paths, no IDE dependency
Modular structure (Tests, Resources, Reports separated)
TestNGMavenDemo/
├── pom.xml
├── testng.xml
├── src/
│ ├── main/java/
│ └── test/java/
│ └── com/quantum/test/SampleTest.java
└── test-output/ (auto-created by TestNG)
pom.xml
clean and readabletestng.xml
before running on CIpom.xml
from StackOverflow without checking versionstestng.xml
—it’s trying to help youYep! Maven Surefire plugin can auto-discover tests using annotations, but it’s better to use testng.xml
for custom groupings.
Putting the test class in src/main/java
instead of src/test/java
.
Absolutely. Just trigger mvn test
in your build step. That’s the beauty of Maven—it works anywhere.
Use groups
in your testng.xml
or use Maven profiles.
Yes, but why would you? It’s like making Maggi without water. You can, but it won’t work right. Maven brings dependency management, build automation, and easy execution to the party. Without it, you’re stuck juggling JARs and command-line hacks.
Just edit your pom.xml
. Drop in the TestNG dependency like this:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version> <!– or whatever version floats your test boat –>
<scope>test</scope>
</dependency>
Ah, the classic facepalm moment. Make sure:
You’ve created a proper testng.xml
It’s placed in the right location (usually src/test/resources
)
You’re using the Surefire plugin in pom.xml
with proper configuration.
Not strictly. You can run tests based on annotations alone. But with testng.xml
, you get fine-grained control over which classes/methods run, group execution, parallel tests, etc. It’s your test suite’s mission control.
Stick with Maven’s standard layout:
src
├── main
│ └── java (for your main code)
└── test
└── java (for your test classes)
└── resources (for testng.xml, config files, etc.)
Don’t get creative here. Let Maven do what it does best.
Add this magic to your Maven command:
mvn test -Dgroups=”smoke”
Your testng.xml
or test classes must have those groups defined, of course.
Oh boy. @Test(priority=...)
is used within a class to define method order. testng.xml
is used to control the execution order of classes. They don’t override each other — they’re both picky about different things.
Two ways:
Use enabled = false
in your @Test
annotation.
Or comment it out in testng.xml
.
You can also exclude it dynamically using Maven profiles if you’re fancy like that.
Hell yes. Maven + TestNG is Jenkins’ dream combo. You just run mvn clean test
as your build step. And boom – automation.
That’s like asking if pizza is better than biryani. Depends on taste. Gradle is faster and more flexible, but Maven is more widespread and stable. For TestNG, Maven’s ecosystem is still smoother to work with.
Could be:
testng.xml
is not in src/test/resources
You didn’t configure the suiteXmlFiles
path in your pom.xml
You’re secretly using JUnit in disguise
Double-check paths and plugin setup.
Yep, that’s one of the reasons TestNG exists. Use this in testng.xml
:
<suite name=”MySuite” parallel=”tests” thread-count=”4″>
And let Maven do its thing. But make sure your tests don’t fight over shared state.
Testers, devs, and accidental automation engineers—comment below if you’ve seen worse setups or had to debug a broken pom.xml
on a Friday night with production breathing down your neck.
I’ve been there. You’re not alone.
Also Don’t forget to check our blogs.
Designed by ScriptNG
One Response