Skip to content

Instantly share code, notes, and snippets.

@juherr
Last active October 31, 2016 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juherr/7e233a52d2f12a22a6d54b0770e86c14 to your computer and use it in GitHub Desktop.
Save juherr/7e233a52d2f12a22a6d54b0770e86c14 to your computer and use it in GitHub Desktop.
Surefire - Mixed JUnit and TestNG tests in a class
package test;
import org.testng.TestNG;
public class Main {
public static void main(String... args) {
TestNG tng = new TestNG();
tng.setTestClasses(new Class[]{MixedTest.class});
tng.setJUnit(false);
tng.run();
}
}
package test;
public class MixedTest {
@org.junit.Test
public void testJ1() {
System.out.println("JUnit test");
}
@org.testng.annotations.Test
public void testT1() {
System.out.println("TestNG test");
}
}

junit=true

surefire-junit47 + surefire-testng

[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ threadCount ---
[INFO] Surefire report directory: /home/juherr/Projects/threadCount/target/surefire-reports
[INFO] Using configured provider org.apache.maven.surefire.junitcore.JUnitCoreProvider
[INFO] Using configured provider org.apache.maven.surefire.testng.TestNGProvider
[INFO] parallel='none', perCoreThreadCount=true, threadCount=1, useUnlimitedThreads=false, threadCountSuites=0, threadCountClasses=0, threadCountMethods=0, parallelOptimized=true

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running test.MixedTest
JUnit test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - in test.MixedTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0


-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running test.MixedTest
Configuring TestNG with: TestNG652Configurator
JUnit test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.176 sec - in test.MixedTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

junit=false

surefire-junit47 + surefire-testng

[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ threadCount ---
[INFO] Surefire report directory: /home/juherr/Projects/threadCount/target/surefire-reports
[INFO] Using configured provider org.apache.maven.surefire.junitcore.JUnitCoreProvider
[INFO] Using configured provider org.apache.maven.surefire.testng.TestNGProvider
[INFO] parallel='none', perCoreThreadCount=true, threadCount=1, useUnlimitedThreads=false, threadCountSuites=0, threadCountClasses=0, threadCountMethods=0, parallelOptimized=true

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running test.MixedTest
JUnit test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - in test.MixedTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0


-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running test.MixedTest
Configuring TestNG with: TestNG652Configurator
JUnit test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.187 sec - in test.MixedTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

junit=true

surefire-testng

[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ threadCount ---
[INFO] Surefire report directory: /home/juherr/Projects/threadCount/target/surefire-reports
[INFO] Using configured provider org.apache.maven.surefire.testng.TestNGProvider

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running test.MixedTest
Configuring TestNG with: TestNG652Configurator
JUnit test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.168 sec - in test.MixedTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

junit=false

surefire-testng

[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ threadCount ---
[INFO] Surefire report directory: /home/juherr/Projects/threadCount/target/surefire-reports
[INFO] Using configured provider org.apache.maven.surefire.testng.TestNGProvider

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running test.MixedTest
Configuring TestNG with: TestNG652Configurator
JUnit test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.195 sec - in test.MixedTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

junit=true

surefire-junit47

[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ threadCount ---
[INFO] Surefire report directory: /home/juherr/Projects/threadCount/target/surefire-reports
[INFO] Using configured provider org.apache.maven.surefire.junitcore.JUnitCoreProvider
[INFO] parallel='none', perCoreThreadCount=true, threadCount=1, useUnlimitedThreads=false, threadCountSuites=0, threadCountClasses=0, threadCountMethods=0, parallelOptimized=true

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running test.MixedTest
JUnit test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - in test.MixedTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

junit=false

surefire-junit47

[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ threadCount ---
[INFO] Surefire report directory: /home/juherr/Projects/threadCount/target/surefire-reports
[INFO] Using configured provider org.apache.maven.surefire.junitcore.JUnitCoreProvider
[INFO] parallel='none', perCoreThreadCount=true, threadCount=1, useUnlimitedThreads=false, threadCountSuites=0, threadCountClasses=0, threadCountMethods=0, parallelOptimized=true

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running test.MixedTest
JUnit test
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec - in test.MixedTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mixedtest</groupId>
<artifactId>mixedtest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<properties>
<property>
<name>junit</name>
<value>true</value>
</property>
</properties>
<threadCount>1</threadCount>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.18.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>2.18.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

Main + junit=false

[TestNG] Running:
  Command line suite

TestNG test

===============================================
Command line suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Main + junit=true

[TestNG] Running:
  Command line suite

JUnit test

===============================================
Command line suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
@juherr
Copy link
Author

juherr commented Oct 31, 2016

surefire-junit47 only: ok
surefire-testng only and junit=true: ok if we consider junit=true means "junit tests only"
surefire-testng only and junit=false: ko -> the testng test is not run and depending on what "junit=false" means, the junit test should not be run

surefire-junit47 + surefire-testng: ko

@juherr
Copy link
Author

juherr commented Oct 31, 2016

@Tibor17 About mixed test: I made the same simple test in many possible configurations.

I added the testng point of view about the junit configuration too.

I hope it will help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment