【发布时间】:2021-02-18 15:39:01
【问题描述】:
我正在使用属于特定组的测试方法运行一个测试套件。
以下是 Selenium 代码:
public class BaseClass
{
@BeforeMethod(onlyForGroups = {"P1"})
public void bmeth1()
{
System.out.println("Before Method1 called");
}
@BeforeMethod(onlyForGroups = {"P2"})
public void bmeth2()
{
System.out.println("Before Method2 called");
}
@BeforeMethod(onlyForGroups = {"P3"})
public void bmeth3()
{
System.out.println("Before Method3 called");
}
@AfterMethod(onlyForGroups = {"P1"})
public void ameth1()
{
System.out.println("After Method1 called");
}
@AfterMethod(onlyForGroups = {"P2"})
public void ameth2()
{
System.out.println("After Method2 called");
}
@AfterMethod(onlyForGroups = {"P3"})
public void ameth3()
{
System.out.println("After Method3 called");
}
}
public class TC_003 extends BaseClass
{
@Test(groups = {"P1"})
public void tCase6()
{
System.out.println("Inside testcase 6");
}
@Test(groups = {"P2"})
public void tCase7()
{
System.out.println("Inside testcase 7");
}
@Test(groups = {"P3"})
public void tCase8()
{
System.out.println("Inside testcase 8");
}
}
下面是 testng.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="10">
<test name="Test1">
<groups>
<run>
<include name=".*"/>
</run>
</groups>
<classes>
<class name="testing.TC_003"/>
</classes>
</test>
</suite>
实际输出:
Inside testcase 6
Inside testcase 7
Inside testcase 8
预期输出:
Before Method1 called
Inside testcase 6
After Method1 called
Before Method2 called
Inside testcase 7
After Method2 called
Before Method3 called
Inside testcase 8
After Method3 called
测试方法已执行,但@BeforeMethod/@AfterMethod 没有执行。仅当我们在 testng.xml 文件中包含某些组时才会出现此问题。但是如果我们在 testng.xml 文件中排除某些组或不使用组标签,那么它们就会被执行。
按照here 的建议,当前的解决方法是使用 alwaysRun=true 标志和 onlyForGroups 标志。但是如果我们应用这个变通方法,并且如果前面/父配置方法中有任何 SkipException,那么即使要跳过测试方法,也会强制执行 @BeforeMethod/@AfterMethod 方法。当前面的/父配置方法失败时,记录了类似的问题here。
【问题讨论】:
标签: java selenium selenium-webdriver testng