Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

CI runs a 4-hour TestNG suite. Leadership wants feedback in 20 minutes. Outline your plan.

I would measure first, per-test durations, setup costs, flaky tests and skips, then split the suite into layers: fast unit tests on every commit and integration and end-to-end tests sharded across CI agents. Sharding only works when tests are independent, so dependency chains must go and slow shared environments are replaced with Testcontainers or fakes. Flaky tests are quarantined with owners and deadlines rather than retried silently, so the 20-minute signal can be trusted.

Why this question is asked

A four-hour suite changes how a team works. Developers stop running it, merge on hope, and find out the next morning that something broke. Leadership asking for 20 minutes is really asking for feedback within one coffee break, which is the difference between a suite people use and one they ignore. Interviewers give this question to senior engineers because there is no single trick: the answer is a plan that combines measurement, test design, parallel execution and process, and puts numbers on each step. In this answer, we will start with what to measure, set up the target structure of the pipeline, work out the sharding arithmetic, deal with the things that prevent parallelism, and finish with keeping the result honest.

1Measure Before Changing Anything

Four hours is a total, and totals hide where the time goes. The test reports that TestNG already writes, testng-results.xml and the JUnit-style XML files, contain the duration of every test method and configuration method. Loading a few weeks of them into a spreadsheet or a small database answers the questions that shape the plan: which classes take the most time, how much of it is setup such as starting the application or seeding data, which tests are flaky, and how many tests are skipped because of failed dependencies.

The result is almost always uneven. A common picture is that 5 percent of the tests take 60 percent of the time, that a single @BeforeSuite spends 12 minutes loading a database snapshot, and that a few dependency chains force long sections to run on one thread. Those numbers decide the order of the work.

2Split the Suite Into Layers

Not every test has to run on every commit. We split by cost and purpose, using TestNG groups as the labels. Unit tests, which need no network or database, run on every commit and should finish in a few minutes. Integration tests, which start a service with a real database or message broker, and end-to-end tests, which drive the deployed system, run on every pull request but are spread across many CI agents. A small set of very slow or exploratory suites can run nightly.

So the 20-minute target applies to what blocks a merge, not to every test the company owns. That distinction should be agreed with leadership at the start, because it changes the plan a lot.

Target pipeline for each change
StageGroupsWhere it runsTime budget
Commitunit1 agentunder 5 min
Pull requestintegration8 shards in parallelabout 15 min
Pull requeste2e (critical journeys)8 shards in parallelabout 15 min
Nightlyslow, full e2e16 shardsunder 60 min
The pull request stages run at the same time, so the merge signal arrives in about 20 minutes.

3The Sharding Arithmetic

Sharding means splitting the tests across several CI agents that run at the same time, each with its own machine. If the integration and end-to-end tests take 200 minutes of pure test time, 16 shards would take about 12.5 minutes each in theory. In practice each shard also pays a fixed overhead, for example 3 minutes to check out code, restore dependencies and start containers, and the shards are never perfectly even. So we plan for 16 shards, 8 for integration and 8 for end-to-end tests, at roughly 15 to 16 minutes each, which fits the target with a small margin.

Balance matters more than the count. Splitting by class name is simple but can put two 20-minute classes on the same shard. Splitting by measured duration, assigning the longest classes first to the shard with the least total time so far, keeps shards within a minute or two of each other. Most CI systems offer timing-based splitting; if ours does not, TestNG lets us filter the classes for a shard with a method interceptor.

Java
public class ShardInterceptor implements IMethodInterceptor {
private final int total = Integer.getInteger("shard.total", 1);
private final int index = Integer.getInteger("shard.index", 0);
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext ctx) {
return methods.stream()
.filter(m -> Math.floorMod(m.getMethod().getRealClass().getName().hashCode(), total) == index)
.collect(Collectors.toList()); // shard 3 of 16 keeps only the classes that hash to 3
}
}
// mvn verify -Dshard.total=16 -Dshard.index=3

Notice that the interceptor shards by class, not by method, so a class's @BeforeClass setup runs on only one shard. Hashing is a starting point; replacing it with an assignment computed from recorded durations gives better balance.

4Removing What Blocks Parallelism

