Vladislav Romarov
Vladislav Romarov

Reputation: 57

TestNG - How can I set up the order of running @Test methods through testng.xml?

I have a 30 @Test methods and 2 Java methods in my @Test class. I need to run these 2 Java methods after concrete @Test method,for example, TestMethod5(). How can I do that ?

For instance:

@Test
public void TestMethod5() {

/* compiled code */

}

public void Method1(){/* compiled code */};
public void Method2{/* compiled code */};

Need 2 methods:

1) with testng.xml
2) with Intellij IDEA

Note: @BeforeMethod and @AfterMethod works only with extends classname command. These 2 Java methods check the rendering of buttons&labels with assertTrue() method so I don`t want to mess my base class with them.


Yeah,these methods are part of test(and future similar tests too),but I can`t just copy the content of them and paste in each method I want...Then @Test methods will be messed(large code).I just need to check the rendering of labels and butons in these methods:

 public void method1_ButtonsTest() {

  assertTrue1();
  assertTrue2();

 }


 public void method2_LabelsTest() {

  assertTrue3();
  assertTrue4();
 }


@Test
public void Test1();

@Test
public void Test2();

@Test
public void Test3();

@Test
public void Test4();

@Test
public void Test5();

@Test
public void Test6();


@Test
public void Test7();

@Test
public void Test8();

Upvotes: 0

Views: 615

Answers (2)

TosaInu
TosaInu

Reputation: 3

If you don't want to modify the base class and also don't want to call the methods directly in the test then you could add another @AfterMethod to the test class and only run the methods for the test cases you call out, then call the @AfterMethod in the base class, something like this:

@AfterMethod(alwaysRun = true)
public void postTestCase(ITestResult _result) {
    if (_result.getMethod().getMethodName().equals("Test1")){
          System.out.println("Do something here...");
    }
    //Call the @AfterMethod in the base class
    super.postTestCase(_result);
}

Upvotes: 0

matt burns
matt burns

Reputation: 25420

Why can't you just call those methods?

@Test public void testMethod5() {
    ...
    method1();
    method2();
}

Upvotes: 1

Related Questions