qyanqing
qyanqing

Reputation: 343

Can I use many times of @BeforeClass in one TestNG case?

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

    @BeforeClass
    protected void setUp2() throws Exception {}

    @Test
    public void queryAcquirerInfoById(){
    }
}

If I use twice '@BeforeClass' at one TestNG class,What is the order of the two methods? Can I assign the order of the two method?

Upvotes: 2

Views: 5063

Answers (2)

Dexterous Sikh
Dexterous Sikh

Reputation: 71

Yes you can add multiple @BeforeClass methods in a class. They will run in alphabetical order as per method name e.g. in following example the order of execution will be,

  1. setUp1()
  2. setUp2()
  3. queryAcquirerInfoById()

public class TestBase{

     @BeforeClass
     protected void setUp2() throws Exception {}

     @BeforeClass
     protected void setUp1() throws Exception {}

      @Test
      public void queryAcquirerInfoById(){
            }
        }

However, you can prioritize the execution of @BeforeClass methods using 'dependsOnMethods' option, like if you write


public class TestBase{

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

     @BeforeClass
     protected void setUp1() throws Exception {}

      @Test
      public void queryAcquirerInfoById(){
            }
        }

then setUp1() will run before setUp2()

Upvotes: 3

Ori Marko
Ori Marko

Reputation: 58882

It was asked in testng group, and suggested to use single method calling multiple methods with order you want:

use the simplest method possible:

@BeforeClass
public static void method1() {
  ...
  method2();
  method3();
}

In your case

@BeforeClass
protected void setUpAll() throws Exception {
 setUp();
 setUp2()
 }
protected void setUp() throws Exception {}

protected void setUp2() throws Exception {}

Upvotes: 0

Related Questions