[英]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.