是否可以从Excel驱动TestNG测试用例?

时间:2017-05-19 19:31:08

标签: selenium selenium-webdriver testng

假设我已经在Selenium中使用TestNG编写了4个测试用例,并希望运行任何2个运行模式的测试用例"是"在Excel中提供。请让我知道,我如何设计我的代码,以便任何运行模式的测试用例"是"从TestNG测试用例列表中执行。 Screenshot from Excel

public class classname {
    @Test public void testcase1() {
        //Test steps here..
    }
    @Test public void testcase2() {
        //Test steps here..
    }
    @Test public void testcase3() {
        //Test steps here..
    }
    @Test public void testcase4() {
        //Test steps here..
    }
}

1 个答案:

答案 0 :(得分:0)

IAnnotationTransformer可以帮助您:

public class MyTransformer implements IAnnotationTransformer {

  private final List<String> activatedTest;

  public MyTransformer() {
      activatedTest = new ArrayList<>();
      // 1. Open Excel file
      // 2. Iterate on lines an add all activated test cases according to the naming convention of test method names (lower case + remove spaces)
      // With you example, the list will containt: "testcase1", "testcase3"
  }

  public void transform(ITest annotation, Class<?> testClass,
      Constructor testConstructor, Method testMethod) {
    if (activatedTest.contains(testMethod.getName())) {
      annotation.setEnabled(true);
    } else {
      annotation.setEnabled(false);
    }
  }
}