【问题标题】:Maven, testng - include and exclude testsMaven, testng - 包含和排除测试
【发布时间】:2019-05-16 21:18:06
【问题描述】:

我有一个场景,我需要通过包含和排除某些测试组来运行我的测试。

考虑以下场景

import org.testng.annotations.Test;

public class GroupingTest {

@Test(groups = {"bat"})
public void batTest(){
    System.out.println("Am bat");
}
@Test(groups = {"p1"})
public void p1Test(){
    System.out.println("Am p1");
}
@Test(groups = {"p2"})
public void p2Test(){
    System.out.println("Am p2");
}
@Test(groups = {"bat","p3"})
public void batp3Test(){
    System.out.println("Am bat p3 ");
}
}

在这里,我如何只运行“bat”测试组,它不应该运行也是“33”的“bat”测试。 在上述情况下,当我运行时..它应该只打印“Am bat” 我怎样才能实现它?有什么建议吗?

【问题讨论】:

标签: maven testng


【解决方案1】:

基本上有两种方法可以完成这项工作。

方法 #1:使用 beanshell 选择器

  1. 确保您使用的是最新发布的 TestNG 版本。从今天起是7.0.0-beta1
  2. 添加对 beanshell 的依赖(以下是使用 maven 时的操作方式)
<dependency>
  <groupId>org.apache-extras.beanshell</groupId>
  <artifactId>bsh</artifactId>
  <version>2.0b6</version>
</dependency>
  1. 将您的 TestNG 套件 xml 更改为如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="53799427_suite" parallel="false" verbose="2">
  <method-selectors>
    <method-selector>
      <script language="beanshell">
        <![CDATA[
                whatGroup = System.getProperty("group");
                shouldRun = Arrays.equals(new String[]{whatGroup}, testngMethod.getGroups());
                return shouldRun;
        ]]>
      </script>
    </method-selector>
  </method-selectors>
  <test name="53799427_test">
    <classes>
      <class name="com.rationaleemotions.stackoverflow.qn53799427.TestClassSample"/>
    </classes>
  </test>
</suite>

这里的测试类com.rationaleemotions.stackoverflow.qn53799427.TestClassSample 看起来与您分享的示例完全相同。

现在,当您通过传递 JVM 参数 -Dgroup=bat 来运行此套件 xml 文件时,您将看到如下所示的输出(之后的内容)

...
... TestNG 7.0.0-beta1 by Cédric Beust (cedric@beust.com)
...
Am bat
PASSED: batTest

===============================================
    53799427_test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

===============================================
53799427_suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

方法 #2:使用自定义方法选择器

  1. 确保您依赖于 TestNG 7.0.0-SNAPSHOT(我之所以这么说是因为 TestNG 中存在一个错误,导致此功能无法正常工作。我继续将其作为 GITHUB-1985 的一部分进行修复。但是它今天尚未发布)
<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>7.0.0-SNAPSHOT</version>
</dependency>

要使用快照版本,您可能需要将如下所示的&lt;repository&gt; 标签添加到您的 pom 文件中。

<repositories>
    <repository>
      <id>sonatype-nexus-snapshots</id>
      <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </repository>
</repositories>
  1. 现在创建一个自定义的org.testng.IMethodSelector 实现,如下所示:
import java.util.Arrays;
import java.util.List;
import org.testng.IMethodSelector;
import org.testng.IMethodSelectorContext;
import org.testng.ITestNGMethod;

public class FilteringMethodSelector implements IMethodSelector {

  @Override
  public boolean includeMethod(
      IMethodSelectorContext context, ITestNGMethod method, boolean isTestMethod) {
    String whichGroup = System.getProperty("group", "all");
    if ("all".equalsIgnoreCase(whichGroup)) {
      return true;
    }
    boolean isEqual = Arrays.equals(new String[]{whichGroup}, method.getGroups());
    if (context != null) {
      context.setStopped(true);
    }

    return isEqual;
  }

  @Override
  public void setTestMethods(List<ITestNGMethod> testMethods) {}
}
  1. 创建如下所示的 testng 套件 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="53799427_suite" parallel="false" verbose="2">
  <method-selectors>
    <method-selector>
      <selector-class
        name="com.rationaleemotions.stackoverflow.qn53799427.FilteringMethodSelector" priority="0"/>
    </method-selector>
  </method-selectors>
  <test name="53799427_test">
    <classes>
      <class name="com.rationaleemotions.stackoverflow.qn53799427.TestClassSample"/>
    </classes>
  </test>
</suite>

这里的测试类com.rationaleemotions.stackoverflow.qn53799427.TestClassSample 看起来与您共享的示例完全相同。

现在,当您通过传递 JVM 参数 -Dgroup=bat 运行此套件 xml 文件时,您将看到如下所示的输出(之后的内容)

...
... TestNG 7.0.0-SNAPSHOT by Cédric Beust (cedric@beust.com)
...
Am bat
PASSED: batTest

===============================================
    53799427_test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

===============================================
53799427_suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 2017-01-09
    相关资源
    最近更新 更多