Shreya Bhat
Shreya Bhat

Reputation: 341

How do you run junit tests only till a particular test is reached

I have a bunch of dependent test cases (Not dependent via dependsOn parameter) and can't run one test at a time. That's why I want to run tests till a particular test is reached. I want to stop executing further tests and want to call teardowns.

Is there a way I can do this?

Upvotes: 1

Views: 307

Answers (1)

PyThon
PyThon

Reputation: 1067

You can control the test case execution by org.junit.Assume.assumeTrue(condition), Which ignores the test cases if it found the condition to be false. Sets the condition to be true in the beginning, a private static boolean variable can be user for example and the sets the condition to false in the end of the last test case that you want to be executed. Checks the condition in @BeforeTest.

Sample code

private static boolean runTest = true;

@org.junit.Before
public void before() throws Exception {
    org.junit.Assume.assumeTrue(runTest);
}

@org.junit.Test
public void test1() throws Exception {
    // will be executed
}

@org.junit.Test
public void test2() throws Exception {
    // will be executed
}

@org.junit.Test
public void test3() throws Exception {
    // will be executed
    runTest = false;
    // no test after this will be executed
}

@org.junit.Test
public void test4() throws Exception {

    // will be ignored

}

Upvotes: 2

Related Questions