目录
(一)TestNG学习之路—HelloWorld入门
(二)TestNG学习之路—注解及属性概览
(三)TestNG学习之路—TestNG.xml/YAML
(四)TestNG学习之路—注解详述之@Test
(五)TestNG学习之路—注解详述之参数化
(六)TestNG学习之路—注解详述之@Factory
(七)TestNG学习之路—注解详述之忽略测试
(八)TestNG学习之路—注解详述之并发
(九)TestNG学习之路—失败测试重跑
(十)TestNG学习之路—编码执行TestNG
(十一)TestNG学习之路—BeanShell高级用法
(十二)TestNG学习之路—注解转换器
(十三)TestNG学习之路—方法拦截器
(十四)TestNG学习之路—TestNG监听器
(十五)TestNG学习之路—依赖注入
(十六)TestNG学习之路—测试报告
(十七)基于TestNG+Rest Assured+Allure的接口自动化测试框架
前言
该篇文章主要覆盖环境配置、testNG入门、执行测试三块内容。
工具
- IntelliJ IDEA
- TestNG 6.11
- Maven
pom.xml配置
新建一个maven项目,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>TestNG-Learning</groupId>
<artifactId>TestNG</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
</dependency>
</dependencies>
</project>
案例
TestNG的入门异常简单,如下所示,@BeforeClass注解方法相当于用例执行前的数据准备,@Test注解方法相当于用例执行,@AfterClass注解方法相当于用例执行后的数据销毁/恢复,其他注解后续文章再覆盖讲解。
import org.testng.annotations.*;
public class TestNGHelloWorld {
@BeforeClass
public void setUp(){
System.out.println("setUp!");
}
@Test
public void helloWorld(){
System.out.println("My first TestNG testCase!");
}
@AfterClass
public void tearDown(){
System.out.println("tearDown!");
}
}
执行结果如下:
setUp!
My first TestNG testCase!
tearDown!
===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
执行测试
用例编写完成后,可通过以下几种方式来执行测试。
-
ant构建
使用ant来执行测试,遵循步骤如下。
1、安装ant。
自行百度。
2、配置build.xml文件。
<project default="test">
<path id="cp">
<pathelement location="lib/testng-6.11.jar"/>
<pathelement location="lib/jcommander-1.48.jar"/>
<pathelement location="build"/>
</path>
<taskdef name="testng" classpathref="cp"
classname="org.testng.TestNGAntTask" />
<target name="test">
<testng classpathref="cp" >
<classfileset dir="build" includes="TestNGHelloWorld.class"/>
</testng>
</target>
</project>
3、切换到build.xml文件目录,执行ant命令。
D:\IntelliJ_IDEA_workspace\TestNG>ant
Buildfile: D:\IntelliJ_IDEA_workspace\TestNG\build.xml
test:
[testng] setUp!
[testng] My first TestNG testCase!
[testng] tearDown!
[testng]
[testng] ===============================================
[testng] Ant suite
[testng] Total tests run: 1, Failures: 0, Skips: 0
[testng] ===============================================
[testng]
BUILD SUCCESSFUL
Total time: 2 seconds
各目录结构如下所示。
务必下载jcommander.jar,否则报错如下。
D:\IntelliJ_IDEA_workspace\TestNG>ant
Buildfile: D:\IntelliJ_IDEA_workspace\TestNG\build.xml
test:
[testng] java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException
[testng] at java.lang.Class.getDeclaredMethods0(Native Method)
[testng] at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
[testng] at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
[testng] at java.lang.Class.getMethod0(Class.java:3018)
[testng] at java.lang.Class.getMethod(Class.java:1784)
[testng] at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
[testng] at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
[testng] Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.ParameterException
[testng] at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[testng] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[testng] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
[testng] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[testng] ... 7 more
[testng] Error: A JNI error has occurred, please check your installation and try again
[testng] The tests failed.
如果有包路径,需把包路径添加上去。
<project default="test">
<path id="cp">
<pathelement location="lib/testng-6.8.7.jar"/>
<pathelement location="lib/jcommander-1.48.jar"/>
<pathelement location="build"/>
</path>
<taskdef name="testng" classpathref="cp"
classname="org.testng.TestNGAntTask" />
<target name="test">
<testng classpathref="cp" >
<!--includes需加上包路径-->
<classfileset dir="build" includes="com/demo/test/TestHello.class"/>
</testng>
</target>
</project>
-
xml执行
新建testNG.xml文件(可参考《IDEA自动生成TestNG的testng.xml》文章),右键run即可。如下xml结构是根据class来执行测试,其他结构配置后续再详解。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default Suite">
<test name="TestNG">
<classes>
<class name="TestNGHelloWorld"/>
</classes>
</test> <!-- TestNG -->
</suite> <!-- Default Suite -->
-
命令行执行
步骤:
1、环境变量配置
2、编译
3、自定义classpath(第2步的TestNGHelloWorld.class路径)
java -classpath "%classpath%;D:\IntelliJ_IDEA_workspace\TestNG\build" org.testng.TestNG testNG.xml
执行xml:
D:\IntelliJ_IDEA_workspace\TestNG\lib>java -classpath "%classpath%;D:\IntelliJ_IDEA_workspace\TestNG\build" org.testng.TestNG testNG.xml
[TestNG] Running:
D:\IntelliJ_IDEA_workspace\TestNG\lib\testNG.xml
setUp!
My first TestNG testCase!
tearDown!
===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
执行class:
D:\IntelliJ_IDEA_workspace\TestNG\lib>java -classpath "%classpath%;D:\IntelliJ_IDEA_workspace\TestNG\build" org.testng.TestNG -testclass TestNGHelloWorld
[TestNG] Running:
Command line suite
setUp!
My first TestNG testCase!
tearDown!
===============================================
Command line suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
还可以将命令行开关放在文本文件中,例如d:\test.txt。,并告诉TestNG使用该文件检索其参数,执行结果与上面的一致。
d:\test.txt 内容:testNG.xml
编译后的class文件在D:\IntelliJ_IDEA_workspace\TestNG\lib下
D:\IntelliJ_IDEA_workspace\TestNG\lib>java -Dtestng.test.classpath org.testng.TestNG @d:\test.txt
[TestNG] Running:
D:\IntelliJ_IDEA_workspace\TestNG\lib\testNG.xml
setUp!
My first TestNG testCase!
tearDown!
===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
命令行执行更多的参数可参考官网。