首页 > 解决方案 > 我可以在一个 TestNG 案例中多次使用 @BeforeClass 吗?

问题描述

public class TestBase{
    @BeforeClass
    protected void setUp() throws Exception {}

    @BeforeClass
    protected void setUp2() throws Exception {}

    @Test
    public void queryAcquirerInfoById(){
    }
}

如果我在一个 TestNG 类中使用两次“@BeforeClass”,这两种方法的顺序是什么?我可以指定这两种方法的顺序吗?</p>

标签: testng

解决方案


是的,您可以在一个类中添加多个 @BeforeClass 方法。它们将根据方法名称按字母顺序运行,例如在以下示例中,执行顺序为,

  1. 设置1()
  2. 设置2()
  3. queryAcquirerInfoById()

public class TestBase{

     @BeforeClass
     protected void setUp2() throws Exception {}

     @BeforeClass
     protected void setUp1() throws Exception {}

      @Test
      public void queryAcquirerInfoById(){
            }
        }

但是,您可以使用 'dependsOnMethods' 选项优先执行 @BeforeClass 方法,就像您编写


public class TestBase{

     @BeforeClass (dependsOnMethods = { "setUp1" })
     protected void setUp2() throws Exception {}

     @BeforeClass
     protected void setUp1() throws Exception {}

      @Test
      public void queryAcquirerInfoById(){
            }
        }

然后 setUp1() 将在 setUp2() 之前运行


推荐阅读