Can I Skip Junit Tests if other tests fail?

2019-02-28 20:33发布

问题:

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 created tests to test Page load/moving documents to other workflows. If a page load test fails, or a workflow move fails I want to skip the subsequent page/workflow tests.

if a Test A fails how can I skip either the rest of the tests in the class or running tests in Class B?

NOTE: I realize what I'm asking is "bad Practice* for UNIT TESTS. However, I'm actually using Junit for Integration and/or Automation Testing. (Depending on your definition.) I've already found @Suite.SuiteClasses, and @FixMethodOrder to order my test classes and test methods. I'm trying to order them to run logically, testing the page load, first, then each feature of the page as a seperate test. Some of the features, move the information to other pages, meanining other classes. 1 of my classes can take over 1/2 hour to finish. If the pre-req tests fail, I'd like to short circuite the "Dependent" tests, in order to get my results/report sooner.

回答1:

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.



回答2:

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.