TestNG:即使我正在运行一组测试,也会为每个组条目调用@BeforeGroups

问题描述 投票:0回答:2

我在testNG测试中看到一个问题,测试如下。

public class testNG {

    @BeforeGroups(groups = {"smoketests", "functionaltests"})
    public void before() {
        System.out.println("Before Groups");
    }

    @Test(groups = {"smoketests", "functionaltests"})
    public void test() {
        System.out.println("Test");
    }

    @AfterGroups(groups = {"smoketests", "functionaltests"})
    public void after() {
        System.out.println("After Groups");
    }

}

当我从testNG命令行运行测试时

java -cp:libs / * org.testng.TestNG -testjar libs / testNGLib.jar -groups smoketests

(假设测试jar在某些libs文件夹中)

我得到的输出如下

Before Groups
Before Groups
Test
After Groups

我不确定为什么BeforeGroups被调用两次,即使我只对运行smoketests组的测试感兴趣。

如果我在@Test指令中只有smoketests组,那么问题就不会发生,但是我仍然不理解@BeforeGroups有多个组的问题。

java testng
2个回答
0
投票

试试用

@BeforeSuite(alwaysRun = true)而不是@BeforeGroups(groups = {"smoketests", "functionaltests"})

@AfterSuite(alwaysRun = true)而不是@AfterGroups(groups = {"smoketests", "functionaltests"})


0
投票

在运行测试套件时遇到同样的问题。

<suite name="Test Suite">
    <test name="GroupTest">
          <groups>
              <run>
                <include name="sanity"/>
              </run>
          </groups>
          <classes>
              <class name="SampleTest"/>
          </classes>
    </test>
</suite>

以下是测试类:

public class SampleTest {

    @BeforeGroups(groups = {"sanity","regression"})
    void beforeGroup(){
        System.out.println("Before Group");
    }

    @AfterGroups(groups = {"sanity","regression"})
    void afterGroups(){
        System.out.println("After Groups");
    }

    @Test(groups = {"sanity"})
    void m1(){
        System.out.println("m1");
    }

    @Test(groups = {"sanity","regression"})
    void m3(){
        System.out.println("m3");
    }
    @Test(groups = {"sanity"})
    void m4(){
        System.out.println("m4");
    }
    @Test(groups = {"regression"})
    void m5(){
        System.out.println("m5");
    }
    @Test
    void m6(){
        System.out.println(Sample.class.getName());
    }
}   

所以当我运行testng.xml时,得到以下结果:

Before Group
m1
Before Group
m3
After Groups
m4
After Groups

以下是将组名更改为回归后的结果。

Before Group
m3
After Groups
m5
After Groups

同时为两个组条目执行@BefroreGroups和@Aftergroups。使用TestNG 6.11版本。

© www.soinside.com 2019 - 2024. All rights reserved.