How do you configure a Gradle project to run tests with TestNG instead of the default JUnit?
answer
- testImplementation org.testng:testng
- useTestNG() on Test task
- not the default — JUnit is
- forgot useTestNG() => 0 tests run
- siblings: useJUnit / useJUnitPlatform
basics
~10 sAdd the TestNG dependency to testImplementation, then call useTestNG() inside the test task. Without useTestNG(), Gradle assumes JUnit and won't detect @Test annotations from TestNG.
solid answer
~40 sTestNG support is built into Gradle's `Test` task, but it is not the default — Gradle uses JUnit Platform or JUnit 4 unless told otherwise. Two things are required: (1) declare the runtime/compile dependency with `testImplementation("org.testng:testng:7.x")`, and (2) switch the test framework by calling `useTestNG()` on the `test` task. The dependency puts TestNG annotations on the test classpath so your code compiles; `useTestNG()` tells Gradle's test executor to load TestNG's runner and scan for `org.testng.annotations.Test` rather than JUnit's `@Test`. If you only add the dependency but forget `useTestNG()`, the build runs but reports zero tests because Gradle's default detector ignores TestNG annotations. The call also gives you a configuration closure for suites, groups, and parallelism.
code
kotlin · 7 linesdependencies {
testImplementation("org.testng:testng:7.9.0")
}
tasks.named<Test>("test") {
useTestNG()
}go deeper
Name the two steps: testImplementation dependency + useTestNG() on the test task.
Explain why both are required and call out the silent 0-tests failure when useTestNG() is missing.
Discuss that the Test task is framework-singular, the useJUnit/useJUnitPlatform siblings, and the TestNGOptions closure exposed.
Frame framework selection as a per-Test-task decision and how multiple source sets / Test tasks let a codebase mix frameworks cleanly during migrations.
## Why two steps are needed Gradle's `Test` task is framework-agnostic at the API level but ships with a default test framework. For modern projects that default is JUnit (Platform or 4), so simply putting TestNG on the classpath is **not** enough — Gradle's class detector looks for the framework it has been told to use. You must explicitly opt into TestNG. ### Step 1 — the dependency TestNG annotations (`@Test`, `@BeforeMethod`, `@DataProvider`, ...) live in the `org.testng:testng` artifact. Declare it on the `testImplementation` configuration so it is available both to compile the test sources and at test runtime: ```kotlin testImplementation("org.testng:testng:7.9.0") ``` ### Step 2 — switch the framework Call `useTestNG()` on the `Test` task. This replaces the framework backing the task with `TestNGTestFramework`, which knows how to instantiate TestNG's runner and how to detect TestNG-annotated classes: ```kotlin tasks.named<Test>("test") { useTestNG() } ``` The sibling methods are `useJUnit()` (JUnit 4) and `useJUnitPlatform()` (JUnit 5). Only one framework backs a given `Test` task. ## The classic failure mode If you add the dependency but omit `useTestNG()`, the build still **succeeds** — but the test report shows `0 tests`. Gradle's default JUnit detector simply never matches TestNG's annotations, so nothing runs and nothing fails. This silent pass is the single most common TestNG-wiring bug. ## What useTestNG() unlocks The method accepts a configuration action exposing `TestNGOptions`, where you configure suite XML files, included/excluded groups, parallel execution, listeners, and more. So the same call that enables TestNG is also the entry point for all TestNG-specific tuning.
- Your build is green but the report says 0 tests ran. What is the most likely cause with TestNG?You added the org.testng dependency but never called useTestNG(), so Gradle's default JUnit detector ignored the TestNG @Test annotations and discovered nothing.
- Can a single Test task run both JUnit and TestNG tests?No. A Test task is backed by exactly one framework. Use separate Test tasks (or source sets) if you genuinely need both.
saying these in an interview costs you the question
- Claiming TestNG is auto-detected just by adding the dependency.
- Saying useTestNG() and useJUnitPlatform() can both apply to the same Test task.
- Putting TestNG on `implementation` instead of `testImplementation`.