java接口自动化框架图_Java接口自动化之TestNG单元测试框架(一)

本文详细介绍了TestNG框架中@Test注解的使用,包括enable属性控制测试执行、dependsOnMethods实现方法依赖、groups进行分组测试、timeOut设置超时以及多线程测试。通过实例展示了如何利用这些特性进行接口自动化测试。
摘要由CSDN通过智能技术生成

@Test是TestNG 最基本的注解,用来将方法标注为测试方法。接下来会介绍@Test注解的常用属性。

1enable 测试方法是否执行

enable默认是true, 表示执行这个方法,如果设置为false,则在运行时不会执行这个测试方法。import org.testng.annotations.Test;

public class TestDemo {

@Test(enabled = true)

public  void testDemo1(){

System.out.println("这是testDemo1");

}

@Test(enabled = false)

public  void testDemo2(){

System.out.println("这是testDemo2");

}

}

运行结果为:

format,png

从上图可以看出,enabled为true的方法执行了,enabled为false的方法未执行。

2dependsOnMethods 依赖方法

在依赖的方法运行完成之后运行当前方法,如果依赖方法测试不通过,那么当前方法也不会继续运行了。

依赖的方法可以有多个,格式为:@Test(dependsOnMethods = { "method1" , “method2” })。

修改testDemo1代码,运行时抛出异常,代码如下:import org.testng.annotations.Test;

public class TestDemo {

@Test()

public  void testDemo1(){

int i=10;

System.out.println(i/0);

System.out.println("这是testDemo1");

}

@Test(dependsOnMethods = {"testDemo1"})

public  void testDemo2(){

System.out.println("这是testDemo2");

}

}

运行结果为:

format,png

从上图可以看出testDemo1方法运行失败后,testDemo2方法被忽略运行了。

3groups 分组

在运行时,一个组的方法会一起运行,然后再运行下一个组的方法;

格式为:@Test(groups = "groupName")。import org.testng.annotations.Test;

import org.testng.annotations.BeforeGroups;

import org.testng.annotations.AfterGroups;

public class TestDemo {

@BeforeGroups(groups = "server")

public void beforeGroupsOnServer() {

System.out.println("服务端组运行前执行的方法。。。");

}

@BeforeGroups(groups = "client")

public void beforeGroupsOnClient() {

System.out.println("客户端组运行前执行的方法。。。");

}

@Test(groups = "server")

public void test1() {

System.out.println("server test1 run ....");

}

@Test(groups = "server")

public void test2() {

System.out.println("server test2 run ....");

}

@Test(groups = "client")

public void test3() {

System.out.println("client test3 run ....");

}

@Test(groups = "client")

public void test4() {

System.out.println("client test4 run ....");

}

@AfterGroups(groups = "server")

public void afterGroupsOnServer() {

System.out.println("服务端组运行之后执行的方法。。。");

}

@AfterGroups(groups = "client")

public void afterGroupsOnClient() {

System.out.println("客户端组运行之后执行的方法。。。");

}

}

运行结果为:服务端组运行前执行的方法。。。

server test1 run ....

server test2 run ....

服务端组运行之后执行的方法。。。

客户端组运行前执行的方法。。。

client test3 run ....

client test4 run ....

客户端组运行之后执行的方法。。。

===============================================

Default Suite

Total tests run: 4, Failures: 0, Skips: 0

4timeOut 超时属性

格式:@Test(timeOut = 3000) 设置超时时间,单位为毫秒。

import org.testng.annotations.Test;

public class TestDemo {

// 单位为毫秒值,3秒内没有响应,就运行失败,反之成功

@Test(timeOut = 3000)

public void testSuccess() throws InterruptedException {

Thread.sleep(2000);

}

// 单位为毫秒值,2秒内没有响应,就运行失败,反之成功

@Test(timeOut = 2000)

public void testFail() throws InterruptedException {

Thread.sleep(3000);

}

}

运行结果为:

format,png

从上图可以看出,testFail方法因为超时运行失败。

5 多线程测试

格式:@Test(invocationCount = 10,threadPoolSize = 3)。

参数说明:

invocationCount:线程调用的次数,默认1次。

threadPoolSize:线程池,需与invocationCount组合使用。

接下来编写一个方法,定义3个线程,执行方法10次。import org.testng.annotations.Test;

public class TestDemo {

@Test(invocationCount = 10,threadPoolSize = 3)

public void testDemo(){

System.out.println("---------");

System.out.printf("Thread Id : %s%n",Thread.currentThread().getId());

}

}

运行结果为:[TestNG] Running:

---------

---------

Thread Id : 13

---------

Thread Id : 12

Thread Id : 14

---------

Thread Id : 13

---------

Thread Id : 14

---------

Thread Id : 12

---------

Thread Id : 12

---------

Thread Id : 13

---------

Thread Id : 14

---------

Thread Id : 12

===============================================

Default Suite

Total tests run: 10, Failures: 0, Skips: 0

===============================================

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值