2016-12-01 14 views
0

試圖獲得問題中的所有好關鍵字。基本上我有一些Selenium測試,使用JUnit4/Maven和創建一個自定義的註釋標記每個測試的一些基本信息:是否有可能運行(通過maven)選擇基於自定義註釋的junit硒測試

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface TestInfo { 
public enum Priority { LOW, MEDIUM, HIGH } 

Priority priority() default Priority.LOW; 
String useCase() default ""; 
String createdBy() default "unknown"; 
String lastModified() default "unknown"; 
} 

所以每個測試看起來是這樣的:

@Test 
@TestInfo(priority = TestInfo.Priority.HIGH, 
     createdBy = "MivaScott", 
     lastModified = "2016/11/29", 
     useCase = "Log into website with valid credentials") 
public void loginValidCredentials() throws Exception { 
    Properties user = Data.getUserCredentials("default"); 
    loginPage.setLogin(user.getProperty("username")); 
    loginPage.setPassword(user.getProperty("password")); 
    loginPage.clickSignInButtonAndWait(); 
    Verify.titleContains(MyConstants.TITLE_DASHBOARD, ""); 
} 

我我希望的是,我可以在命令中指定只運行標記爲高優先級的測試。所以東西的效果:

mvn -DTestInfo.priority=HIGH test

這是可能的,或類似的東西?

+0

是否使用junit4?如果是這樣,你應該看看類別。基於開箱即用的註釋解決您運行測試的需求。 https://github.com/junit-team/junit4/wiki/categories – Grasshopper

+0

@Grasshopper,我看到類別在我尋找答案(還沒有研究他們如何工作呢)。我真的希望不必添加另一個註釋/要記住的東西。我正在爲非編碼器開發這個框架,所以移動較少的部分越好。但是,是的,這是後備選項。 – MivaScott

回答

0

我可以通過兩種方式來解決這個問題。

創建一個自定義的測試運行,它分析你的系統性能,並只運行與匹配註釋的測試方法。

public class PrioritzedTestRunner extends BlockJUnit4ClassRunner { 


@Override 
protected void runChild(final FrameworkMethod method, RunNotifier notifier) { 
    String priority = System.getProperty("TestInfo.priority"); 
    TestInfo info = method.getAnnotation(TestInfo.class); 

    if (priority == null || info == null) { 
     //If the configuration is blank or the test is uncategorized then run it 
     super.runChild(method, notifier); 
    } else if (priority != null) { 
     //try to resolve the priority and check for a match. 
     TestInfo.Priority configPri = TestInfo.Priority.valueOf(priority.toUpperCase()); 
     if (info.equals(configPri)) { 
      super.runChild(method, notifier); 
     } 
    } 
    } 

您需要的RunWith註釋添加到您的測試類。

@RunWith(PrioritizedTestRunner.class) 
public voidMyTestClass() { 
    @Test 
    @TestInfo(...)  
    public void testThing1(){} 
    @Test 
    @TestInfo(...)  
    public void testThing2(){} 
} 

如果測試是相當靜態的,它們分成類,而不是註解和使用自定義的Maven配置文件的測試集基於共同命名的文件或資源來執行。

我還沒有配置這個,但我已經看到它完成了。你應該能夠擁有針對你的PriorityLevels的Maven測試階段。
http://maven.apache.org/guides/introduction/introduction-to-profiles.html

此時,你應該能夠執行每個優先級作爲一個單獨的MVN命令,如果我正確地閱讀文檔。

祝您好運

相關問題