2014-02-11 24 views
0

我有這樣的配置:爲什麼maven surefire會嘗試實例化其他組的測試?

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.13</version> 

      <configuration> 
       <excludedGroups>integration</excludedGroups> 
       <argLine>-Xmx512m -XX:MaxPermSize=128m</argLine> 
       <systemPropertyVariables> 
        <project.build.directory>${project.build.directory}</project.build.directory> 
       </systemPropertyVariables> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

和測試:

@Test(groups = "integration")//testng 
public class MyTest extends BaseTestForMyTest { 


private final SomeClass sut = new SomeClass(getStuffFromSysPropDefinedForFailSafe());//should fail in surefire. 


@Test(groups = "integration") 
public void someTest() throws IOException { 
    //some code 
} 
} 

萬無一失嘗試實例化一個類和失敗(失敗是確定的,該試驗對故障安全!)。 爲什麼,surefire爲什麼試圖從排除的組中實例化測試?

對不起,我沒有提到。我正在使用testng

+0

我沒有注意到代碼窗口有一個滾動條,完全錯過了代碼中的註釋!我已經用我認爲是更合適的答案更新了我的答案。我留下了最初的答案,儘管他們並不嚴格回答你問的問題。 –

+0

另外:如果此測試是針對故障安全插件的,那麼您應該將它從絕對執行中排除。最簡單的方法(根據maven關於配置的約定的想法)是將你的類從MyTest重命名爲MyIT。或者,將它添加到surefire的執行配置中。 –

回答

1

這裏有兩個答案。

對第一個問題的回答「爲什麼要調用new SomeClass(getStuffFromSysPropDefinedForFailSafe());?」

TestNG在查找註釋之前實例化所有測試類。我不知道這是故意的還是可以避免的。 TestNG正在準備調用@BeforeClass方法,因此它希望對象調用該方法。在實例化對象時,實例成員(在本例中爲sut)也被實例化。

我可以看到,無條件地創建每個測試類的實例會使TestNG的生活變得更加簡單,並且很可能沒有人要求任何其他行爲。


答到第二個問題(不知道是否你實際上問這個)「爲什麼是試驗正在運行?」

你沒有正確排除它。在TestNG中,groups參數接受一個字符串數組,而不是一個字符串。嘗試

@Test(groups = { "Integration" }) 
public class YourTests { 
    @Test(groups = { "ReallySlow" }) 
    public void someTest { 
    // This test is a really slow integration test and is in both groups. 
    } 
} 

不是一個答案,你問任何問題,但希望感興趣的一些人誰讀過這一步(加上我之前的一些你的問題的編輯寫了這個):

要將JUnit測試標記爲安全火狐插件將排除的組中,您必須將它們註釋爲處於類別中。上述TestNG代碼的JUnit相當於:

@Category(IntegrationTest.class) 
public class YourTests { 
    @Category(ReallySlowTest.class) 
    @Test 
    public void someTest { 
    // This test is a really slow integration test and is in both categories. 
    } 
} 

您可以在this dzone article中閱讀更多內容。

此外,您應該閱讀failsafe plugin,因爲它更適合集成測試。

+0

我在故障安全插件中爲「集成」組分配了conf。我只是不明白爲什麼surefire試圖「觸摸/實例化」已經排除了anno名稱。所以我稍後會報告。感謝輸入 – Sergey

+0

所以解決方案不嘗試在字段中實例化smth,只需將其移到懶惰的getters/setter。它的工作原理,謝謝。 – Sergey

相關問題