IntelliJ IDEA + TestNG:在组中的每个测试之前运行一个方法
-
25-09-2019 - |
题
我正在学习将 TestNG 用于 IntelliJ IDEA 9。
据我了解,在一个名为的组中进行测试的一种方法是 name
就是对其进行注释 @Test(group = "name")
. 。要在每次测试之前运行方法,请将其注释为 @BeforeMethod
.
在我的测试设置中,我希望有一个方法仅在特定组中的每个测试之前运行。所以有一个方法 beforeA
在组中每个测试之前运行 A
, , 一个方法 beforeB
在每个之前运行 B
测试等。
示例代码:
public class TestExample
{
@BeforeMethod(groups = "A")
public void beforeA()
{
System.out.println("before A");
}
@BeforeMethod(groups = "B")
public void beforeB()
{
System.out.println("before B");
}
@Test(groups = "A")
public void A1()
{
System.out.println("test A1");
}
@Test(groups = "A")
public void A2()
{
System.out.println("test A2");
}
@Test(groups = "B")
public void B1()
{
System.out.println("test B1");
}
@Test(groups = "B")
public void B2()
{
System.out.println("test B2");
}
}
我期望输出像
before A
test A1
before A
test A2
before B
test B1
before B
test B2
但我得到以下信息:
before A
before B
before A
before B
test A2
before A
before B
before A
before B
test B1
===============================================
test B2
===============================================
Custom suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================
IntelliJ IDEA 已突出显示我的所有注释,并显示消息“A 组未定义”或“B 组未定义”。
我究竟做错了什么?
解决方案
- 列表的顺序不太好,这是 intelliJ 的错。在命令行或使用 Maven 运行测试,顺序将是正确的。
@BeforeMethod
和@AfterMethod
似乎与团体破碎了。- IntelliJ 会记住您之前使用过的组,如果您使用尚未记住的组,则会显示消息“Group X is undefined”。只是按 替代 + 进入 在一个未定义的组上记住它。
资源 :
其他提示
我问的Intellij解决它。请检查问题: http://youtrack.jetbrains.net/issue/IDEA-67653 我们需要把票投给它,JetBrains公司将修复它
@BeforeMethod(groups =...)
不应该之前的组的每个方法运行。
它会在一个类中的每个方法之前运行。所不同的是,它只会属于一个特定群体,仅此而已。 见文档
如所提到的由TEH EMPRAH @BeforeMethod不假设每其中属于同一组,因为它方法之前运行。
为了做到这一点,你必须配置你正确的testng.xml。为了您的预期输出应该看起来像这样
<suite....>
<test name="A">
<groups>
<run>
<include name="A"/>
</run>
</groups>
<classes>
<class name="...TestExample"/>
</classes>
</test>
<test name="B">
<groups>
<run>
<include name="B"/>
</run>
</groups>
<classes>
<class name="...TestExample"/>
</classes>
</test>
</suite>
不隶属于 StackOverflow