TestNG之使用构建工具(八)
在前面介绍的体系中,介绍的知识体系都是针对一个测试用例的管理以及执行,但是我们的执行更多是基于testng.xml的
配置文件而言,企业案例里面我们更多的是和maven以及ant这些构建工具来整合,以及和持续交付的平台进行整合,来完成
自动化测试的测试效率从而达到工程效率的提升。本文章中,我们主要会说到构建自动化测试工具以及构建自动化测试的过程
,和构建工具ant以及maven。所以建议先在本地搭建好ant和maven的构建工具,这些就不详细的说明构建测试工具的搭建了。
生成自动化可以定义为编写源代码的脚本和自动编译、运行、部署和打包源代码的过程。这适用于各种软件语言,无论类型
如何。每个生成工具作为生成自动化的一部分支持某些常见任务,例如清理、编译、执行、报告和发布。每个工具都有实现完成
该任务的不同方法。对于某些工具,某些任务是预定义的,但对于其他工具,提供了实用程序,用户可以使用它们手动配置它们。
这地方我们说的构建工具就只介绍下maven构建工具的使用。搭建好环境后,输入mvn,就会显示如下的信息。在开始之前,我
们先来熟悉下maven的各个组件部分,具体如下:
- 项目:在 Maven 中,这也定义为配置的开始,并包含与项目相关的相关依赖项和配置。
- 插件:它们是作为 Maven 的一部分提供的实用程序,用于实现构建过程中涉及的不同类型的步骤。
- 依赖项:它们用于配置项目依赖项。用户可以配置相应项目所依赖的 API、JARS 和版本。
如我们来看我们目前系统的pom.xml文件内容,内容具体如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>ngApp</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.9</version> <scope>compile</scope> </dependency> <!--WebDriver的测试框架--> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-server</artifactId> <version>3.141.59</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.14.1</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
我们进入到Java项目的路径下,执行mvn test来构建执行我们的自动化测试用例,具体输出信息如下:
[INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for org.example:ngApp:jar:1.0-SNAPSHOT [WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-surefire-plugin @ line 59, column 21 [WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-surefire-plugin @ line 96, column 21 [WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-surefire-plugin @ line 107, column 21 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] -------------------------< org.example:ngApp >-------------------------- [INFO] Building ngApp 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- jacoco-maven-plugin:0.8.4:prepare-agent (default) @ ngApp --- [INFO] argLine set to -javaagent:/Users/liwangping/.m2/repository/org/jacoco/org.jacoco.agent/0.8.4/org.jacoco.agent-0.8.4-runtime.jar=destfile=/Applications/code/workSpace/ngApp/target/jacoco.exec [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ngApp --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ ngApp --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ngApp --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /Applications/code/workSpace/ngApp/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ ngApp --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ ngApp --- [INFO] Tests are skipped. [INFO] [INFO] --- jacoco-maven-plugin:0.8.4:report (report) @ ngApp --- [INFO] Skipping JaCoCo execution due to missing execution data file. [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.166 s [INFO] Finished at: 2021-01-15T15:46:57+08:00 [INFO] ------------------------------------------------------------------------ localhost:ngApp liwangping$ clear localhost:ngApp liwangping$ mvn test [INFO] Scanning for projects... [INFO] [INFO] -------------------------< org.example:ngApp >-------------------------- [INFO] Building ngApp 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ngApp --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ ngApp --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ngApp --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /Applications/code/workSpace/ngApp/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ ngApp --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.14.1:test (default-test) @ ngApp --- [INFO] Surefire report directory: /Applications/code/workSpace/ngApp/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running TestSuite test method executing on thread with id:12 test method executing on thread with id:11 test method executing on thread with id:13 test method executing on thread with id:13 test method executing on thread with id:12 test method executing on thread with id:11 Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.276 sec Results : Tests run: 6, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.429 s [INFO] Finished at: 2021-01-15T15:53:15+08:00 [INFO] ------------------------------------------------------------------------

我们已成功创建一个 Maven 脚本,用于编译和运行通过 Maven 的 TestNG 测试。Maven 有一个预定义的步骤,在执行之前编译代码
测试时,我们尝试运行我们的测试。上一个屏幕截图显示了通过 Maven 生成编译和运行测试所涉及的不同步骤。插件maven-sure-plugin
用于配置和执行测试。此处,该插件用于配置 testng .xml测试和生成测试报告。插件 maven 编译器 -插件用于帮助编译代码并使用特定
的 JDK 版本进行编译。我们开始运行所有的测试用例,修改testng.xml的配置文件,运行测试包下所有的测试类里面的测试方法,testng.xml
的配置文件内容为:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <!--<suite name="UnitSuite">--> <suite name="Simple Test" parallel="methods" thread-count="1"> <test name="packageTest"> <packages> <package name="test.*"></package> </packages> </test> </suite>
再看输出的信息,如下所示:

我们再来看看naven的其他信息,具体如下:
- outputdir :用于生成报表的输出目录
- parallel :使用方法、类或测试的并行模式
- threadCount :可用于运行的线程数
- timeOut:执行单个测试的最大耗时时间(毫秒)
我们可以在pom.xml里面把这些信息整合进去,新增了运行的线程数和parallel,完善后的pom.xml配置文件内容为:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>ngApp</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.9</version> <scope>compile</scope> </dependency> <!--WebDriver的测试框架--> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-server</artifactId> <version>3.141.59</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.14.1</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> <threadCount>1</threadCount> <parallel>tests</parallel> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
再次执行,也可以使用IDE的maven,双击test来进行执行,如下所示:

执行后IDE的控制台输出的信息,具体如下:

关于构建工具与TestNG测试框架的整合就介绍到这里,感谢您的阅读,后续会持续的介绍。

浙公网安备 33010602011771号