1. Overview
1.概述
Usually, we execute tests during a Maven build using the Maven surefire plugin.
通常,我们在Maven构建期间使用Maven surefire插件执行测试。
This tutorial will explore how to use this plugin to run a single test class or test method.
本教程将探讨如何使用这个插件来运行一个单一的测试类或测试方法。
2. Introduction to the Problem
2.对问题的介绍
The Maven surefire plugin is easy to use. It has only one goal: test.
Maven surefire插件很容易使用。它只有一个目标:测试。
Therefore, with the default configuration, we can execute all tests in the project by the command mvn test.
因此,在默认配置下,我们可以通过mvn test命令执行项目中的所有测试。
Sometimes, we may want to execute one single test class or even one single test method.
有时,我们可能想执行一个单一的测试类,甚至一个单一的测试方法。
In this tutorial, we’ll take JUnit 5 as the testing provider example to address how to achieve it.
在本教程中,我们将以JUnit 5作为测试提供者的例子来解决如何实现它。
3. The Example Project
3.示例项目
To show the test results in a more straightforward way, let’s create a couple of simple test classes:
为了更直接地展示测试结果,让我们创建几个简单的测试类。
class TheFirstUnitTest {
// declaring logger ...
@Test
void whenTestCase_thenPass() {
logger.info("Running a dummyTest");
}
}
class TheSecondUnitTest {
// declaring logger ...
@Test
void whenTestCase1_thenPrintTest1_1() {
logger.info("Running When Case1: test1_1");
}
@Test
void whenTestCase1_thenPrintTest1_2() {
logger.info("Running When Case1: test1_2");
}
@Test
void whenTestCase1_thenPrintTest1_3() {
logger.info("Running When Case1: test1_3");
}
@Test
void whenTestCase2_thenPrintTest2_1() {
logger.info("Running When Case2: test2_1");
}
}
In the TheFirstUnitTest class, we have only one test method. However, TheSecondUnitTest contains four test methods. All our method names are following the “when…then…” pattern.
在TheFirstUnitTest类中,我们只有一个测试方法。然而,TheSecondUnitTest包含四个测试方法。我们所有的方法名称都遵循”when…then…“模式。
To make it simple, we’ve made each test method output a line indicating the method is being called.
为了简单起见,我们让每个测试方法输出一行,表明该方法正在被调用。
Now if we run mvn test, all tests will be executed:
现在如果我们运行mvn test,所有的测试将被执行。
$ mvn test
...
[INFO] Scanning for projects...
...
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.baeldung.runasingletest.TheSecondUnitTest
16:58:16.444 [main] INFO ...TheSecondUnitTest - Running When Case2: test2_1
16:58:16.448 [main] INFO ...TheSecondUnitTest - Running When Case1: test1_1
16:58:16.449 [main] INFO ...TheSecondUnitTest - Running When Case1: test1_2
16:58:16.450 [main] INFO ...TheSecondUnitTest - Running When Case1: test1_3
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.065 s - in com.baeldung.runasingletest.TheSecondUnitTest
[INFO] Running com.baeldung.runasingletest.TheFirstUnitTest
16:58:16.453 [main] INFO ...TheFirstUnitTest - Running a dummyTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - in com.baeldung.runasingletest.TheFirstUnitTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0
[INFO]
...
Next, let’s tell Maven to execute only specified tests.
接下来,让我们告诉Maven只执行指定的测试。
4. Execute a Single Test Class
4.执行一个单一的测试类
The Maven surefire plugin provides a test parameter that we can use to specify test classes or methods we want to execute.
Maven surefire插件提供了一个test参数,我们可以用它来指定我们要执行的测试类或方法。
If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”.
如果我们想执行一个测试类,我们可以执行mvn test -Dtest=”TestClassName”命令。
For instance, we can pass -Dtest=”TheFirstUnitTest” to the mvn command to execute the TheFirstUnitTest class only:
例如,我们可以将-Dtest=”TheFirstUnitTest”传递给mvn命令,只执行TheFirstUnitTest类。
$ mvn test -Dtest="TheFirstUnitTest"
...
[INFO] Scanning for projects...
...
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.baeldung.runasingletest.TheFirstUnitTest
17:10:35.351 [main] INFO com.baeldung.runasingletest.TheFirstUnitTest - Running a dummyTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.053 s - in com.baeldung.runasingletest.TheFirstUnitTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
...
As the output shows, only the test class we’ve passed to the test parameter is executed.
如输出显示,只有我们传递给test参数的测试类被执行。
5. Execute a Single Test Method
5.执行单一的测试方法
Additionally, we can ask the Maven surefire plugin to execute a single test method by passing -Dtest=”TestClassName#TestMethodName” to the mvn command.
此外,我们可以通过向mvn命令传递-Dtest=”TestClassName#TestMethodName” ,要求Maven surefire插件执行单个测试方法。
Now let’s execute the whenTestCase2_thenPrintTest2_1() method in the TheSecondUnitTest class:
现在让我们执行whenTestCase2_thenPrintTest2_1()类中的TheSecondUnitTest方法。
$ mvn test -Dtest="TheSecondUnitTest#whenTestCase2_thenPrintTest2_1"
...
[INFO] Scanning for projects...
...
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.baeldung.runasingletest.TheSecondUnitTest
17:22:07.063 [main] INFO ...TheSecondUnitTest - Running When Case2: test2_1
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.057 s - in com.baeldung.runasingletest.TheSecondUnitTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
...
As we can see, this time, we’ve executed only the specified test method.
我们可以看到,这一次,我们只执行了指定的测试方法。
6. More About the test Parameter
6.关于test参数的更多信息
So far, we’ve shown how to execute a single test class or test method by providing the test parameter value accordingly.
到目前为止,我们已经展示了如何通过提供相应的test参数值来执行单个测试类或测试方法。
Actually, the Maven surefire plugin allows us to set the value of the test parameter in different formats to execute tests flexibly.
实际上,Maven surefire插件允许我们以不同格式设置test参数的值,以灵活执行测试。
Next, we’ll show some commonly used formats:
接下来,我们将展示一些常用的格式。
- Execute multiple test classes by name: -Dtest=”TestClassName1, TestClassName2, TestClassName3…”
- Execute multiple test classes by name pattern: -Dtest=”*ServiceUnitTest” or -Dtest=”The*UnitTest, Controller*Test”
- Specify multiple test methods by name: -Dtest=”ClassName#method1+method2″
- Specify multiple method names by name pattern: -Dtest=”ClassName#whenSomethingHappens_*”
Finally, let’s see one more example.
最后,让我们再看一个例子。
Let’s say we only want to execute all “whenTestCase1…” methods in the TheSecondUnitTest class.
假设我们只想执行TheSecondUnitTest类中所有”whenTestCase1…“方法。
So, following the pattern we’ve talked about above, we hope that -Dtest=”TheSecondUnitTest#whenTestCase1*” will do the job:
因此,按照我们上面谈到的模式,我们希望-Dtest=”TheSecondUnitTest#whenTestCase1*”能够完成这项工作。
$ mvn test -Dtest="TheSecondUnitTest#whenTestCase1*"
...
[INFO] Scanning for projects...
...
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.baeldung.runasingletest.TheSecondUnitTest
17:51:04.973 [main] INFO ...TheSecondUnitTest - Running When Case1: test1_1
17:51:04.979 [main] INFO ...TheSecondUnitTest - Running When Case1: test1_2
17:51:04.980 [main] INFO ...TheSecondUnitTest - Running When Case1: test1_3
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.055 s - in com.baeldung.runasingletest.TheSecondUnitTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
...
As we expected, only the three test methods matching the specified name pattern have been executed.
正如我们所期望的那样,只有与指定名称模式相匹配的三个测试方法被执行。
7. Conclusion
7.结语
The Maven surefire plugin provides a test parameter that allows us a lot of flexibility in choosing which tests we want to execute.
Maven surefire插件提供了一个test参数,让我们可以很灵活地选择要执行的测试。
In this article, we’ve discussed some commonly used value formats of the test parameter.
在这篇文章中,我们已经讨论了test参数的一些常用的值格式。
Also, we’ve addressed through examples how to run only specified test classes or test methods with Maven.
此外,我们还通过实例说明了如何用Maven只运行指定的测试类或测试方法。
As always, the code for the article can be found over on GitHub.
一如既往,该文章的代码可以在GitHub上找到over。