Running test methods one after another in TestNG

2019-02-26 11:24发布

问题:

I m using Eclipse + Selenium WebDriver + TestNG

This is my class structure :

class1
{
@test (invocation count =4)
method1()

@test (invocation count =4)
method2()

}

my testing.xml file :

<classes>
<class name="tests.class1">
<methods>
<include name="method1" />
<include name="method2" />
</methods>
</class>
</classes>

When running through my current testing.xml, the order of the tests is: method1 method1 method1 method1 method2 method2 method2 method2

But I want the tests to be run in the following order: method1 method2 method1 method2 method1 method2 method1 method2

Please guide me to achieve the desired result. Thanks a lot.

回答1:

Look up "dependsOnGroups" in the documentation.



回答2:

You can also use "priority" of TestNG as:

@Test(priority = -19)
public void testMethod1(){
//some code
}
@Test(priority = -20)
public void testMethod2(){
//some code
}

[Note: The priority for this test method. Lower priorities will be scheduled first]

So, in the above example testMethod2 will be executed first as -20 less than -19

You can visit for more details: http://testng.org/doc/documentation-main.html#annotations



回答3:

Of course there is a lot of methods to make it. An Example:

import java.lang.reflect.Method;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

public class ABC {

    @Test(invocationCount=12)
    public void uno(){
        System.out.println("UNO");
    }
    @AfterMethod()
    public void sec(Method m){
        if(m.getName().equals("uno"))
        System.out.println("SEC");
    }
}

And suite:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  <test name="Test" parallel="none" >
    <classes>
      <class name="aaa.ABC">
          <methods>
              <include name="uno">
          </methods>
      </class>
    </classes>
  </test> <!-- Test -->
</suite>

Remeber, if you use dependsOnMethod then thoes method will be executed after all invocation. For example:

@Test(invocationCount=3)
public void uno(){
    System.out.println("UNO");
}
@Test(dependsOnMethods={"uno"})
public void sec(){

    System.out.println("SEC");
}

with:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  <test name="Test" parallel="none" >
    <classes>
      <class name="aaa.ABC">
          <methods>
              <include name="uno"/>
                 <include name="sec"/>
          </methods>
      </class>
    </classes>
  </test> <!-- Test -->
</suite>

will give:

UNO
UNO
UNO
SEC

===============================================
Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================

And if you test your tests please use verbose ="3" in suite conf. example:

<suite name="Suite" parallel="none" verbose="3">

Cause this is turning on full logs.