栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java练习(二十二):TestNG的简单使用流程(Eclipse中)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java练习(二十二):TestNG的简单使用流程(Eclipse中)

目录

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:在本测试中的所有测试方法上使用的默认超时时间

测试用例,name为必须属性。选择测试脚本可以从包,类,方法三个层级进行。

用例中包含的包,包中所有的方法都会执行,子标签为

测试包,name为必须属性

用例中包含的类,子标签为

测试类,其中属性name为必须属性

指定测试类中包含或排除的方法,子类为

指定需要测试的方法,name为必须属性

指定需要测试的方法,name为必须属性

指定测试用例中要运行或排除运行的分组,字标签为 , 下包含, 标签, 他们的name指定运行,不运行的分组
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/284965.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号