2008-08-12 53 views
11

我有一個代表TestNG中的數據庫測試的基類,我想指定從這個類擴展的所有類都是一個「db-test」組,但是我發現這似乎不可能。我曾嘗試@Test註解:我可以在TestNG測試用例上指定一個類寬的組嗎?

@Test(groups = { "db-test" }) 
public class DBTestBase { 
} 

然而,這不起作用,因爲@Test標註將努力使一堆的方法進入測試,並警告/錯誤彈出在日食時的測試跑。

所以,我試圖禁用的測試,所以至少組分配:

@Test(enabled = false, groups = { "db-test" }) 
public class DBTestBase { 
} 

但這時@BeforeTest(以及其他類似的說明)還可以獲得殘疾...這當然不是我想。

我想用某種方式來註釋一個類作爲特定類型的組,但在TestNG中看起來不太可能。有沒有人有任何其他想法?

+0

剛剛添加了一個可能的解決方案到您的TestNG'班級範圍'組的問題。你可以檢查一下,並告訴我,如果這是正確的方向? – VonC 2008-11-08 23:31:39

回答

2

答案是通過自定義org.testng。IMethodSelector

它的includeMethod()可以排除任何我們想要的方法,如公共未註釋的方法。

然而,註冊自定義的Java MethodSelector,必須將其添加到任何TestRunner的,這意味着你需要自己定製TestRunner的管理的的XMLTest實例。

但是,要建立一個自定義的TestRunner,你需要註冊一個TestRunnerFactory,通過-testrunfactory選項。

但-testrunfactory永遠不會被TestNG的類考慮...所以你也需要定義一個自定義TestNG的類:

  • ,以覆蓋配置(圖)方法,
  • 所以實際上你可以設置TestRunnerFactory
  • TestRunnerFactory這將爲你創造一個定製的TestRunner,
  • 的TestRunner將設置到的XMLTest例如自定義XMLMethodSelector
  • XMLMethodSelector將構建一個自定義IMethodSelector
  • IMethodSelector將排除您所選擇的任何TestNG方法!

好的...這是一場噩夢。但它也是一個代碼挑戰,所以它必須有一定的挑戰性;)

所有的代碼可在DZone snippets

照例一個代碼的挑戰:

  • 一個Java類(和相當多的內部類)
  • 複製粘貼類在「源極/測試」目錄(因爲包是'測試「)
  • 來看,它(不需要參數)從斯通

更新:

我打算接受這個,因爲它聽起來非常接近我最終做的事情,但我想我會加上我所做的。

基本上,我創建了一個組註釋,其行爲與Test(和其他)註釋的組屬性相似。

然後,我創建了一個GroupsAnnotationTransformer,它使用IAnnotationTransformer查看所有正在定義的測試和測試類,然後修改測試以添加組,這與羣組排除和包含完美配合。

修改構建以使用新的註釋轉換器,它完美地工作!

那麼......有一點需要注意的是,它不會將組添加到非測試方法中......因爲當時我這樣做了,還有另一個註釋轉換器可以讓你轉換任何東西,但它以某種方式沒有包含在TestNG中,我出於某種原因使用了...所以最好將你之前/之後的註釋方法設置爲alwaysRun = true ...這對我來說已經足夠了。

最終的結果是我可以做的:

@Groups({ "myGroup1", "myGroup2"}) 
public class MyTestCase { 
    @Test 
    @Groups("aMethodLevelGroup") 
    public void myTest() { 
    } 
} 

而且我做了子類和一切變壓器工作。

+0

對不起,花了這麼久...我有點停止看了一會兒:-) – 2009-03-16 18:31:34

2

TestNG將使用@Test註解運行類中的所有公共方法。也許你可以改變你不想讓TestNG運行的方法是非公開的

0

你可以在方法級別指定@Test註釋,以便獲得最大的靈活性。

public class DBTestBase { 

    @BeforeTest(groups = "db-test") 
    public void beforeTest() { 
     System.out.println("Running before test"); 
    } 

    public void method1() { 
     Assert.fail(); // this does not run. It does not belong to 'db-test' group. 
    } 

    @Test(groups = "db-test") 
    public void testMethod1() { 
     Assert.assertTrue(true); 
    } 
} 

這是否適用於您或我缺少您的問題的東西。

1

這似乎把我當成以下代碼挑戰(社區維基職位):

如何能夠執行從組「aGlobalGroup」擴展類的所有測試方法,而不:

  • 在擴展類本身上指定'aGlobalGroup'組?
  • 測試擴展類的未註釋的公共方法?

第一個答案很簡單:
基類水平

添加類TestNG的(組= {「aGlobalGroup」})該小組將同時適用於基類的所有公共方法和擴展類。

但是:即使非testng公共方法(沒有TestNG註釋)也會包含在該組中。

挑戰:避免包括那些非TestNG方法。

@Test(groups = { "aGlobalGroup" }) 
public class Base { 

    /** 
    * 
    */ 
    @BeforeClass 
    public final void setUp() { 
      System.out.println("Base class: @BeforeClass"); 
    } 


    /** 
    * Test not part a 'aGlobalGroup', but still included in that group due to the class annotation. <br /> 
    * Will be executed even if the TestNG class tested is a sub-class. 
    */ 
    @Test(groups = { "aLocalGroup" }) 
    public final void aFastTest() { 
     System.out.println("Base class: Fast test"); 
    } 

    /** 
    * Test not part a 'aGlobalGroup', but still included in that group due to the class annotation. <br /> 
    * Will be executed even if the TestNG class tested is a sub-class. 
    */ 
    @Test(groups = { "aLocalGroup" }) 
    public final void aSlowTest() { 
     System.out.println("Base class: Slow test"); 
     //throw new IllegalArgumentException("oups"); 
    } 

    /** 
     * Should not be executed. <br /> 
     * Yet the global annotation Test on the class would include it in the TestNG methods... 
    */ 
    public final void notATest() { 
     System.out.println("Base class: NOT a test"); 
    } 

    /** 
    * SubClass of a TestNG class. Some of its methods are TestNG methods, other are not. <br /> 
    * The goal is to check if a group specify in the super-class will include methods of this class. <br /> 
    * And to avoid including too much methods, such as public methods not intended to be TestNG methods. 
    * @author <a href="http://stackoverflow.com/users/6309/vonc">VonC</a> 
    */ 
    public static class Extended extends Base 
    { 
     /** 
     * Test not part a 'aGlobalGroup', but still included in that group due to the super-class annotation. <br /> 
     * Will be executed even if the TestNG class tested is a sub-class. 
     */ 
     @Test 
     public final void anExtendedTest() { 
      System.out.println("Extended class: An Extended test"); 
     } 

     /** 
     * Should not be executed. <br /> 
     * Yet the global annotation Test on the class would include it in the TestNG methods... 
     */ 
     public final void notAnExtendedTest() { 
      System.out.println("Extended class: NOT an Extended test"); 
     } 
    } 
相關問題