简体   繁体   English

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

[英]TestNG: @BeforeGroups gets called for every group entry even though I am running one group of tests

I see a problem in my testNG tests, the test is as follows. 我在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");
    }

} }

When I run the tests from testNG command line by 当我从testNG命令行运行测试时

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

(Assume that the test jar is in some libs folder) (假设测试罐在某些libs文件夹中)

The output I get is as follows 我得到的输出如下

Before Groups
Before Groups
Test
After Groups

I am not sure why the BeforeGroups is called twice even though I am only interested in running the test that's part of the smoketests group. 我不确定为什么为什么只调用两次BeforeGroups,即使我只对运行smoketests组的一部分测试感兴趣。

The problem doesn't happen if I only have smoketests group in the @Test directive, but I still don't understand the issue with @BeforeGroups with multiple groups in place. 如果我在@Test指令中只有smooketests组,则不会发生此问题,但是我仍然不了解具有多个组的@BeforeGroups的问题。

Getting the same issue while running the test suite. 在运行测试套件时遇到相同的问题。

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

Below is the test class: 下面是测试类:

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

So when I am runnting the testng.xml, getting the following result: 因此,当我运行testng.xml时,得到以下结果:

Before Group
m1
Before Group
m3
After Groups
m4
After Groups

The following is the result after changing the group name to regression. 以下是将组名更改为回归后的结果。

Before Group
m3
After Groups
m5
After Groups

@BefroreGroups and @Aftergroups get executed for both the group entries. 对两个组条目都执行@BefroreGroups和@Aftergroups。 Using TestNG 6.11 version. 使用的是TestNG 6.11版本。

Try use 尝试使用

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Maven 运行 testng 测试时不执行 BeforeGroups 方法 - BeforeGroups method is not executed when running testng tests by maven TestNG:使用 Testng XML 文件运行特定的组测试 - TestNG: Running specific group tests using Testng XML file TestNG Groups:我们可以包含两个组名并创建一个组来运行测试吗? - TestNG Groups: Can we include two group names and create one group to run tests? 除非在父类或本地类中,否则不会调用TestNG BeforeGroups - TestNG BeforeGroups not being called unless in parent class or local class 如果其中一项测试失败,如何继续在TestNG中运行测试 - How can I continue running tests in TestNG if one of the test fails 没有组的TestNG mvn运行测试 - TestNG mvn run tests without group 什么在TestNG中控制组测试的顺序? - What is controlling the order of group tests in TestNG? Selenium和TestNg并行运行并未将驱动程序附加到我们启动的每个浏览器,即使我使用ThreadLocal概念 - Selenium and TestNg parallel run is not attaching driver to every browser that we launched even though i used ThreadLocal concept 仅Gradle + TestNG运行指定的组 - Gradle + TestNG Only Running Specified Group 我们可以在Maven中的一个命令中执行Testng(Group)和黄瓜(Tag)测试吗 - Can we execute testng (group) and cucumber (tag) tests in one command in maven
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM