Home > Blockchain >  TestNg and Junit not running in same gradle project
TestNg and Junit not running in same gradle project

Time:08-19

I am using JUnit 4.13.1 and TestNg 6.14.3 in the same gradle project(Jenkins Plugin). One class has TestNg unit tests and other has JUnit unit tests. For me both are not working together. If I use this config in build.gradle then only JUnit works

test {
    testLogging {
        showStandardStreams = true
    }

    doFirst {
        environment 'OUTPUT_DIR', project.buildDir
        systemProperty 'build.notifications.disabled', 'true'
    }
    useTestNG()
    useJUnit()
}

And if I use below config or remove useJUnit() then only JUnit works.

test {
    testLogging {
        showStandardStreams = true
    }

    doFirst {
        environment 'OUTPUT_DIR', project.buildDir
        systemProperty 'build.notifications.disabled', 'true'
    }
    useJUnit()
}

If I keep only useTestNG() then only TestNG works. What is the proper configuration for both to run in same gradle project? The error I get -

Execution failed for task ':test'.
> No tests found for given includes: [com.build.plugins.TestDataTest](--tests filter)

CodePudding user response:

It is conceptually wrong to write

test {
    ...
    useTestNG()
    useJUnit()
}

as 1 Test can only use 1 test framework. Hence JUnit override TestNG in this case.


JUnit 5 is the solution for this case.

Why?

The JUnit Platform serves as a foundation for launching testing frameworks on the JVM. It also defines the TestEngine API for developing a testing framework that runs on the platform

AND

  1. JUnit 4 tests can run with junit-vintage-engine
  2. Test Engine for TestNG is already available. Luckily it support TestNg 6.14.3.

How?

Set build.gradle as

dependencies {
    ...
    testImplementation('org.junit.jupiter:junit-jupiter-api:5.9.0')
    testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine:5.9.0')
    // For Junit 4
    testRuntimeOnly('org.junit.vintage:junit-vintage-engine:5.9.0')
    testImplementation('junit:junit:4.13.1')
    // For testng
    testRuntimeOnly("org.junit.support:testng-engine:1.0.4")
    testImplementation('org.testng:testng:6.14.3')
}

test {
   // use Junit 5
   useJUnitPlatform()
}

Useful Resources: JUnit 5 Third party Extensions Gradle Testing in Java & JVM projects

  • Related