跳至内容

如何将 testng.xml 文件添加到 AWS Device Farm 的 Appium Java TestNG 测试包中的 *-tests.jar 文件根目录?

2 分钟阅读
0

我想为我的 AWS Device Farm 项目配置 Appium Java TestNG 测试。但是,我需要先在 Appium 测试包中的 *-tests.jar 文件根目录中添加一个 testng.xml 文件。我该如何操作?

简短描述

配置 Apache Maven pom.xml 文件,将 testng.xml 文件打包到 Appium 测试包的 *-tests.jar 文件根目录中。

注意: 在 Device Farm 的标准测试环境中,仅支持 testng.xml 文件中的部分功能。如果您的项目需要以下任何 testng.xml 文件功能,请使用自定义测试环境:

  • 优先级
  • 'include' 和 'exclude' 标签
  • 复杂分组
  • 在 testng.xml 文件中定义的任何 TestNG 参数

解决方法

1.    在 Apache Maven pom.xml 文件中,验证 testResources 标签是否包含以下目录:src/test/resources。如果未看到该目录,请将以下目录配置添加到该文件中:

<testResources>
     <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
     </testResource>
</testResources>

注意: 您可以指定另一个目录用于此过程。如果您选择使用其他目录,请确保在步骤 2 和 3 中使用同一个目录。

2.    将您的 testng.xml 文件添加到 pom.xml 文件的 src/test/resources 目录中。

3.    (可选)要在本地运行测试,请将以下 Maven Surefire 插件添加到 pom.xml 文件的**<plugins>**列表中。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12.4</version>
    <configuration>
       <suiteXmlFiles> <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile></suiteXmlFiles>
    </configuration>
</plugin>

注意: 最佳做法是在将项目上传到 Device Farm 之前在本地运行测试。Maven Surefire 插件允许您的 pom.xml 文件在本地测试期间引用您的 testng.xml 文件。

4.    运行以下 Maven 命令,清理 pom.xml 文件的 target 目录并重新打包测试文件:

$ mvn clean package -DskipTests=true

注意: DskipTests=True 值指定构建不会运行单元测试。

该命令会在 pom.xml 文件的 target 目录中创建一个 zip-with-dependencies.zip 文件。这个 zip 文件是您的 Appium 测试包,并在其 *-tests.jar 文件的根目录中包含您的 testng.xml 文件。

5.    通过运行以下命令之一,验证您的 testng.xml 文件是否位于 Appium 测试包的 *-tests.jar 文件根目录中:

要查看 *-tests.jar 文件中文件的快照,请运行以下 jar 命令:

$ jar tvf foo-1.0.0-SNAPSHOT-tests.jar

-or-

要将 *-tests.jar 文件中的所有文件解压缩到一个文件夹中,请运行以下解压缩命令:

$ unzip foo-1.0.0-SNAPSHOT-tests.jar -d sampletestsjarcontents

现在,您可以将测试包上传到 Device Farm,并使用您的应用程序和 zip-with-dependencies.zip 文件创建测试运行。

相关信息

在 AWS Device Farm 中对 Appium Java TestNG Web 应用程序进行故障排除

Working with custom test environments in AWS Device Farm(在 AWS Device Farm 中使用自定义测试环境)

AWS 官方已更新 5 年前