2012-02-08 91 views
1

我不希望Junit按順序調用我的測試方法/硒測試用例。但我想要執行特定的測試用例,或者根據需要調用它。如何自定義Junit的測試用例調用功能

示例代碼:

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 

import com.thoughtworks.selenium.DefaultSelenium; 
import com.thoughtworks.selenium.Selenium; 


public class demo2 { 
    Selenium selenium; 

    @Before 
    public void setUp() throws Exception { 
     selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.co.in/"); 
     selenium.start(); 
     selenium.setTimeout("6000"); 
    } 

    @Test 
    public void test_3() throws Exception { 
     selenium.open("/"); 
     selenium.type("q", "3"); 
    } 
    @Test 
    public void test_4() throws Exception { 
     selenium.open("/"); 
     selenium.type("q", "4"); 
    } 

    @After 
    public void tearDown() throws Exception { 
      selenium.stop(); 
    } 
} 

注意

我想的方法test_3,test_4 ....應視情況調用。

回答

2

您可以使用Assume

assumeTrue(conditionIsFulfilled) 

從DOC:

失敗的假設並不意味着代碼被打破,但 測試沒有提供有用的信息。缺省的JUnit runner將 測試的失敗假設視爲忽略。自定義跑步者可能會有不同的表現 。

0

,而不是編寫不同@test的,你可以寫這些測試是正常的Java方法和只寫一個測試。

在該測試可以調用基於一些條件的任何方法。

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 

import com.thoughtworks.selenium.DefaultSelenium; 
import com.thoughtworks.selenium.Selenium; 


public class demo2 { 
    Selenium selenium; 

    @Before 
    public void setUp() throws Exception { 
     selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.co.in/"); 
     selenium.start(); 
     selenium.setTimeout("6000"); 
    } 

    @Test 
    public void test() throws Exception { 
     if(<condition1>){ 
      method1(selenium); 
     } 
     if(<condition2>){ 
      method2(selenium); 
     } 
     if(<condition3>){ 
      method3(selenium); 
     } 
     if(<condition4>){ 
      method4(selenium); 
     } 

    } 

    public static void method1(Selenium selenium) 
    throws Exception { 
     selenium.open("/"); 
     selenium.type("q", "1"); 
    } 

    public static void method2(Selenium selenium) 
    throws Exception { 
     selenium.open("/"); 
     selenium.type("q", "2"); 
    } 

    public static void method3(Selenium selenium) 
    throws Exception { 
     selenium.open("/"); 
     selenium.type("q", "3"); 
    } 

    public static void method4(Selenium selenium) 
    throws Exception { 
     selenium.open("/"); 
     selenium.type("q", "4"); 
    } 

    @After 
    public void tearDown() throws Exception { 
      selenium.stop(); 
    } 
}