Quick revision
The short answer
We separate the two things the 25 files mix together. Suite structure, which classes or packages, which groups and which parallel mode, goes into one or a few suite files, or is expressed through Surefire groups and include patterns or generated in code. Environment values move to external configuration: environment variables or one property file per environment, read through a typed configuration class. Then we validate the suites in CI so a missing class or an empty group fails fast, and document which suite each pipeline stage runs.
What to remember
In this answer, we saw that 25 suite files drift because each one mixes structure and environment, so every environment multiplies the copies. We moved environment values into external configuration read by a typed TestConfig class, kept secrets in the CI secret store, collapsed the structure into one or two suites with groups and patterns per stage or generated it with the XmlSuite API, validated the suites and the test counts in CI, and documented which stage runs which suite against which environment.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We separate the two things the 25 files mix together. Suite structure, which classes or packages, which groups and which parallel mode, goes into one or a few suite files, or is expressed through Surefire groups and include patterns or generated in code. Environment values move to external configuration: environment variables or one property file per environment, read through a typed configuration class. Then we validate the suites in CI so a missing class or an empty group fails fast, and document which suite each pipeline stage runs.
Why this question is asked
Twenty-five suite files rarely start as a design. Someone copies testng.xml to make a staging version, someone else copies that to run only smoke tests on staging, and two years later there are files like testng-staging-smoke-v2.xml that differ from their siblings in one URL and three forgotten classes. Interviewers ask this question because drift between copies is a general problem, and the fix shows whether we can find the real axes of variation and give each one a single home. In this answer, we will look at why the files drift, separate structure from environment, move the environment into typed configuration, reduce the structural files, add validation, and finish with documenting the mapping to the pipeline.
1Why the Files Drift
A testng.xml file usually mixes two unrelated concerns. The first is structure: which classes, packages or groups run, in which <test> elements, with which parallel mode and thread count. The second is environment: the base URL, database host, feature flags and timeouts, usually given as <parameter> elements. When we need a new environment, we copy the whole file to change the parameters, and we now have two copies of the structure as well. When someone adds a new test class to one copy, the others do not get it.
With 5 environments and 5 kinds of selection, the copies multiply to 25, and every change has to be made 25 times. In practice it is made in 3, which is how a test class ends up running in staging but never in production checks.
| Axis | Examples | Where it should live |
|---|---|---|
| Structure | classes, packages, groups, parallel mode | One or few suite files, or build config |
| Environment | base URL, database host, flags, timeouts | Env variables or one properties file per env |
| Secrets | passwords, tokens | CI secret store, injected as env variables |
2Moving Environment Values Out
The first step removes the environment axis from the suite files completely. Every environment-specific value moves into external configuration. For a handful of values, environment variables are enough, since CI systems set them per job. For more values, one small properties file per environment, such as env/staging.properties, works well, selected with a single variable like TEST_ENV=staging.
Test code should not read these values with scattered System.getenv() calls. A typed configuration class reads them once, applies defaults, and fails with a clear message if something required is missing.
Notice that the precedence is written in one place: an environment variable beats the file, and the file beats the default. Tests get typed values, a URI and a Duration, instead of strings, so a malformed URL fails at startup with a clear exception rather than halfway through a test.
3Reducing the Structural Files
With the environment gone, the remaining differences are about selection. Most of them can be expressed without separate files. If the copies differ only in groups, one suite file plus Surefire's groups and excludedGroups, set per pipeline stage, replaces them. If they differ in which classes run, include patterns in Surefire, or packages in a single suite, usually cover it.
When the variation is real and structured, for example a suite per product area with different parallel settings, we can generate the suites in code with TestNG's XmlSuite API instead of copying XML. The program becomes the single source of truth.
In most teams, though, the result is one or two suite files, with groups and environments supplied from outside, which is simpler than any generator.
4Validating Suites in CI
Even a small set of suites can break silently. If a suite lists a class that was renamed, TestNG fails with a cannot-find-class error, which is good. But a suite that selects a group no test carries any more simply runs zero tests and passes. So we add two checks to CI. First, every suite file is loaded and run in a dry mode or a quick listing step so a missing class fails the build within a minute. Second, after each real run, the pipeline fails if the number of executed tests is zero or drops sharply compared with the previous run, for example by more than 10 percent.
5Documenting the Mapping
Finally, we write down, in one short page next to the pipeline definition, which suite and which groups each stage runs and which environment it uses: commit runs unit with TEST_ENV=local, the pull request runs integration with TEST_ENV=ci, the post-deployment stage runs smoke with TEST_ENV=staging. A new team member should be able to answer what runs where without opening 25 XML files. When this page and the pipeline disagree, the pipeline is fixed, not the page.
- Diff the 25 filesseparate structure from environment
- Move environment values to configtyped
TestConfig, one file per env - Remove secretsCI secret store, env variables
- Collapse structure into 1 or 2 suitesgroups and patterns per stage
- Validate in CImissing classes, zero or falling test counts
- Document stage -> suite -> envnext to the pipeline
6 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Is there still a reason to keep more than one suite file?
Yes, when the structure really differs, for example a browser suite that must run with parallel = tests and one <test> per browser, next to an API suite that runs with parallel = classes. Two or three files with clear purposes are fine. The goal is that no two files differ only in values that belong in configuration.
How do you migrate without losing tests?
Before switching, we list the tests each old suite actually runs, from the reports, and compare them with what the new setup runs for the same stage. Any test that was run before and is not run now is either deliberately dropped or added back. Running both side by side for a week makes the comparison easy.
Why a typed config class instead of TestNG parameters?
TestNG parameters are strings tied to the suite file, which is exactly what drifted. A typed class reads from the environment, converts and validates values once, works the same in the IDE and in CI, and can be shared as a library across repositories.
7Conclusion
In this answer, we saw that 25 suite files drift because each one mixes structure and environment, so every environment multiplies the copies. We moved environment values into external configuration read by a typed TestConfig class, kept secrets in the CI secret store, collapsed the structure into one or two suites with groups and patterns per stage or generated it with the XmlSuite API, validated the suites and the test counts in CI, and documented which stage runs which suite against which environment.
Open with
The 25 files drift because each one mixes two things: structure, meaning which classes and groups run with which parallel mode, and environment values, like the base URL, database host and flags.
Then, if they want more
Every new environment copies the whole file, so the structure gets copied too, and a new test class only reaches some of the copies.
My first step is to move all environment values out: environment variables, or one small properties file per environment selected by a TEST_ENV variable, read through a typed configuration class that applies defaults, lets an environment variable override the file, and fails at startup with a clear message if something is missing.
Secrets go to the CI secret store, not into any file. Once that is done, most of the remaining files differ only in which groups they run, so I replace them with one or two suite files and set groups and include patterns per pipeline stage in Surefire.
If there is real structured variation, I would generate suites in code with TestNG's XmlSuite API rather than copy XML. Then I validate in CI: a renamed class fails fast, and a run that executes zero tests, or far fewer than last time, fails the build, because an empty group passes silently otherwise.
Finally I document, next to the pipeline, which stage runs which suite against which environment, so nobody has to read XML to find out.
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.
- Official documentation
- JUnit user guide, Mockito documentation
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 "Your team keeps 25 testng.xml files for different environments and test selections, and...": https://scrivio-interview-library.netlify.app/java/testng/dadc4d3c53/