如何根据条件禁用TestNG测试

目前有一种方法可以根据条件禁用TestNG测试

我知道你当前可以在TestNG中禁用测试:

@Test(enabled=false, group={"blah"}) public void testCurrency(){ ... } 

我想根据条件禁用相同的测试,但不知道如何。 像这样的东西:

 @Test(enabled={isUk() ? false : true), group={"blah"}) public void testCurrency(){ ... } 

任何人都知道这是否可行。

你有两个选择:

  • 实现注释转换器 。
  • 使用BeanShell 。

您的注释转换器将测试条件,然后覆盖@Test注释以在不满足条件时添加属性“enabled = false”。

更简单的选择是在检查条件的方法上使用@BeforeMethod注释。 如果你想跳过测试,那么只需抛出一个SkipException 。 喜欢这个:

 @BeforeMethod protected void checkEnvironment() { if (!resourceAvailable) { throw new SkipException("Skipping tests because resource was not available."); } } 

我知道有两种方法可以控制TestNG中的“禁用”测试。

值得注意的区别是,SkipException将突破所有后续测试,而实际上IAnnotationTransformer根据您指定的条件使用Reflection来取消单个测试。 我将解释SkipException和IAnnotationTransfomer。

SKIP例外示例

 import org.testng.*; import org.testng.annotations.*; public class TestSuite { // You set this however you like. boolean myCondition; // Execute before each test is run @BeforeMethod public void before(Method methodName){ // check condition, note once you condition is met the rest of the tests will be skipped as well if(myCondition) throw new SkipException(); } @Test(priority = 1) public void test1(){} @Test(priority = 2) public void test2(){} @Test(priority = 3) public void test3(){} } 

IAnnotationTransformer示例

有点复杂,但背后的想法是一个称为reflection的概念。

Wiki – http://en.wikipedia.org/wiki/Reflection_(computer_programming)

首先实现IAnnotation接口,将其保存在* .java文件中。

 import java.lang.reflect.Constructor; import java.lang.reflect.Method; import org.testng.IAnnotationTransformer; import org.testng.annotations.ITestAnnotation; public class Transformer implements IAnnotationTransformer { // Do not worry about calling this method as testNG calls it behind the scenes before EVERY method (or test). // It will disable single tests, not the entire suite like SkipException public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod){ // If we have chose not to run this test then disable it. if (disableMe()){ annotation.setEnabled(false); } } // logic YOU control private boolean disableMe()){ } 

然后在你的测试套件java文件中,在@BeforeClass函数中执行以下操作

 import org.testng.*; import org.testng.annotations.*; /* Execute before the tests run. */ @BeforeClass public void before(){ TestNG testNG = new TestNG(); testNG.setAnnotationTransformer(new Transformer()); } @Test(priority = 1) public void test1(){} @Test(priority = 2) public void test2(){} @Test(priority = 3) public void test3(){} 

最后一步是确保在build.xml文件中添加侦听器。 我最终看起来像这样,这只是build.xml中的一行:

  

第三个选项也可以是TestNG的假设假设 – 当假设失败时,将指示TestNG忽略测试用例,因此不会执行它。

  • 使用@Assumption注释
  • 使用AssumptionListener使用Assumes.assumeThat(…)方法

您可以使用此示例: 示例

在使用@BeforeMethod注释的方法中抛出SkipException对我来说不起作用,因为它跳过了我的测试套件的所有剩余测试,而不管是否为这些测试抛出了SkipException

我没有彻底调查,但我找到了另一种方法:在@Test注释上使用dependsOnMethods属性:

 import org.testng.SkipException; import org.testng.annotations.Test; public class MyTest { private boolean conditionX = true; private boolean conditionY = false; @Test public void isConditionX(){ if(!conditionX){ throw new SkipException("skipped because of X is false"); } } @Test public void isConditionY(){ if(!conditionY){ throw new SkipException("skipped because of Y is false"); } } @Test(dependsOnMethods="isConditionX") public void test1(){ } @Test(dependsOnMethods="isConditionY") public void test2(){ } }