依赖测试、忽略测试
1、依赖测试
使用属性dependsOnMethods:当前方法依赖的方法列表。
1、创建DependTest类
脚本代码:
package com.demo.depend;
import org.testng.annotations.Test;
public class DependTest {
@Test
public void testCase1() {
System.out.println("testCase1");
}
@Test(dependsOnMethods = { "testCase1" })
public void testCase2() {
System.out.println("testCase2");
}
@Test(dependsOnMethods = { "testCase2" })
public void testCase3() {
System.out.println("testCase3");
throw new RuntimeException();
}
@Test(dependsOnMethods = { "testCase3" })
public void testCase4() {
System.out.println("testCase4");
}
}
2、执行脚本(DependTest鼠标右键Run As--->TestNG Test)。
3、执行结果:
控制台打印结果信息:
testCase1方法执行成功。
testCase2方法依赖testCase1方法,testCase2执行成功。
testCase3方法依赖testCase2方法,虽然testCase2执行成功,但testCase3方法里抛异常,则执行失败。
testCase4方法依赖testCase3方法,因为testCase3方法执行失败,所以testCase4方法则跳过不执行。
TestNG结果报告:
2、忽略测试
使用属性enabled:当前类的方法/方法是否被激活。
enabled = true(被激活)
enabled = false(不被激活,即为忽略)
1、创建IgnoreTest类
脚本代码:
package com.demo.ignore;
import org.testng.annotations.Test;
public class IgnoreTest {
@Test
public void ignore1() {
System.out.println("ignore1 执行");
}
@Test(enabled = false)
public void ignore2() {
System.out.println("ignore2 不执行");
}
@Test(enabled = true)
public void ignore3() {
System.out.println("ignore3 执行");
}
}
2、执行脚本(IgnoreTest鼠标右键Run As--->TestNG Test)。
3、执行结果:
控制台打印结果信息:
ignore1方法虽然没有设置属性enabled = true,但是默认情况下是可执行的。
ignore2方法设置属性为enabled = false,所以没有执行。
ignore3方法设置属性为enabled = true,所以执行。
TestNG结果报告:
本文分享自 AllTests软件测试 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有