Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Mark some TestNG tests as 'smoke' and others as 'slow', and show how to run only smoke tests from testng.xml and from Maven Surefire.

We label tests with @Test(groups = "smoke") or @Test(groups = "slow"), on methods or on the whole class, and select them in testng.xml with <groups><run><include name="smoke"/><exclude name="slow"/></run></groups> inside a <test> element. With Maven Surefire we set <groups>smoke</groups> and <excludedGroups>slow</excludedGroups> in the plugin configuration, or pass -Dgroups=smoke on the command line. Configuration methods must have alwaysRun = true or belong to the selected groups, or they will not run when groups are filtered.

Why this question is asked

Groups are how a TestNG suite answers the question which tests should run now. A quick check after deployment, a full run at night and a pull request run in between all come from the same code, selected by labels. Interviewers ask this coding question because it touches three places, the annotations, the suite file and the build tool, and because one detail, configuration methods being filtered out along with tests, breaks many first attempts. In this answer, we will label the tests, select them from testng.xml, select them from Maven Surefire, see the configuration-method trap, and finish with a small habit that prevents typos in group names.

1A Quick Recap of TestNG Groups

A group is simply a string label attached to a test. A test can belong to several groups, and a run can include some groups and exclude others. TestNG then runs the tests that are in at least one included group and in no excluded group. If a run includes no groups at all, every test runs.

2Labelling Tests as smoke and slow

We put the groups attribute on @Test. On a method, it applies to that method. On the class, it applies to every public method of the class, and method-level groups are added to the class-level ones.

Java
@Test(groups = "slow") // every method in this class is in "slow"
public class ReportExportTest {
public void exportsYearlyReport() { ... } // groups: slow
@Test(groups = "smoke")
public void exportsEmptyReport() { ... } // groups: slow and smoke
}
public class LoginTest {
@Test(groups = "smoke")
public void logsInWithValidPassword() { ... } // groups: smoke
@Test
public void locksAccountAfterFiveFailures() { ... } // no group
}

Notice that a class-level @Test makes all public methods tests, even without their own annotation. exportsEmptyReport ends up in both groups, which matters in the next step.

3Selecting Groups in testng.xml

In the suite file, the <groups> element goes inside a <test> element, next to the classes or packages it applies to. Inside it, <run> lists what to include and exclude.

XML
<suite name="shop">
<test name="smoke-only">
<groups>
<run>
<include name="smoke"/>
<exclude name="slow"/>
</run>
</groups>
<packages>
<package name="com.shop.*"/>
</packages>
</test>
</suite>
<!-- runs: LoginTest.logsInWithValidPassword
left out: ReportExportTest.exportsEmptyReport (smoke, but also slow: exclusion wins)
not run: locksAccountAfterFiveFailures, exportsYearlyReport -->
Step throughHow TestNG decides whether a test is selected
  1. Does the run include any groups?
    YesCheck the test's groupsNoRun every test
  2. Is the test in an excluded group?
    YesLeave it outNoNext check
  3. Is the test in an included group?
    YesRun itNoLeave it out

3 steps in order. Play them, or step through them one at a time.

The reason exportsEmptyReport does not run is that exclusion wins over inclusion. A test in an excluded group is left out even if it is also in an included group. That is usually what we want: a smoke run should never accidentally include something slow.

4Selecting Groups From Maven Surefire

Maven Surefire, the plugin that runs tests in the test phase, can pass groups to TestNG without a suite file. We set them in the plugin configuration for the default, and override them from the command line when needed.

XML
<properties>
<test.groups>smoke</test.groups>
<test.excludedGroups>slow</test.excludedGroups>
</properties>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<groups>${test.groups}</groups>
<excludedGroups>${test.excludedGroups}</excludedGroups>
</configuration>
</plugin>
mvn test # smoke tests that are not slow
mvn test -Dtest.groups=smoke,regression # a comma-separated list of groups

Notice why the values go through Maven properties. Surefire's groups parameter also reads the user property groups, so with no <groups> in the pom, mvn test -Dgroups=smoke works on its own. But once the pom sets <groups>smoke</groups> as a literal, the explicit configuration wins and -Dgroups is ignored. Writing <groups>${test.groups}</groups> keeps a default in the pom and still lets the command line switch it cleanly.

