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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11519989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:24:33  来源:igfitidea点击:

testng - how to separate tests and supporting methods?

javatestng

提问by prockel

I am experimenting with testng. My goal is to have test methods in several classes and "supporting" methods for preparation and wrap-up of a bunch of tests in a separate class.

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

Another requirement is that in a test suite the supporting methods have to be called for multiple test parts. E.g. a first part containing testA and testB, and a second part containing testC and testD. This would result in the following steps:

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

support1, testA, testB, support2, support1, testC, testD, support2

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

My first approach that (partly) worked was to annotate all methods with @Test, use groups and define dependencies between groups, e.g. the test methods depend on a group "setUp", which is a group of one supporting method "support1" in the above example.

我(部分)工作的第一种方法是用 注释所有方法@Test,使用组并定义组之间的依赖关系,例如测试方法依赖于组“setUp”,这是上面示例中的一组支持方法“support1” .

The problem with this approach is that the supporting methods count as tests, so the generated report shows the wrong number of "real" tests.

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

The next idea was to use @BeforeGroupsand @AfterGroups, put the supporting methods in a group, and use group dependencies. The supporting methods should not be counted as tests any more. But I am stuck at the very beginning. For example I tried

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

@BeforeGroups (groups = {"setUp"})

for a setup method in class Support, and

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

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

in a "real" test class. This results in the following (simplyfied) error:

在“真正的”测试课中。这会导致以下(简化的)错误:

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

Why is group "setUp" nonexistent? Did I overlook something?

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

Or is there another approach which works?

还是有另一种有效的方法?

Thanks for your help!

谢谢你的帮助!

Edit:The tests are started with Ant and I use a testng.xml like this:

编辑:测试从 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>

回答by Patton

I got the glitch!!

我遇到了故障!!

The problem is with the annotation

问题出在注释上

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

Basically your error message is trying to say that there is no @Test method with groupname as setUp!! Coming to your question, the solution is to modify annotation for the test method as below

基本上,您的错误消息试图说明没有@Test 方法的 groupname 为setUp!! 来到你的问题,解决方案是修改测试方法的注释如下

 @Test(groups = { "TestA" })

And in the support method modify the annotation

并在支持方法中修改注解

  @BeforeGroups (groups = {"TestA"})

I ran a sample example with this set up

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

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");
    }

}

and with my Test class as

和我的测试课一样

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");
    }
}

and my testng.xml as shown below

和我的 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>

Now this is how the test runs: beforeGroups-> superTestA/superTestB ->afterGroupsand beforeGroups-> superTest -> afterGroupsand closes off

现在,这是怎么测试运行:beforeGroups-> superTestA/superTestB ->afterGroupsbeforeGroups-> superTest -> afterGroups与封闭

回答by prockel

I think I have come up with the solution I wanted.

我想我已经想出了我想要的解决方案。

What I need to use is @BeforeTestand @AfterTestinstead of @BeforeGroupsand @AfterGroups, respectively, in the support class:

我需要在支持类中分别使用@BeforeTestand@AfterTest而不是@BeforeGroupsand @AfterGroups

@BeforeTest(groups = {"setUp"})
public void beforeTest() {[...]}

@AfterTest( groups = {"tearDown"})
public void afterTest() {[...]}

In the test class:

在测试类中:

@Test(groups = { "TestA" })
public void testSomething() {[...]}

The dependsOnGroupsis gone, as in Patton's approach.

dependsOnGroups走了,正如巴顿的做法。

The testng.xml is unchanged compared to my question. I.e. the tests can be configured in the testng.xml file, without having to change java code.

与我的问题相比,testng.xml 没有变化。即测试可以在 testng.xml 文件中配置,而无需更改 java 代码。

Moreover, this solution also gets rid of another problemof the BeforeGroups approach, at least as supposed by Patton (@Patton I do not mean to offend you). With the latter a test using several test groups does not run as intended, because the beforeTest()method would be run before any of the groups. E.g. if you have the following test (extract of testng.xml):

此外,这个解决方案还摆脱了 BeforeGroups 方法的另一个问题,至少正如 Patton 所假设的那样(@Patton 我并不是要冒犯你)。对于后者,使用多个测试组的测试不会按预期运行,因为该beforeTest()方法将在任何组之前运行。例如,如果您有以下测试(testng.xml 的摘录):

    <groups>
        <run>
            <include name="TestA"/>
            <include name="TestB"/>
        </run>
    </groups>

... the resulting steps of execution are:
beforeTest(), TestA, beforeTest(), TestB, afterTest().

... 执行的结果步骤是:
beforeTest()、TestA、beforeTest()、TestB、afterTest()。

Using the solution with BeforeTest, you would have the following test:

将解决方案与 BeforeTest 结合使用,您将进行以下测试:

    <groups>
        <run>
            <include name="setUp" />
            <include name="TestA"/>
            <include name="TestB"/>
            <include name="tearDown"/>
        </run>
    </groups>

... the resulting steps of execution are:
setUp = beforeTest(), TestA, TestB, tearDown = afterTest().

... 执行的结果步骤是:
setUp = beforeTest()、TestA、TestB、tearDown = afterTest()。

回答by kavita

package com.test.MySample;

import org.testng.annotations.*; 

public class TestNGTest1 {    

    @BeforeTest   
    public void BeforeTest() {         
        System.out.println("@BeforeTest");
    }

    @BeforeClass    
    public void BeforeClass() {         
        System.out.println("@BeforeClass");
    }      

    @BeforeGroups  (groups = {"My group"}) 
    public void BeforeGroups() {       
        System.out.println("@BeforeGroups");
    }

    @BeforeGroups  (groups = {"My group1"}) 
    public void BeforeGroups1() {       
        System.out.println("@BeforeGroups1");
    }

    @AfterGroups  (groups = {"My group1"}) 
    public void AfterGroups1() {       
        System.out.println("@AfterGroups1");
    }

    @BeforeMethod    
    public void BeforeMethod() {         
        System.out.println("@BeforeMethod");     
    } 

    @Test(groups = {"My group"})     
    public void test1() {         
        System.out.println("test1");     
    } 

    @Test (groups = {"My group", "My group1"})    
    public void test2() {         
        System.out.println("test2");     
    }

    @AfterMethod    
    public void AfterMethod() {         
        System.out.println("@AfterMethod");     
    } 

    @AfterGroups  (groups = {"My group"}) 
    public void AfterGroups() {       
        System.out.println("@AfterGroups");
    }

    @AfterClass    
    public void AfterClass() {         
        System.out.println("@AfterClass");
    }  

    @AfterTest   
    public void AfterTest() {         
        System.out.println("@AfterTest");
    }
}