23

次の形式のJUnitテストスイートがあります。

@RunWith(Suite.class)
@Suite.SuiteClasses( { xx.class, yy.cass })

public class AllTests {

public static Test suite() {
    TestSuite suite = new TestSuite(AllTests.class.getName());
    //$JUnit-BEGIN$

    //$JUnit-END$
    return suite;
}
}

次に、これは次のようなバニラテストを呼び出します。

public class xxx {

@Test
public void test () throws {
    ...

エラーが発生した場合、または最初のテストで失敗した場合に、残りのテストスイートの実行を停止したいという状況があります。しかし、他のエラー/失敗は問題なく、スイートはできるだけ多くの他のテストを完了する必要があります。基本的に、最初のテストが失敗すると、残りのテストを実行するのは安全ではないことを示します。

これは可能ですか?

4

7 に答える 7

20

まず、junitRunListenerが必要です。

import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;

public class FailureListener extends RunListener {

    private RunNotifier runNotifier;

    public FailureListener(RunNotifier runNotifier) {
        super();
        this.runNotifier=runNotifier;
    }

    @Override
    public void testFailure(Failure failure) throws Exception {
        super.testFailure(failure);
        this.runNotifier.pleaseStop();
    }
}

次に、スイートを準備します。

public class StopOnFailureSuite extends Suite {

    public StopOnFailureSuite(Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {
        super(klass, suiteClasses);
    }

    public StopOnFailureSuite(Class<?> klass) throws InitializationError {
        super(klass, klass.getAnnotation(SuiteClasses.class).value());
    }

    @Override
    public void run(RunNotifier runNotifier) {
        runNotifier.addListener(new FailureListener(runNotifier));
        super.run(runNotifier);
    }
}

そして、スイートを実行します。

@RunWith(StopOnFailureSuite.class)
@Suite.SuiteClasses({
    FirstTestClass.class,
    SecondTestClass.class,
    ...
})
于 2015-07-14T03:59:29.240 に答える
9

電話をかけることの何が問題になっていSystem.exit()ますか?

于 2012-04-05T23:04:12.737 に答える
5

最初のテストの場合は、検証を@BeforeClassに移動し、失敗した場合は例外をスローすることを検討してください。この場合、この例外の場合は@AfterClassメソッドのみが実行されます。

もちろん、そうすれば、テストセットアップメソッドで作成されたすべてのフィクスチャアーティファクトが不足します。

于 2012-04-06T03:55:15.890 に答える
4

あなたの答えと同じですが@Before、統合テストで使用して、私は次のようなことをしました:

public class FooTest {

   private static boolean bar;

   @BeforeClass
   public static void setUpBeforeClass() throws Exception {
         bar = false;
   }

   @Before
   public void setUp() throws Exception {
         assertTrue(bar);
   }

   @Test
   public void test() {
         System.out.println("something");
         assertTrue(true);
   }

   @Test
   public void test1() {
         System.out.println("Something2");
         assertTrue(true);
   }
}

よろしく!

于 2015-01-05T13:01:33.977 に答える
3

Hiro2kからの回答に基づいて(ありがとう!)私は次の解決策を使用しました。ちょっとしたハックですが、機能します。

他のテストの実行を妨げる可能性のあるテストは、@Suite.SuiteClassesリストの一番上にあります。そのテストには次のようなものがあります。

private static boolean shouldStopRestOfSuite = false;

    @Test
    public void test () throws Throwable {
    try {
            ... run some test code...
    }
    catch (Throwable e) {
        shouldStopRestOfSuite = true;
        throw e;
    }
}

上記はThrowable(例外ではない)をキャッチする必要があるため、アサーションエラーをキャッチすることに注意してください。また、エラーを再スローして、分析のためにJUnitによってログに記録されるようにします。

次に、別のテスト方法があります。

@Test
public void testKillIfNeeded () throws Exception {
    if (!shouldStopRestOfSuite) {
        return;
    }

    System.out.println ("Test suite killed due to dangerous error / failure");
    System.exit(1);
}

上記は2番目に実行され、JUnitプロセスを強制終了します。

この方法を使用すると、問題が発生した場合にJUnitテストが失敗/エラーで終了することはありませんが、失敗/エラーはJUnitによる分析のためにログに記録され、それ以上のテストは実行されません。

あまりきれいではありませんが、それは仕事をします:)

于 2012-04-06T14:21:33.400 に答える
2

最初に、エラーをキャッチして、2番目のテストを実行する前に同じことを確認する必要があります。

@Rule
public ErrorCollector collector = new ErrorCollector();

1.エラーを追加します。

collector.addError(new Throwable("first thing went wrong"));

2.依存実行の前に確認してください

collector.checkThat(getResult(), not(containsString("ERROR!")));

参照-ErrorCollector

于 2017-11-15T05:11:38.330 に答える
0

antを使用してテストを実行していますか?

カスタムテストリスナーを作成できます。これは、ant http://ant.apache.org/manual/Tasks/junit.html(enableTestListenerEvents)で設定できます。

于 2012-04-06T03:41:27.800 に答える