java - testng - 如何分离测试和支持方法?

标签 java testng

我正在试验 testng。我的目标是在多个类中使用测试方法,并在单独的类中使用“支持”方法来准备和总结一堆测试。

另一个要求是在一个测试套件中,必须为多个测试部分调用支持方法。例如。第一部分包含 testA 和 testB,第二部分包含 testC 和 testD。这将导致以下步骤:

支持1,测试A,测试B,支持2,支持1,测试C,测试D,支持2

我的第一个(部分)有效的方法是用 @Test 注释所有方法,使用组并定义组之间的依赖关系,例如测试方法依赖于一组“setUp”,在上例中是一组支持方法“support1”。

这种方法的问题是支持方法算作测试,因此生成的报告显示错误的“真实”测试数量。

下一个想法是使用@BeforeGroups@AfterGroups,将支持的方法放在一个组中,并使用组依赖。支持方法不应再算作测试。但是我一开始就卡住了。 比如我试过

@BeforeGroups (groups = {"setUp"})

对于 Support 类中的设置方法,以及

@Test(groups = { "TestA" }, dependsOnGroups = { "setUp" })

在一个“真正的”测试类中。这会导致以下(简单的)错误:

[testng] DependencyMap::Method "TestClass.testSomething()[...]" depends on nonexistent group "setUp"

为什么组“setUp”不存在?我是不是忽略了什么?

或者还有其他有效的方法吗?

感谢您的帮助!

编辑: 测试是从 Ant 开始的,我使用这样的 testng.xml:

<test name="TestA">
    <groups>
        <run>
            <include name="setUp" />
            <include name="TestA"/>
            <include name="tearDown"/>
        </run>
    </groups>
    <classes>
        <class name="seleniumtest.test.technical.Support"/>
        <class name="seleniumtest.test.business.TestClassA"/>
    </classes>
</test>
<test name="TestB">
    <groups>
        <run>
            <include name="setUp" />
            <include name="TestB"/>
            <include name="tearDown"/>
        </run>
    </groups>
    <classes>
        <class name="seleniumtest.test.technical.Support"/>
        <class name="seleniumtest.test.business.TestClassB"/>
    </classes>
</test>

最佳答案

我遇到了错误!!

问题出在注释上

@Test(groups = { "TestA" }, dependsOnGroups = { "setUp" })

基本上,您的错误消息试图说明没有@Test 方法将groupname 设置为setUp!!针对您的问题,解决方案是修改测试方法的注释,如下所示

 @Test(groups = { "TestA" })

并在支持方法中修改注解

  @BeforeGroups (groups = {"TestA"})

我用这个设置运行了一个示例

public class TestSupport {

    @BeforeGroups(groups = { "sample","sample1" })
    public void beforeTest() {
        System.out.println("Before test");
    }

    @AfterGroups(groups = { "sample","sample1" })
    public void afterTest() {
        System.out.println("after test");
    }

}

和我的测试类

public class TestClassA {

    @Test(groups = { "sample" })
    public void superTestA() {
        System.out.println("This is the actual test");
    }

    @Test(groups = { "sample" })
    public void superTestB() {
        System.out.println("This is the another test under sample group");
    }

    @Test(groups = { "sample1" })
    public void superTest() {
        System.out.println("This is another test");
    }
}

和我的testng.xml如下图

<test name="sampletest" >
     <groups>
        <run>
            <include name="sample" />
            <include name="sample1" />

        </run>
    </groups>
    <classes>
        <class name="test.global.testng.TestClassA"/>
        <class name="test.global.testng.TestSupport"/>
    </classes>

  </test>

现在测试是这样运行的:beforeGroups-> superTestA/superTestB ->afterGroupsbeforeGroups-> superTest -> afterGroups 并关闭

关于java - testng - 如何分离测试和支持方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11519989/

相关文章:

testng - 如何在 Cypress 中添加测试用例分组

java - TestNG 测试外部对象的同步

java - 如何初始化 StringoriginalHandle = driver.getWindowHandle();一次?

java - 如何使用具有多种响应类型的 RestTemplate?

java - 从 web.xml 文件获取初始化参数。在 Eclipse 和 Tomcat 中

java - 用IntelliJ IDEA运行某个TestNG测试组

java - 在selenium网格中分配多少个线程

java - 从 Java 启动且代码为 `Runtime.getRuntime().exec` 的进程无法访问文件系统位置

java - 使用不同的表达式分割字符串

java - 如何在java中更新图形?