testng - 如何根据之前的@Test测试结果在TestNG中启用@Test方法
问题描述
我在下面的类及其@Test 方法中有一个条件:
class myClass{
@Test
public void test1(){..}
@Test
public void test2(){..}
@Test
public void test3(enabled=false){..}
}
在这里,当上述 @Tests(test1 或 test2) 中的任何一个失败时,我想执行 @Test test3 。
有问题的是,测试结果,我的意思是结果(通过或失败)。不是他们返回的值。
解决方案
可以通过 boolean 变量进行设置并抛出SkipException
它会中断所有后续测试执行:
class myClass{
// skip variable
boolean skipCondition;
// Execute before each test is run
@BeforeMethod
public void before(Method methodName){
// condition befor execute
if(skipCondition)
throw new SkipException();
}
@Test(priority = 1)
public void test1(){..}
@Test(priority = 2)
public void test2(){..}
@Test(priority = 3)
public void test3(){..}
}
另一个是实现IAnnotationTransformer
,更复杂。
public class ConditionalTransformer implements IAnnotationTransformer {
// calls before EVERY test
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod){
// add skip ckeck
if (skipCkeck){
annotation.setEnabled(false);
}
}
}
推荐阅读
- graphql - 如何在本地模式中使用拼接模式中的类型
- sql - 产品级别 date 和 date-1 之间的销售差异
- java - 你如何命名一个公开序列化和反序列化/编组的方法的类
- sql-server - 了解 SQL 引发的转换错误
- javafx - Javafx 在舞台上播放视频然后关闭它并打开新舞台
- xml - 我如何执行 XML 和 DTD?
- kubernetes - 使用 Kubernetes/OpenShift API 从 JOB 名称中查找创建的 POD 名称
- wordpress - wordpress wpbakery 突然在前端显示简码
- php - 记录无法在 PHP 中删除
- java - 从 JPanel 之外的类对象访问变量