1

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?

Shreya Bhat
  • 311
  • 2
  • 3
  • 13
  • 1
    Possible duplicate of [How to run test methods in specific order in JUnit4?](http://stackoverflow.com/questions/3693626/how-to-run-test-methods-in-specific-order-in-junit4) – Dragan Bozanovic Feb 18 '16 at 11:22
  • 1
    One possible way, I can think of is create a test suite and include only the test of your target. This way, it executes all the prior dependent tests and also stop execution when the target is hit – Balaji Katika Feb 18 '16 at 11:29
  • I think the real issue here is the problem. Why are your tests so dependent? If they're so dependent, why are you implementing them in Junit as separate test methods? – Ashley Frieze Feb 18 '16 at 13:00

1 Answers1

2

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

}
PyThon
  • 1,007
  • 9
  • 22