intellij-idea - IntelliJ IDEA + TestNG : Run a method before each test in a group

标签 intellij-idea testng

我正在学习将 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 记住您之前使用过的组,如果您使用了尚未记住的组,则会显示消息“X 组未定义”。只需在未定义的组上按 alt + Enter 即可记住它。


  • 资源:
  • TestNG bug tracker - BeforeMethod and AfterMethod groups support broken
  • TestNG Mailing list - Order of execution for configuration methods involving groups
  • TestNG Mailing list - Before and After for groups
  • talios.com - New Inspections for the IntelliJ TestNG Plugin
  • 关于intellij-idea - IntelliJ IDEA + TestNG : Run a method before each test in a group,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3602404/

    相关文章:

    selenium - 截取屏幕截图时出现异常 null 且配置失败 : @AfterMethod teardown

    java - 无法在Kotlin中解析字符串到文件

    scala - 替换为 .lengthCompare 警告

    java - intellij 不解析 JSP 代码检查或自动完成中的 el 变量

    intellij-idea - IntelliJ for Android xml 文件中的自动代码格式化

    testing - testNG 中的测试套件结果依赖性

    java - Intellij,为内部类自动生成公共(public)静态最终类 EDIT_CLASS_NAME { }

    xml - TestNG XML配置文件DTD "test"标签错误

    java - 为什么 Try-With-Resources 不会每次都创建一个新实例

    Java、Selenium webdriver TestNG "Pls Help Me"