当前位置:网站首页>Reference to junit5 test framework in gradle

Reference to junit5 test framework in gradle

2022-06-24 15:39:00 Exclusive rainy days

Junit5 Is well known recently Junit The test framework .Junit5 It's modular , And it is composed of different modules .

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

  • JUnit Platform Is in JVM Start the test framework on the basis of .
  • JUnit Jupiter It's a combination of a new programming model and an extended model , Used in JUnit 5 Write tests and extensions in .
  • JUnit Vintage Provides a TestEngine, Used to run on a platform based on JUnit 3 and JUnit 4 Test of .

stay build.gradle Write the following code to support the operation of Junit Platform

test {
  useJUnitPlatform()
} 

Can be in useJUnitPlatform See more details in .

Compiling and executing JUnit Jupiter tests

For support Junit Jupiter , You can add the following dependencies

dependencies { 
  testImplementation("org.junit.jupiter:junit-jupiter-api:5.1.0") 
  testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.1.0") 
} 

Then you can write code , And run gradle test To execute .

Executing legacy tests with JUnit Vintage

As previously written , If you want to run Junit3/4, Or the same Jupiter tests Hybrid operation , So we need to introduce JUnit Vintage Engine rely on .

dependencies { 
  testImplementation("org.junit.jupiter:junit-jupiter-api:5.1.0") 
  testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.1.0") 
  testCompileOnly("junit:junit:4.12") 
  testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.1.0") 
} 

Filtering test engine

Junit Platform Allow different test engines , Junit There are currently two out of the box TestEngine,junit-jupiter-engine and junit-vintage-engine.. Of course, you can customize your own implementation For detailed description, click the link .

By default , All test engines will be used during the test run , But if you want to show the implementation of a specific test engine , You can add the following configuration to achieve Filter specific engines.

tasks.test { 
  useJUnitPlatform { 
​    includeEngines("junit-vintage") 
​    // excludeEngines("junit-jupiter") 
  } 
} 

A test engine filtering sample can be found at samples/testing/junitplatform/engine in the ‘-all’ distribution of Gradle.

Reference documents


  1. Testing in Java & JVM projects
原网站

版权声明
本文为[Exclusive rainy days]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241321403613.html