25

(安らかな)Webサービスで機能テストを行いたいです。テスト スイートには多数のテスト ケースが含まれており、それぞれが Web サービスに対していくつかの HTTP 要求を実行します。

当然、Web サービスを実行する必要があります。実行しないと、テストは失敗します。:-)

Web サービスの開始には数分かかります (大量のデータ処理が行われます)。そのため、できるだけ頻繁に開始しないようにします (少なくとも、サービスからリソースを GET するだけのすべてのテスト ケースで 1 つを共有できます)。

テストケースの @BeforeClass メソッドのようにテストが実行される前に、テストスイートに爆弾を設定する方法はありますか?

4

4 に答える 4

27

答えは@ClassRule、スイート内に を作成することです。ルールは、各テスト クラスが実行される前または後に (実装方法に応じて) 呼び出されます。拡張/実装できる基本クラスがいくつかあります。クラス ルールの良いところは、それらを匿名クラスとして実装しなければ、コードを再利用できることです!

それらに関する記事は次のとおりです。http://java.dzone.com/articles/junit-49-class-and-suite-level-rules

これらの使用方法を説明するためのサンプル コードを次に示します。はい、些細なことですが、開始するのに十分なほどライフサイクルを説明する必要があります。

最初にスイートの定義:

import org.junit.*;
import org.junit.rules.ExternalResource;
import org.junit.runners.Suite;
import org.junit.runner.RunWith;


@RunWith( Suite.class )
@Suite.SuiteClasses( { 
    RuleTest.class,
} )
public class RuleSuite{

    private static int bCount = 0;
    private static int aCount = 0;

    @ClassRule
    public static ExternalResource testRule = new ExternalResource(){
            @Override
            protected void before() throws Throwable{
                System.err.println( "before test class: " + ++bCount );
                sss = "asdf";
            };

            @Override
            protected void after(){
                System.err.println( "after test class: " + ++aCount );
            };
        };


    public static String sss;
}

そして今、テストクラスの定義:

import static org.junit.Assert.*;

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExternalResource;

public class RuleTest {

    @Test
    public void asdf1(){
        assertNotNull( "A value should've been set by a rule.", RuleSuite.sss );
    }

    @Test
    public void asdf2(){
        assertEquals( "This value should be set by the rule.", "asdf", RuleSuite.sss );
    }
}
于 2011-10-03T18:12:37.677 に答える
1

jUnit はそのようなことはできませんが、TestNG には注釈が@BeforeSuiteあります。@AfterSuite通常、ビルド システムにそれを実行させます。Maven には、「pre-integration-test」フェーズと「post-integration-test」フェーズがあります。ANT では、タスクにステップを追加するだけです。

あなたの質問は、 jUnit 4.x の Before and After Suite 実行フックのほぼ複製であるため、そこの提案を見てみたいと思います。

于 2008-12-08T15:28:24.277 に答える
0

1 つのオプションは、Apache Ant などを使用して単体テスト スイートを起動することです。次に、junit ターゲットの前後にターゲット呼び出しを配置し​​て、Web サービスを開始および停止できます。

<target name="start.webservice"><!-- starts the webservice... --></target>
<target name="stop.webservice"><!-- stops the webservice... --></target>
<target name="unit.test"><!-- just runs the tests... --></target>

<target name="run.test.suite" 
        depends="start.webservice, unit.test, stop.webservice"/>

次に、ant (または任意の統合ツール) を使用してスイートを実行します。ほとんどの IDE は Ant をサポートしており、テストを継続的インテグレーション環境 (その多くは Ant ターゲットを使用して独自のテストを定義しています) に移行するのがはるかに簡単になります。

于 2008-12-09T12:28:30.880 に答える
-3

As an aside, it's a bad idea to have unit tests actually calling external resources like webservices, databases, etc.

Unit tests should be super-quick to run and a delay of 'a couple of minutes' for each run of the suite will mean it won't be run as much as it should.

My advice:

Look at mocking external dependencies in unit tests with something like EasyMock (http://www.easymock.org/).

Build a seperate suite of integration tests with something like Fitnesse (http://fitnesse.org/) or a homegrown solution that runs against a test environment and which is continually up.

于 2008-12-09T11:52:46.723 に答える