Can I Skip Junit Tests if other tests fail?

后端 未结 3 622
广开言路
广开言路 2021-01-25 04:43

I\'m using Junit to run tests with Seleniun WebDriver. I\'m trying to split up my tests into function area for better error reporting. I\'ve

相关标签:
3条回答
  • 2021-01-25 04:53

    I suggest you switching to TestNG. Check this out.

    Btw:When I used to work with Java - almost one year ago - I didn't find such a solution for JUnit.

    0 讨论(0)
  • 2021-01-25 05:05

    You could try manipulating the tests via the Rules interface, they have an example here: http://www.codeaffine.com/2013/11/18/a-junit-rule-to-conditionally-ignore-tests/

    That might be a good starting point.

    0 讨论(0)
  • 2021-01-25 05:08

    By using Junit's Assumewithin the @Before or @Test methods, you can skip the current test by using either

    1. Assume#assumeTrue(boolean) for any depended tests that failed. You can skip the current test by calling Assume.assumeTrue(false)
    2. Assume#assumeNoException(Throwable) for skippable tests and skip the current test by passing the AssumptionVoilated exception when the other test was skipped.

    Here's an example:

    public class Tests{
        @Test
        public void test1(){
            ...
        }
    
        @Test
        public void test2(){
            try{
                test1();
            }catch(AssertionError failExcep){
                //test1 failed, skip this test
                Assume.assumeTrue(false);
            }catch(AssumptionVoilationException skipExcep){
                //test1 was skipped, skip this test
                Assume.assumeNoException(skipExcep);
            }
            ...
        }
    }
    

    This particular example would run test1() twice, but by adding @FixMethodOrder and catching the any error thrown during test1 to be referenced in test2; we could circumvent that issue.

    Also this example does not cover any other exception type that test1() could throw, e.g. NullPointerException. Editing the AssumptionVoilationException catch clause to Exception will allow test2 to be skipped on any exception. If you also want to catch Error or Throwable just add a catch clause and put in Assume.assumeTrue(false) like AssertionError. Or you could just throw AssumptionViolatedException

    Here's the dummy proof version

    @FixMethodOrder
    public class Tests{
    
        private Optional<Throwable> testOneThrown;
    
        @Test
        public void test1(){
            try{
                ...
            }catch(Throwable t){
                // test1 throw something, catching it for test2() reference
                testOneThrown = Optional.of(t);
                /*
                 * still throwing it for junit normal functions and
                 * in case test2 ran independently
                 */
                throw t;
            }
            //Ran successfully, making reference non-null but empty
            testOneThrown = Optional.empty();
        }
    
        @Test
        public void test2(){
            try{
                if(testOneThrown == null){
                    // test1 did not run, thus needs to run
                    test1();
                }else if(testOneThrown.isPresent()){
                    // test1 ran and threw something
                    throw testOneThrown.get();
                }
                //test1 ran and throw nothing
            }catch(Throwable thrown){
                /*
                 * something was throw in test1, test1 could have
                 * failed, could have been skipped, etc. thus skip 
                 * this test.
                 */
                throw new AssumptionViolatedException("Dependency on test1 failed.", thrown);
            }
            ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题