使用注释转换器禁用@Test
Disable @Test with Annotation Transformer
我有两个 class:第 1 页和第 2 页。从 page2 我想在 page1 中调用一个 运行 的变量,该变量将被 运行domly 选择为“内部”或“外部”,并从中决定是否执行一些 @test我的 page2 class 中的方法。我不想要任何 skipExceptions。
我想要下面这样的东西。如果位置等于“外部”,则预计该页面不会显示某些功能。其中,我不想让那些缺失特性的测试用例是 运行.
if(page1.location.equals("External")){
annotation.setEnabled(false);
}
我正在按照此处解释的示例进行操作:How to disable TestNG test based on a condition 但我仍然不太能理解它。我如何将其应用于特定的 @Test
而不是整个 class?
注释中的要求无法使用注释转换器。这是因为在任何测试 运行 之前变压器是 运行。这样做是为了在测试执行开始之前完成注释。
字段m_enabled
用于判断测试方法是否可以运行。即使您可以在测试执行期间将 m_enabled
更改为 false
,但这也无济于事,因为测试执行已经达到了 运行 的测试已经完成并且m_enabled
之后不检查。
因此对于此要求,禁用使用不同测试结果的测试方法使得禁用的测试不会出现在测试报告中是不可能的。 但是,根据你的评论“如果我生成一个介于1或2之间的随机数怎么办。如果1,则执行测试路径A。如果2 ,然后执行测试路径 B,在这种情况下,您可以使用转换器来 enable/disable 某些方法,因为此逻辑独立于任何测试结果。为此,您可以按如下方式定义转换器:
public class MyTransformer implements IAnnotationTransformer {
private final List<String> pathOneMethods = List.of("testA","testB");
private final List<String> pathTwoMethods = List.of("testB","testC");
private final Random r = new Random(); //java.util
private final List<String> selectedPath;
public MyTransformer() {
selectedPath = (r.nextBoolean()) ? pathOneMethods : pathTwoMethods;
}
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
if(testMethod != null && !selectedPath.contains(testMethod.getName())) {
annotation.setEnabled(false);
}
}
}
注释转换器不能作为 @Listeners
的一部分包含在代码中。它必须在 testng.xml
中指定为 <suite>
标签内的 <listeners>
。
<suite>
................
................
<listeners>
<listener class-name = "com.code.MyTransformer"></listener>
</listeners>
................
................
</suite>
我有两个 class:第 1 页和第 2 页。从 page2 我想在 page1 中调用一个 运行 的变量,该变量将被 运行domly 选择为“内部”或“外部”,并从中决定是否执行一些 @test我的 page2 class 中的方法。我不想要任何 skipExceptions。
我想要下面这样的东西。如果位置等于“外部”,则预计该页面不会显示某些功能。其中,我不想让那些缺失特性的测试用例是 运行.
if(page1.location.equals("External")){
annotation.setEnabled(false);
}
我正在按照此处解释的示例进行操作:How to disable TestNG test based on a condition 但我仍然不太能理解它。我如何将其应用于特定的 @Test
而不是整个 class?
注释中的要求无法使用注释转换器。这是因为在任何测试 运行 之前变压器是 运行。这样做是为了在测试执行开始之前完成注释。
字段m_enabled
用于判断测试方法是否可以运行。即使您可以在测试执行期间将 m_enabled
更改为 false
,但这也无济于事,因为测试执行已经达到了 运行 的测试已经完成并且m_enabled
之后不检查。
因此对于此要求,禁用使用不同测试结果的测试方法使得禁用的测试不会出现在测试报告中是不可能的。 但是,根据你的评论“如果我生成一个介于1或2之间的随机数怎么办。如果1,则执行测试路径A。如果2 ,然后执行测试路径 B,在这种情况下,您可以使用转换器来 enable/disable 某些方法,因为此逻辑独立于任何测试结果。为此,您可以按如下方式定义转换器:
public class MyTransformer implements IAnnotationTransformer {
private final List<String> pathOneMethods = List.of("testA","testB");
private final List<String> pathTwoMethods = List.of("testB","testC");
private final Random r = new Random(); //java.util
private final List<String> selectedPath;
public MyTransformer() {
selectedPath = (r.nextBoolean()) ? pathOneMethods : pathTwoMethods;
}
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
if(testMethod != null && !selectedPath.contains(testMethod.getName())) {
annotation.setEnabled(false);
}
}
}
注释转换器不能作为 @Listeners
的一部分包含在代码中。它必须在 testng.xml
中指定为 <suite>
标签内的 <listeners>
。
<suite>
................
................
<listeners>
<listener class-name = "com.code.MyTransformer"></listener>
</listeners>
................
................
</suite>