2017-02-24 23 views
0

我有3個類,每個類有3個測試。通過test.xml文件基於多個類定義的優先級運行測試

1類

@Test(priority = 1) 
public void testA1() { 
    System.out.println("testA1"); 
} 

@Test(priority = 2) 
public void testA2() { 
    System.out.println("testA2"); 
} 

@Test(priority = 3) 
public void testA3() { 
    System.out.println("testA3"); 
} 

2類

@Test(priority = 1) 
public void testB1() { 
    System.out.println("testB1"); 
} 

@Test(priority = 2) 
public void testB2() { 
    System.out.println("testB2"); 
} 

@Test(priority = 3) 
public void testB3() { 
    System.out.println("testB3"); 
} 

3類

@Test(priority = 1) 
public void testC1() { 
    System.out.println("testC1"); 
} 

@Test(priority = 2) 
public void testC2() { 
    System.out.println("testC2"); 
} 

@Test(priority = 3) 
public void testC3() { 
    System.out.println("testC3"); 
} 

這是我的XML文件的代碼。

<test verbose="2" name="hello" group-by-instances="true"> 
    <classes> 
     <class name="Class1"></class> 
     <class name="Class2"></class> 
     <class name="Class3"></class> 
    </classes> 
</test> 

這裏是我的回答 testA1 testB1 testC1 testA2 testB2 testC2 testA3 testB3 testC3

但是我預期的答案是 testA1 testA2 testA3 testB1 testB2 testB3 testC1 testC2 testC3

在此先感謝有這方面的幫助。

+0

你如何運行你的套件? –

+0

我正在運行maven框架,並直接在intellij,這兩種情況下相同的錯誤 –

回答

1

看到的行爲是預期的行爲。

事實上,priority更重要的是group-by-instanceshttps://github.com/cbeust/testng/blob/master/CHANGES.txt#L48) ,這就是爲什麼TestNG的尊重priority而不是group-by-instances

實現你預期的行爲,你有一個更重要的命令功能,以取代priority,像dependsOnMethods

@Test 
public void testA1() { 
    System.out.println("testA1"); 
} 

@Test(dependsOnMethods = "testA1") 
public void testA2() { 
    System.out.println("testA2"); 
} 

@Test(dependsOnMethods = "testA2") 
public void testA3() { 
    System.out.println("testA3"); 
} 

由於要求在評論,如果你真的想要一個「優先班級沒有強烈的依賴性​​「,你可以自己製作a method interceptor,你可以根據需要訂購方法。在僞代碼中,類似於:

public class PriorityOnClassOrder implements IMethodInterceptor { 

    public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { 
    // 1. Group by instance/class 
    Map<Class<?>, List<IMethodInstance>> map = ... 
    for (IMethodInstance method : methods) { 
     map.get(method.getInstance().getClass()).add(method); 
    } 

    List<IMethodInstance> result = ... 
    // 2. Order methods from an instance/clas according to their priority 
    for(Map.Entry entry : map.entries()) { 
     List<IMethodInstance> m = entry.value(); 
     Collections.sort(m, new Comparator<IMethodInstance>() { 
     public int compare(IMethodInstance o1, IMethodInstance o2) { 
      return o1.getMethod().getPriority() - o2.getMethod().getPriority() 
     } 
     }); 
     result.addAll(m); 
    } 

    // 3. Return the result 
    return result; 
    } 
} 
+0

感謝您的輸入。但我想運行獨立測試。在這種情況下,取決於方法不適用於我。有沒有其他方法可以用這個優先級數字和xml文件進行分類? –

+0

不是。在這種情況下取​​消優先權。 – juherr

+0

我已將此鏈接指向同一個問題。 「http://stackoverflow.com/questions/26632241/priority-in-testng-with-multiple-classes」。 –

0

正如您可能已經注意到的那樣,優先級標誌影響整體而不是單個類。最簡單的方法是提高二級優先級。

@Test(priority = 4) 
public void testA1() { 
    System.out.println("testA1"); 
} 

@Test(priority = 5) 
public void testA2() { 
    System.out.println("testA2"); 
} 

@Test(priority = 6) 
public void testA3() { 
    System.out.println("testA3"); 
} 

此外,你可以把單個類放在單一的,我認爲它甚至更好,如果你想單獨測試域。

<test name="Test1" verbose="3" > 
    <classes> 
    <class name="tests.NewTest"></class> 
    </classes> 
    </test> <!-- Test --> 
    <test name="Test2" verbose="3" > 
    <classes> 
    <class name="tests.NewTest2"></class> 
    </classes> 
    </test> 

verbose標誌。我在調試過程中幾乎不推薦這樣做

+0

在第一種情況下,我將很難設法優先考慮每一個測試..總的來說,我將有400-500個測試用例。所以這不會用於我。 –

+0

第二種情況要好得多。但我還有一個選項,我在這裏添加.. –

+0

你說你有400-500個測試用例,就你而言,你在下面發佈的結果將需要包括所有方法,一個接一個。也許你應該考慮制定計劃。只需通過XmlSuite,XmlClass,XmlTest創建TestNG對象並添加類方法等,然後將其聚合到主對象中即可。 –

0

最後我得到了問題的解決方案。

請參閱下面的XML代碼。

<classes> 
      <class name="Class1"> 
       <methods> 
        <include name="testA1" /> 
        <include name="testA2" /> 
        <include name="testA3" /> 
       </methods> 
      </class> 
      <class name="Class2"> 
       <methods> 
        <include name="testB1" /> 
        <include name="testB2" /> 
        <include name="testB3" /> 
       </methods> 
      </class> 
      <class name="Class3"> 
       <methods> 
        <include name="testC1" /> 
        <include name="testC2" /> 
        <include name="testC3" /> 
       </methods> 
      </class> 

這會給你預期的結果。另外,它也更容易管理。就好像我們希望看到我的所有測試或刪除一些測試,我可以看看test.xml