How to execute JUnit and TestNG tests in same proj

2019-01-31 15:39发布

Right now I have both type of tests but when I say "mvn test" it only executes TestNG tests and not Junit. I want to execute both one after another. Any Idea ?

12条回答
2楼-- · 2019-01-31 16:18

There is an open issue for this, so there's no elegant way to do this.

It would be far simpler for you to pick a framework and stick with it.

Edit: My previous answer doesn't work because you can't specify dependencies in the execution. I've tried a few approaches, but the best I can manage is to create a profile for the TestNG dependency so you can toggle between TestNG and JUnit testing, there doesn't seem to be a means to run both TestNG and Junit 4 tests.

One other point to note: You can launch your JUnit tests from TestNG, but I think this only works for JUnit 3 tests.

查看更多
相关推荐>>
3楼-- · 2019-01-31 16:18

I found out a solution to run both test types with TestNG without changing your build tool configuration.

I tested with Gradle but should work with Maven too.

Note that this will run JUnit tests inside TestNG, but not the other way back.

The trick is to use both frameworks' annotations in the test classes and use TestNG asserts for JUnit compatibility.

import static org.testng.AssertJUnit.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

@org.testng.annotations.Test
public final class ApplicationTest {

    @org.testng.annotations.BeforeClass
    @BeforeClass
    public static void setup () {}

    @org.testng.annotations.AfterClass
    @AfterClass
    public static void cleanup () {}

    @Test public void json () throws IOException {
        assertTrue (true);
    }
}

Using this hack, you can easily run existing JUnit tests with TestNG, helping you migrate them when time allows.

Hope it helps!

查看更多
叼着烟拽天下
4楼-- · 2019-01-31 16:19

For JUnit ---

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.19.1</version>
   <dependencies>
  <dependency>
    <groupId>org.apache.maven.surefire</groupId>
    <artifactId>surefire-junit47</artifactId>
    <version>2.19.1</version>
  </dependency>
</dependencies>

Similarly use the dependency for TestNG when required

查看更多
ら.Afraid
5楼-- · 2019-01-31 16:22

for Junit this solved my problem

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.19.1</version>
   <dependencies>
  <dependency>
    <groupId>org.apache.maven.surefire</groupId>
    <artifactId>surefire-junit47</artifactId>
    <version>2.19.1</version>
  </dependency>
</dependencies>
查看更多
做自己的国王
6楼-- · 2019-01-31 16:27

I found that the solution was to force the sure-fire plugin to use JUnit. I did this by overriding surefire plugin in the specific project as follows. The dependency forces surefire to use JUnit.

<build>
    <plugins>
        <!-- force sure fire to use junit instead of testng -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>2.10</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
查看更多
▲ chillily
7楼-- · 2019-01-31 16:29

if you just specify testng provider, it will run both junit tests and testng tests all just once.
so there is no restriction on naming the tests.

plugin versions:
surefire-plugin 2.16 (junit47 and testng providers both version set to 2.16)
testng dependency 6.8.7
junit dependency 4.7

查看更多
登录 后发表回答