2012-07-16 48 views
1

我爲使用Eclipse +硒的webdriver + TestNG的運行測試方法陸續在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

請指引我達到預期的效果。 非常感謝。

+0

你使用的是Java嗎? – 2015-02-20 05:43:17

回答

2

您還可以使用 「優先級」 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

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> 

Remeber,如果你使用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"> 

原因是打開完整日誌。