java - 通过方法和组执行 TestNG

标签 java testng

我喜欢执行按方法和组过滤的测试。这在 TestNG 中可能吗?

例如。我有以下两个带有两种方法的 TestNG 类。

Class - SampleJ1
Methods - Test1(group=sanity), Test2(group=regression), Test3, Test4, Test5
Class - SampleJ2
Methods - Test1(group=sanity), Test2(group=regression), Test3, Test4, Test5

我的自动化框架生成 TestNG XML 文件。如果我使用以下数据创建 XML 文件,它应该只执行上述方法的完整组。

<groups>
  <run>
    <include name="sanity" />
  </run>
</groups>
<test thread-count="12" name="Browser">
  <classes>
    <class name="SampleJ1">
      <include method="Test1"/>
      <include method="Test2"/>
      <include method="Test3"/>
      <include method="Test4"/>
    </class>
    <class name="SampleJ2">
      <include method="Test1"/>
      <include method="Test2"/>
      <include method="Test3"/>
      <include method="Test4"/>
    </class>
  </classes>
</test>

如果可行,请告诉我。

最佳答案

据我了解,您想过滤测试方法以按其名称和所属组执行。 首先,在纯 TestNG 中没有针对此的特殊解决方案(否则像@juherr 这样的一些 TestNG 专家会回答)。使用 InvokedMethodListener 的实现跳过测试方法会留下您可能不想要的日志。

我看到两个选项。

在第一个中你可以实现一个 @Factory可以使用 Set<Method> 实例化您的测试类的方法或 Set<String>作为构造函数的参数。在每个测试类中,您将检查要执行的方法(或其 String 表示)是否在 Set 中. @Before注释或 beforeInvocation方法将处理逻辑,无论是否执行给定的方法。此外,您的 testng.xml将指定 <groups>要运行的元素。同样,这可能会留下 Skipped: x 的缺点。在您的日志和报告中。

第二种选择是充分利用 Maven 及其 Surefire Plugin . (如果您不使用任何构建工具,如 Gradle 或 Maven,那么我相信您应该尝试一下)。当您使用 Maven 进行测试时,您可以指定必须执行哪些组和哪些测试方法。 你的pom.xml应该很像下面这样:

<!-- modelVersion, groupId, etc. -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
        <!-- (...) -->
    </plugins>
    <!-- (...) -->
</build>
<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.10</version>
    </dependency>
    <!-- (...) -->
</dependencies>

比如说,你有一个这样的测试类:

public class TestClass {

    @Test(groups = {"firstGroup"})
    public void firstMethod(Method method) {
        assertEquals(1, 1);
        System.out.println(method.getName());
    }

    @Test(groups = {"secondGroup"})
    public void secondMethod(Method method) {
        assertEquals(1, 1);
        System.out.println(method.getName());
    }

    @Test(groups = {"secondGroup"})
    public void thirdMethod(Method method) {
        assertEquals(1, 1);
        System.out.println(method.getName());
    }

    @Test(groups = {"secondGroup"})
    public void fourthMethod(Method method) {
        assertEquals(1, 1);
        System.out.println(method.getName());
    }
}

在你的testng.xml您可以放置​​要执行的组的信息:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default Suite Name">
    <groups>
        <run>
            <include name="secondGroup"/>
        </run>
    </groups>
    <test name="Default Test Name">
        <classes>
            <class name="TestClass"/>
        </classes>
    </test>
</suite>

然后执行一个 Maven 目标来决定执行哪些方法:

mvn clean -Dtest=TestClass#thirdMethod+secondMethod test

可以找到多个方法和多个类的语法here .
输出:

Running TestClass
secondMethod
thirdMethod
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

第四个方法没有被执行,尽管它属于secondGroup . 如果有很多方法要执行,你可以为它编写一个自定义的 bash 脚本。

不可能(至少对我而言)用 Maven 过滤组,用 TestNG 过滤方法。

关于java - 通过方法和组执行 TestNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42390240/

相关文章:

java - 不支持的格式异常 : No suitable ImageReader found for source data

java - 扫描文件中的重复字符串

java - Java从FTP服务器递归下载所有文件夹

java - TestNG ExpectedExceptions 抛出 TestException

java - Ant-TestNG 类路径问题 : getResource can't find the file

java - 尝试在 Selenium Java 中制作屏幕截图时出现 NPE

java - 无法停止 AsyncTask 中的 ProgressDialog

java - 条件 log4j.xml 模式

maven - 如何在 Jenkins 中生成 Selenium HTML 报告?

java - TestNG 中的超时测试