1. Overview
1.概述
In this tutorial, we’ll understand how to run JUnit 5 tests directly from the command line.
在本教程中,我们将了解如何直接从命令行运行JUnit 5测试。
2. Test Scenarios
2.测试方案
Previously, we’ve covered how to run a JUnit test programmatically. For our examples, we’re going to use the same JUnit tests:
在此之前,我们已经介绍了如何以编程方式运行JUnit测试。在我们的例子中,我们将使用相同的JUnit测试。
public class FirstUnitTest {
@Test
public void whenThis_thenThat() {
assertTrue(true);
}
@Test
public void whenSomething_thenSomething() {
assertTrue(true);
}
@Test
public void whenSomethingElse_thenSomethingElse() {
assertTrue(true);
}
}
public class SecondUnitTest {
@Test
public void whenSomething_thenSomething() {
assertTrue(true);
}
@Test
public void whensomethingElse_thenSomethingElse() {
assertTrue(true);
}
}
3. Running a JUnit 5 Test
3.运行JUnit 5测试
We can run a JUnit 5 test case using JUnit’s console launcher. The executable for this jar can be downloaded from Maven Central, under the junit-platform-console-standalone directory.
我们可以使用JUnit的控制台启动器运行一个JUnit 5测试案例。这个jar的可执行文件可以从Maven中心下载,在junit-platform-console-standalone目录下。
Also, we’ll need a directory that will contain all our compiled classes:
此外,我们还需要一个目录,该目录将包含我们所有的编译类。
$ mkdir target
Let’s see how we can run different test cases using the console launcher.
让我们看看我们如何使用控制台启动器运行不同的测试案例。
3.1. Run a Single Test Class
3.1.运行一个单一的测试类
Before we run the test class, let’s compile it:
在我们运行测试类之前,让我们先编译它。
$ javac -d target -cp target:junit-platform-console-standalone-1.7.2.jar src/test/java/com/baeldung/commandline/FirstUnitTest.java
Now, we’ll run the compiled test class using the Junit console launcher:
现在,我们将使用Junit控制台启动器运行已编译的测试类。
$ java -jar junit-platform-console-standalone-1.7.2.jar --class-path target --select-class com.baeldung.commandline.FirstUnitTest
This will give us test run results:
这将给我们带来试运行结果。
Test run finished after 60 ms
[ 3 containers found ]
[ 0 containers skipped ]
[ 3 containers started ]
[ 0 containers aborted ]
[ 3 containers successful ]
[ 0 containers failed ]
[ 3 tests found ]
[ 0 tests skipped ]
[ 3 tests started ]
[ 0 tests aborted ]
[ 3 tests successful ]
[ 0 tests failed ]
3.2. Run Multiple Test Classes
3.2.运行多个测试类
Again, let’s compile the test classes we want to run:
再次,让我们编译我们想要运行的测试类。
$ javac -d target -cp target:junit-platform-console-standalone-1.7.2.jar src/test/java/com/baeldung/commandline/FirstUnitTest.java src/test/java/com/baeldung/commandline/SecondUnitTest.java
We’ll now run the compiled test classes using the console launcher:
现在我们将使用控制台启动器运行编译好的测试类。
$ java -jar junit-platform-console-standalone-1.7.2.jar --class-path target --select-class com.baeldung.commandline.FirstUnitTest --select-class com.baeldung.commandline.SecondUnitTest
Our results now show that all five test methods were successful:
现在我们的结果表明,所有五个测试方法都是成功的。
Test run finished after 68 ms
...
[ 5 tests found ]
...
[ 5 tests successful ]
[ 0 tests failed ]
3.3. Run All Test Classes in a Package
3.3. 运行一个包中的所有测试类
To run all the test classes in a package, let’s compile all the test classes present in our package:
为了运行一个包中的所有测试类,让我们编译我们包中存在的所有测试类。
$ javac -d target -cp target:junit-platform-console-standalone-1.7.2.jar src/test/java/com/baeldung/commandline/*.java
Again, we’ll run the compiled test classes of our package:
再一次,我们将运行我们包的编译后的测试类。
$ java -jar junit-platform-console-standalone-1.7.2.jar --class-path target --select-package com.baeldung.commandline
...
Test run finished after 68 ms
...
[ 5 tests found ]
...
[ 5 tests successful ]
[ 0 tests failed ]
3.4. Run All the Test Classes
3.4.运行所有的测试类
Let’s run all the test cases:
让我们运行所有的测试案例。
$ java -jar junit-platform-console-standalone-1.7.2.jar --class-path target --scan-class-path
...
Test run finished after 68 ms
...
[ 5 tests found ]
...
[ 5 tests successful ]
[ 0 tests failed ]
4. Running JUnit Using Maven
4.使用Maven运行JUnit
If we’re using Maven as our build tool, we can execute test cases directly from the command line.
如果我们使用Maven作为构建工具,我们可以直接从命令行执行测试用例。
4.1. Running a Single Test Case
4.1.运行单个测试案例
To run a single test case on the console, let’s execute the following command by specifying the test class name:
要在控制台运行一个测试案例,让我们通过指定测试类名称来执行以下命令。
$ mvn test -Dtest=SecondUnitTest
This will give us test run results:
这将给我们带来试运行结果。
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.069 s - in com.baeldung.commandline.SecondUnitTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.211 s [INFO] Finished at: 2021-08-02T23:13:41+05:30
[INFO] ------------------------------------------------------------------------
4.2. Run Multiple Test Cases
4.2.运行多个测试案例
To run multiple test cases on the console, let’s execute the command, specifying the names of all the test classes we want to execute:
要在控制台运行多个测试用例,让我们执行命令,指定我们要执行的所有测试类的名称。
$ mvn test -Dtest=FirstUnitTest,SecondUnitTest
...
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.069 s - in com.baeldung.commandline.SecondUnitTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.069 s - in com.baeldung.commandline.FirstUnitTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.211 s
[INFO] Finished at: 2021-08-02T23:13:41+05:30
[INFO] ------------------------------------------------------------------------
4.3. Run All Test Cases in a Package
4.3.运行一个包中的所有测试用例
To run all the test cases within a package, on the console, we need to specify the package name as part of the command:
要运行一个包内的所有测试用例,在控制台,我们需要指定包的名称作为命令的一部分。
$ mvn test -Dtest="com.baeldung.commandline.**"
...
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.069 s - in com.baeldung.commandline.SecondUnitTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.069 s - in com.baeldung.commandline.FirstUnitTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.211 s
[INFO] Finished at: 2021-08-02T23:13:41+05:30
[INFO] ------------------------------------------------------------------------
4.4. Run All Test Cases
4.4.运行所有测试案例
Finally, to run all the test cases using Maven on the console, we simply execute mvn clean test:
最后,为了在控制台使用Maven运行所有测试案例,我们只需执行mvn clean test。
$ mvn clean test
...
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.069 s - in com.baeldung.commandline.SecondUnitTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.069 s - in com.baeldung.commandline.FirstUnitTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.211 s
[INFO] Finished at: 2021-08-02T23:13:41+05:30
[INFO] ------------------------------------------------------------------------
5. Conclusion
5.总结
In this article, we’ve learned how to run JUnit tests directly from the command line, covering JUnit 5 both with and without Maven.
在本文中,我们学习了如何从命令行直接运行JUnit测试,涵盖了有Maven和无Maven的JUnit 5。
Implementation of the examples shown here is available over on GitHub.
这里显示的例子的实现可以在GitHub上获得,。