目录
1. 什么是TestNG?
2. Eclipse中,TestNG的环境搭建
3. TestNG用例的创建和运行
3.1 创建一个TestNG用例
3.2 根据实际需求编写TestNG用例,示例代码如下
3.3 运行结果
4. xml配置文件(TestNG的打包测试)
4.1 TestNG如何实现打包测试?
4.2 xml配置文件示例
4.3 在配置好的xml文件中,右键选择TestNG Suite,完成多用例的执行
4.4 补充:xml配置文件中常用的标签
1. 什么是TestNG?
TestNG是一个开源自动化测试框架,灵感来源于JUnit和NUnit, TestNG还涵盖了整个核心的JUnit4功能,还引入一些新功能。
TestNG和JUnit4最大的不同就是通过配置文件对用例进行运行时的配置管理。另外还有依赖测试和多线程测试。
2. Eclipse中,TestNG的环境搭建
安装TestNG插件
3. TestNG用例的创建和运行
3.1 创建一个TestNG用例
在某个工程目录下,右键,new, others...,TestNG class
选择路径,包名,注解等信息, 点Finish
生成一个TestNG用例。
3.2 根据实际需求编写TestNG用例,示例代码如下
package com.my.testng.demo;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeClass;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
public class Case1 {
@Test
public void f1() {
System.out.println("f1");
assertEquals("a","b");
}
@Test
public void f2() {
System.out.println("f2");
assertEquals("a","a");
}
@BeforeMethod
public void beforeMethod(){
System.out.println("beforeMethod1");
}
@AfterMethod
public void afterMethod() {
System.out.println("afterMethod1");
}
@BeforeClass
public void beforeClass() {
System.out.println("BeforeClass1");
}
@AfterClass
public void afterClass(){
System.out.println("afterClass1");
}
@BeforeTest
public void beforeTest() {
System.out.println("beforeTest1");
}
@AfterTest
public void afterTest() {
System.out.println("afterTest1");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("beforeSuite1");
}
@AfterSuite
public void afterSuite() {
System.out.println("afterSuite1");
}
}
3.3 运行结果
4. xml配置文件(TestNG的打包测试)
4.1 TestNG如何实现打包测试?
TestNG通过配置文件来完成Suite功能,即:一次运行多个测试类,在项目根目录testng下,编辑xml文件,testng.xml,xml文件名自定义。
4.2 xml配置文件示例
(1)TeseNG用例1:Case1.java
package com.my.testng.demo;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeClass;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
public class Case1 {
@Test
public void f1() {
System.out.println("f1");
assertEquals("a","b");
}
@Test
public void f2() {
System.out.println("f2");
assertEquals("a","a");
}
@BeforeMethod
public void beforeMethod(){
System.out.println("beforeMethod1");
}
@AfterMethod
public void afterMethod() {
System.out.println("afterMethod1");
}
@BeforeClass
public void beforeClass() {
System.out.println("BeforeClass1");
}
@AfterClass
public void afterClass(){
System.out.println("afterClass1");
}
@BeforeTest
public void beforeTest() {
System.out.println("beforeTest1");
}
@AfterTest
public void afterTest() {
System.out.println("afterTest1");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("beforeSuite1");
}
@AfterSuite
public void afterSuite() {
System.out.println("afterSuite1");
}
}
(2)TeseNG用例2:Case2.java
package com.my.testng.demo;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeClass;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
public class Case2 {
@Test
public void f3() {
System.out.println("f3");
assertEquals("a","b");
}
@Test
public void f4() {
System.out.println("f4");
assertEquals("a","a");
}
@BeforeMethod
public void beforeMethod(){
System.out.println("beforeMethod1");
}
@AfterMethod
public void afterMethod() {
System.out.println("afterMethod1");
}
@BeforeClass
public void beforeClass() {
System.out.println("BeforeClass1");
}
@AfterClass
public void afterClass(){
System.out.println("afterClass1");
}
@BeforeTest
public void beforeTest() {
System.out.println("beforeTest1");
}
@AfterTest
public void afterTest() {
System.out.println("afterTest1");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("beforeSuite1");
}
@AfterSuite
public void afterSuite() {
System.out.println("afterSuite1");
}
}
(3)xml配置文件:testng.xml
4.3 在配置好的xml文件中,右键选择TestNG Suite,完成多用例的执行
结果如下:
4.4 补充:xml配置文件中常用的标签
name属性:套件的名称,必须属性
verbose:运行的级别或详细程度
parallel: 是否运行多线程来运行这个套件
thread-count:如果启用多线程,用于指定开户的线程数
annotations:在测试中使用的注释类型
time-out:在本测试中的所有测试方法上使用的默认超时时间