Sharding assumes that any test can run on any shard, in any order, at the same time as any other. Dependency chains break that assumption, because dependsOnMethods forces a chain onto a single thread and a single shard. So part of the plan is to rewrite chains into independent tests that create their own state through API helpers or fixtures, keeping only a few genuine journey tests.

Shared environments are the second blocker. If every shard talks to the same staging database, they overwrite each other's data and the database itself becomes the bottleneck. Replacing the shared database with a container per shard, using Testcontainers, the library that starts real databases and brokers in Docker for a test run, gives each shard a clean, private copy in a few seconds. Calls to slow external services can be replaced with stubs such as WireMock, with contract tests making sure the stubs stay honest.

5Keeping the 20-Minute Signal Trustworthy

A fast pipeline that is often red for no reason is not an improvement. More parallelism tends to expose flaky tests, so the plan includes a quarantine process: a test that both passes and fails on the same commit is moved within a day to a non-blocking group, assigned to its owner, and fixed or deleted within a set time, for example two weeks. We do not retry failures silently, because that hides real race conditions and makes the suite slower without anyone noticing.

Finally, we track the numbers we started with: the 90th percentile of pull request pipeline time, not the average, the number of tests in quarantine, and the skip count. A dashboard reviewed every two weeks keeps the suite from drifting back to four hours as tests are added.

Step throughPlan in the order we would execute it
  1. Measure durations, setup, flakiness, skipsweek 1
  2. Tag tests by layer with groupsunit, integration, e2e
  3. Quarantine flaky tests with ownersstarts immediately
  4. Replace shared environments with containersone per shard
  5. Rewrite dependency chainskeep a few journeys
  6. Shard by measured duration16 agents
  7. Track p90 pipeline timereview every two weeks

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

6Follow-up questions

What if the budget does not allow 16 CI agents?

Then we lean harder on the other levers. Better test design, pushing checks down from end-to-end to integration and unit tests, cuts total time permanently. Test selection, running only the tests affected by the change on pull requests and the full set nightly, cuts the time per change. Sharding is the fastest lever but costs compute every day.

Why use the 90th percentile instead of the average pipeline time?

Because developers remember the slow runs. If the average is 18 minutes but one run in ten takes 45, the team experiences the suite as slow and starts working around it. The 90th percentile shows that tail, and a target on it forces us to deal with unbalanced shards and slow outliers.

Should the unit tests also be sharded?

Usually not at first. A well-written unit suite of several thousand tests runs in a few minutes on one machine with TestNG's parallel = classes. If it grows beyond about five minutes, sharding it across two or three agents is cheap, but the larger gains are almost always in the integration and end-to-end layers.

7Conclusion

In this answer, we turned a four-hour suite into a plan for 20-minute feedback. We measured where the time goes, split the tests into layers so only the merge-blocking set has to meet the target, worked out that about 16 duration-balanced shards fit within the budget after overhead, removed dependency chains and shared environments that prevent parallel runs, and protected the result with a quarantine process and a p90 pipeline-time metric.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start by measuring, because four hours is a total that hides where the time goes. The TestNG reports already have every method's duration, so a few weeks of them tell me which classes dominate, how much is setup, which tests are flaky and how many are skipped by dependencies.

Then, if they want more

Usually a small share of tests takes most of the time. Then I would agree with leadership that the 20 minutes is for what blocks a merge.

I would split the suite into layers with groups: unit tests on every commit in a few minutes, integration and critical end-to-end tests on every pull request, sharded across agents, and the very slow suites nightly.

The arithmetic for sharding is simple: if the pull request tests are about 200 minutes of work, 16 shards give about 12 or 13 minutes each, plus a few minutes of overhead, so around 15 to 16 minutes if I balance shards by measured duration.

Sharding only works if tests are independent, so I would rewrite dependsOnMethods chains into tests that build their own state through API helpers, and replace the shared staging database with Testcontainers per shard and stubs for slow external services. More parallelism exposes flakiness, so flaky tests get quarantined with an owner and a deadline instead of silent retries.

Finally I would track the 90th percentile pipeline time every two weeks so it does not drift back.

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 "CI runs a 4-hour TestNG suite. Leadership wants feedback in 20 minutes. Outline your plan.": https://scrivio-interview-library.netlify.app/java/testng/6f157e52cd/