5The Trap: Configuration Methods Are Filtered Too

Configuration methods such as @BeforeClass and @BeforeMethod are subject to group filtering as well. If a run includes only smoke, a @BeforeClass with no group is not run, and the smoke tests in that class fail with a NullPointerException or a not-logged-in error.

Java
public class LoginTest {
private Browser browser;
@BeforeClass // not in "smoke" -> skipped in a smoke run
public void open() { browser = Browser.start(); }
@Test(groups = "smoke")
public void logsInWithValidPassword() {
browser.login("ana", "pw"); // NullPointerException: browser is null
}
}
// Fix: run the setup whatever groups are selected
@BeforeClass(alwaysRun = true)
public void open() { browser = Browser.start(); }

alwaysRun = true tells TestNG to run the configuration method regardless of group selection. The alternative is to put the method in the same groups, as in @BeforeClass(groups = {"smoke", "slow"}), but that has to be updated every time a new group appears, so alwaysRun is the more robust choice for setup and teardown.

6Avoiding Typos With a Constants Class

Group names are plain strings, and a typo such as "smoek" does not cause an error; the test just never runs in a smoke run. Keeping the names in one class, and referring to them everywhere, lets the compiler catch mistakes.

Java
public final class Groups {
public static final String SMOKE = "smoke";
public static final String SLOW = "slow";
private Groups() {}
}
@Test(groups = Groups.SMOKE)
public void logsInWithValidPassword() { ... }

Annotation values must be compile-time constants, and a static final String initialised with a literal is one, so this compiles. It also makes it easy to find every smoke test with the IDE's find-usages.

7Follow-up questions

Can groups be selected with patterns?

Yes. TestNG accepts regular expressions in group names in testng.xml, for example <include name="smoke.*"/> to include smoke.login and smoke.checkout. It is handy for hierarchical names, but too many patterns make it hard to know what a run includes, so most teams keep a small, flat list of groups.

What is the Gradle equivalent?

In Gradle, the test task's TestNG options take includeGroups and excludeGroups, for example test { useTestNG { includeGroups 'smoke'; excludeGroups 'slow' } }. For a command-line switch, we read a project property in the build script and pass it to includeGroups.

Why might a smoke run report zero tests?

The usual causes are a typo in the group name, groups set in both testng.xml and Surefire so that they combine unexpectedly, or a mix of JUnit and TestNG on the classpath where Surefire picked the wrong provider. Failing the build when the test count is zero catches all of these early.

8Conclusion

In this answer, we labelled tests with @Test(groups = ...) on methods and classes, selected them in testng.xml with include and exclude inside <run>, where exclusion wins, and selected them from Maven Surefire with groups and excludedGroups or -Dgroups. We saw that configuration methods are filtered too, so setup needs alwaysRun = true, and we kept group names in a constants class so typos become compile errors.

What to say in the interviewAbout 90 seconds aloud

Open with

I label the tests with the groups attribute on @Test, so @Test with groups equals smoke on the quick checks and groups equals slow on the long ones.

Then, if they want more

I can also put @Test with a group on the class, and then every public method in the class is in that group, plus any groups the method adds itself. In testng.xml I add a groups element inside the test element, with run containing include name smoke and exclude name slow.

TestNG runs tests that are in an included group and not in an excluded one, and exclusion wins, so a test tagged both smoke and slow is left out.

With Maven Surefire I do not need the suite file: in the plugin configuration I set groups to smoke and excludedGroups to slow, and on the command line I can run mvn test with -Dgroups equals smoke. I usually route that through a Maven property so overriding it is predictable.

The important trap is configuration methods: when groups are filtered, a @BeforeClass or @BeforeMethod that is not in the selected group does not run, and the smoke tests fail with nulls. So setup and teardown methods get alwaysRun equals true.

Finally, group names are just strings and a typo silently runs nothing, so I keep them as constants in a Groups class and reference those, and I make CI fail if a run executes zero tests.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Mark some TestNG tests as 'smoke' and others as 'slow', and show how to run only smoke...": https://scrivio-interview-library.netlify.app/java/testng/74f9f4dfd5/