在TestNG中一个接一个地运行测试方法

时间:2012-07-16 11:55:14

标签: methods selenium-webdriver parallel-processing testng

我正在使用Eclipse + Selenium WebDriver + TestNG

这是我的班级结构:

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

@test (invocation count =4)
method2()

}

我的testing.xml文件:

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

当运行我当前的testing.xml时,测试的顺序是: 方法1 方法1 方法1 方法1 方法2 方法2 方法2 方法2

但我希望测试按以下顺序运行: 方法1 方法2 方法1 方法2 方法1 方法2 方法1 方法2

请指导我达到预期的效果。 非常感谢。

3 个答案:

答案 0 :(得分:3)

documentation中查找“dependsOnGroups”。

答案 1 :(得分:3)

您还可以将TestNG的“优先级”用作:

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

[注意:此测试方法的优先级。将优先安排较低的优先事项]

因此,在上面的示例中,testMethod2将首先执行为-20小于-19

您可以访问以获取更多详细信息: http://testng.org/doc/documentation-main.html#annotations

答案 2 :(得分:0)

当然有很多方法可以做到。一个例子:

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");
    }
}

套房:

<?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>

记住,如果你使用dependsOnMethod那么thoes方法将在所有调用之后执行。 例如:

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

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

使用:

<?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>

会给:

UNO
UNO
UNO
SEC

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

如果您测试测试,请在套件conf中使用verbose ="3"。例如:

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

因为这会打开完整日志。