Running a TestNG Project From the Command Line – 从命令行运行一个TestNG项目

最后修改: 2022年 1月 13日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this short tutorial, we’ll see how to launch TestNG tests from the command line. This is useful for builds or if we want to run an individual test directly during development.
We may use a build tool like Maven to execute our tests, or we may wish to run them directly via the java command.
Let’s look at both approaches.

在这个简短的教程中,我们将看到如何从命令行启动TestNG>测试。这对于构建或我们想在开发过程中直接运行单个测试非常有用。
我们可以使用
Maven等构建工具来执行我们的测试,或者我们希望通过java命令直接运行它们。
让我们来看看这两种方法。

2. Example Project Overview

2.示例项目概述

For our example, let’s use some code containing one service that formats a date into a string:

对于我们的例子,让我们使用一些包含一个服务的代码,将一个日期格式化为一个字符串。

public class DateSerializerService {
    public String serializeDate(Date date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }
}

For the test, let’s have one test to check that a NullPointerExeception is thrown when a null date is passed to the service:

对于测试,让我们有一个测试来检查当一个null日期被传递给服务时,是否会抛出一个NullPointerExeception

@Test(testName = "Date Serializer")
public class DateSerializerServiceUnitTest {
    private DateSerializerService toTest;

    @BeforeClass
    public void beforeClass() {
        toTest = new DateSerializerService();
    }

    @Test(expectedExceptions = { NullPointerException.class })
    void givenNullDate_whenSerializeDate_thenThrowsException() {
        Date dateToTest = null;

        toTest.serializeDate(dateToTest, "yyyy/MM/dd HH:mm:ss.SSS");
    }
}

We’ll also create a pom.xml that defines the required dependencies to execute TestNG from the command line. The first dependency we need is TestNG:

我们还将创建一个pom.xml,定义从命令行执行TestNG所需的依赖性。我们需要的第一个依赖是TestNG

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.4.0</version>
    <scope>test</scope>
</dependency>

Next, we need JCommander. TestNG uses it to parse the command line:

接下来,我们需要JCommander>。TestNG使用它来解析命令行。

<dependency>
    <groupId>com.beust</groupId>
    <artifactId>jcommander</artifactId>
    <version>1.81</version>
    <scope>test</scope>
</dependency>

Finally, if we want TestNG to write HTML test reports, we need to add the WebJar for JQuery dependency:

最后,如果我们想让TestNG编写HTML测试报告,我们需要添加WebJar for JQuery依赖。

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.5.1</version>
    <scope>test</scope>
</dependency>

3. Setup to Run TestNG Commands

3.运行TestNG命令的设置

3.1. Using Maven to Download the Dependencies

3.1.使用Maven下载依赖项

As we have a Maven project, let’s build it:

既然有了Maven项目,我们就来构建它。

c:\> mvn test

This command should output:

这个命令应该输出。

[INFO] Scanning for projects...
[INFO] 
[INFO] ----------< com.baeldung.testing_modules:testng_command_line >----------
[INFO] Building testng_command_line 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.639 s
[INFO] Finished at: 2021-12-19T15:16:52+01:00
[INFO] ------------------------------------------------------------------------

Now, we have everything we need to run TestNG tests from the command line.
All the dependencies will have been downloaded into the Maven local repository, which is normally inside the user’s .m2 folder.

现在,我们拥有了从命令行运行TestNG测试所需的一切。
所有依赖项都已下载到Maven本地仓库,通常在用户的.m2文件夹中。

3.2. Getting Our Classpath

3.2.获取我们的类路径

To execute commands via the java command, we need to add a -classpath option:

为了通过java命令执行命令,我们需要添加一个-classpath选项

$ java -cp "~/.m2/repository/org/testng/testng/7.4.0/testng-7.4.0.jar;~/.m2/repository/com/beust/jcommander/1.81/jcommander-1.81.jar;~/.m2/repository/org/webjars/jquery/3.5.1/jquery-3.5.1.jar;target/classes;target/test-classes" org.testng.TestNG ...

We’ll abbreviate this to -cp <CLASSPATH> in our command-line examples later on.

在我们后面的命令行例子中,我们将把它缩写为-cp <CLASSPATH>

4. Check TestNG Command Line

4.检查TestNG命令行

Let’s check that we can access TestNG via java:

让我们检查一下,我们可以通过java访问TestNG。

$ java -cp <CLASSPATH> org.testng.TestNG

If everything works fine, the console will show a message:

如果一切工作正常,控制台将显示一条信息。

You need to specify at least one testng.xml, one class or one method
Usage: <main class> [options] The XML suite files to run
Options:
...

5. Launch TestNG Single Test

5.启动TestNG单项测试

5.1. Running a Single Test With the java Command

5.1.使用java命令运行单个测试

Now, we can run a single test quickly without having to configure a single test suite file, simply using the following command line:

现在,我们可以快速运行一个测试,而不需要配置一个测试套件文件,只需使用以下命令行。

$ java -cp <CLASSPATH> org.testng.TestNG -testclass "com.baeldung.testng.DateSerializerServiceUnitTest"

5.2. Running a Single Test With Maven

5.2.用Maven运行单个测试

If we want Maven to only execute this test, we could configure the maven-surefire-plugin in the pom.xml file:

如果我们想让Maven只执行这个测试,我们可以在pom.xml文件中配置maven-surefire-plugin

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/DateSerializerServiceUnitTest.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

In the example, we have a profile with the name ExecuteSingleTest configured to execute DateSerializerServiceUnitTest.java. We can run this profile:

在这个例子中,我们有一个名为ExecuteSingleTest的配置文件,被配置为执行DateSerializerServiceUnitTest.java。我们可以运行这个配置文件。

$ mvn -P ExecuteSingleTest test

As we can see, Maven requires much more configuration than a simple TestNG command-line execution to execute a single test.

我们可以看到,Maven需要的配置比简单的TestNG命令行执行单个测试要多得多

6. Launch TestNG Test Suite

6.启动TestNG测试套件

6.1. Running a Test Suite With the java Command

6.1.用java命令运行一个测试套件

Test Suite files define how the tests should be run. We can have as many as we need. And, we can run a test suite by pointing to the XML file that defines the test suite:

测试套件文件定义了测试应该如何运行。我们需要多少个就有多少个。而且,我们可以通过指向定义测试套件的XML文件来运行一个测试套件

$ java -cp <CLASSPATH> org.testng.TestNG testng.xml

6.2. Running a Test Suite Using Maven

6.2.使用Maven运行测试套件

If we want to execute test suites using Maven, we should configure the plugin maven-surefire-plugin:

如果我们想用Maven执行测试套件,我们应该配置插件maven-surefire-plugin

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Here, we have a Maven profile named ExecuteTestSuite that will configure the maven-surefire plugin to launch the testng.xml test suite. We can run this profile using the command:

在这里,我们有一个名为ExecuteTestSuite的Maven配置文件,它将配置maven-surefire插件以启动testng.xml测试套件。我们可以用以下命令来运行这个配置文件。

$ mvn -P ExecuteTestSuite test

7. Conclusion

7.结语

In this article, we saw how the TestNG command line is helpful to run single test files, while Maven should be used to configure and launch a full set of tests.
As always, the example code for this article is available over on GitHub.

在这篇文章中,我们看到TestNG命令行对运行单个测试文件很有帮助,而Maven应被用来配置和启动一整套测试
一如既往,本文的示例代码可在GitHub上找到