2012-02-08 14 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

あなたは、DOCからAssume

assumeTrue(conditionIsFulfilled) 

を使用することができます。

Aは、コードを意味するものではありません仮定が壊れて失敗したが、それ テストは有用な情報を提供しません。デフォルトのJUnitランナーは、失敗した前提を無視して テストを処理します。カスタムランナーは の動作が異なる場合があります。

0

別の@testを書く代わりに、通常のjavaメソッドとしてそれらのテストを記述し、1つのテストだけを書くことができます。

このテストでは、何らかの条件に基づいて任意のメソッドを呼び出すことができます。

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(); 
    } 
} 
関連する問題