2

我正在使用 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运行时,测试的顺序是:method1 method1 method1 method1 method2 method2 method2 method2

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

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

4

3 回答 3

4

dependsOnGroups文档中查找“ ” 。

于 2012-07-17T02:28:45.730 回答
3

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

于 2012-08-07T09:37:39.460 回答
1

当然,制作的方法有很多。一个例子:

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>

记住,如果你使用dependsOnMethodthen thes 方法将在所有调用后执行。例如:

@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
===============================================

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

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

因为这是打开完整的日志。

于 2015-02-20T13:28:12.333 回答