2012-03-11 17 views
2

メソッドのテストを実行できますか?クラス全体ではありませんか?理想的にはコマンドラインから。1つのメソッドのテストのみを実行する

クラス:

public class TestClass extends Unittest { 
    @Test 
    public void test1_shouldbeRun() { 

    } 

    @Test 
    public void test2_shouldNotBeRun() { 

    } 
} 

私は唯一の方法「test1_shouldBeRun」を実行します。

答えて

2

JUnitによってネイティブにサポートされているとは思われません。しかし、あなたはいくつかの選択肢を持っている:あなたはたぶん、あなたを助け@Ignore注釈を使用して個別に

  • それらを実行できるように

    1. は異なるクラスに必要なテストを入れてください。
    2. IDEを使用している場合は、このアプローチをサポートしている可能性があります。たとえば、Eclipseでパッケージエクスプローラでテストクラスを展開し、実行するメソッドを選択し、実行 - > JUnitテストをクリックします。
    3. Here's a quite extensive tutorialカスタムを作成する方法については、TestSuitと、これを実現するAntタスクを参照してください。
    4. UPDATEIn this thread人が古いjunit.textui.TestRunnerユーティリティはrunSingleMethod()を経由して、コマンドライン上の単一のメソッドを実行するための方法を提供します」と言うが、それはJUnit4注釈付きのテストをサポートしていません。クラス "
  • +0

    Eclipseオプションがクラスのすべてのテストを実行しているように見えるにもかかわらず、それはまだ私を助けてくれます。 – forste

    +0

    しかし、アプリケーション・ログはEclipseコンソールに記録されません。そこに指示する方法は分かりますか? – forste

    +0

    明らかにログレベルは何とかINFOに設定されているので、http://stackoverflow.com/questions/3501355/log4j-output-not-displayed-in-eclipse-consoleに似た問題が発生しています – forste

    0

    少し遅れて私はpost about thisを作りました。

    これは、JUnit 4とAntを使用して可能です。そのトリックは、メソッド名[s]をプロパティとしてコマンドラインに渡し、そのプロパティが渡された場合に別のタスクを使用します。

    は、このテストクラスを考える:

    <project default="test"> 
    
        <!-- Tell Ant where to find JUnit --> 
        <path id="classpath.test"> 
         <pathelement location="junit-4.11.jar" /> 
         <pathelement location="hamcrest-core-1.3.jar" /> 
         <pathelement location="." /> <!-- or where ever your test class file is --> 
        </path> 
        <target name="test" description="Runs the tests"> 
         <!-- Set a new Ant property "use-methods" to true if the "test-methods" Ant property - which we'll pass in from the command line - exists and is not blank.--> 
         <condition property="use-methods" else="false"> 
          <and> 
           <isset property="test-methods"/> 
           <not> 
            <equals arg1="${test-methods}" arg2=""/> 
           </not> 
          </and> 
         </condition> 
    
         <!-- Sanity check --> 
         <echo message="use-methods = ${use-methods}"/> 
         <echo message="test-methods = ${test-methods}"/> 
    
         <!-- Run the tests --> 
         <junit> 
          <classpath refid="classpath.test" /> 
          <formatter type="brief" usefile="false" /> 
          <!-- The "if" tells JUnit to only run the test task if the use-methods property is true.--> 
          <test if="${use-methods}" name="FooTests" methods="${test-methods}"/> 
          <!-- The "unless" tells JUnit to not run the test task if the use-methods property is true.--> 
          <test unless="${use-methods}" name="FooTests"/> 
         </junit> 
        </target> 
    </project> 
    

    を実行し、すべてのテスト:

    import org.junit.Test; 
    
    public class FooTests { 
    
        @Test 
        public void firstTest() { 
        System.out.println("test 1"); 
        } 
    
        @Test 
        public void secondTest() { 
        System.out.println("test 2"); 
        } 
    
        @Test 
        public void thirdTest() { 
        System.out.println("test 3"); 
        } 
    
    } 
    

    はこの蟻ファイルを作成します。コマンドラインで「テスト方法」のAntプロパティを指定することによってのみ、第二の試験

    $ ant 
    Buildfile: build.xml 
    
    test: 
        [echo] use-methods = false 
        [echo] test-methods = ${test-methods} 
        [junit] Testsuite: FooTests 
        [junit] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec 
        [junit] 
        [junit] ------------- Standard Output --------------- 
        [junit] test 3 
        [junit] test 1 
        [junit] test 2 
        [junit] ------------- ---------------- --------------- 
    
    BUILD SUCCESSFUL 
    Total time: 0 seconds 
    

    ラン。

    $ ant -Dtest-methods=secondTest 
    Buildfile: build.xml 
    
    test: 
        [echo] use-methods = true 
        [echo] test-methods = secondTest 
        [junit] Testsuite: FooTests 
        [junit] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.009 sec 
        [junit] 
        [junit] ------------- Standard Output --------------- 
        [junit] test 2 
        [junit] ------------- ---------------- --------------- 
    
    BUILD SUCCESSFUL 
    Total time: 0 seconds 
    

    2番目と3番目のテストのみを実行します。

    $ ant -Dtest-methods=secondTest,thirdTest 
    Buildfile: build.xml 
    
    test: 
        [echo] use-methods = true 
        [echo] test-methods = secondTest,thirdTest 
        [junit] Testsuite: FooTests 
        [junit] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.01 sec 
        [junit] 
        [junit] ------------- Standard Output --------------- 
        [junit] test 3 
        [junit] test 2 
        [junit] ------------- ---------------- --------------- 
    
    BUILD SUCCESSFUL 
    Total time: 0 seconds 
    
    0

    はMavenの中央に掲載さJute plugin、存在し、プラグインは、外部の分離JavaプロセスとしてJUnitテストメソッドを実行可能にする、唯一の試験方法を定義する場合が含まれ、テストプロセスから除外され、メソッドを定義することができテストメソッドのみが実行されます。

    関連する問題