I am doing some Junit testing and I need to know how to run a Test class only if a specific test from another class was passed.
-
You shouldn't ideally depend on other testcase to run a testcase. Make them as one testcase. – Abimaran Kugathasan Oct 17 '14 at 08:36
3 Answers
There is 'Categories' feature in JUnit. (See: https://github.com/junit-team/junit/wiki/Categories)

- 31,208
- 22
- 85
- 130
IMHO, This is bad practice, even in a well-designed Integration test-suite. I would encourage you to rethink your overall test class design.
If these tests are truly meant to be unit tests, they should be atomic and independent of each other. See this post for a good read.
Having said that, I have often used JUnit 4.x to build & run rather large suites of Integration tests (backend functional tests that test a RESTful services responses). If this is your use case, I recommend restructuring your tests such that you never have one test in TestClassA depend on a test in TestClassB. That is a bad idea, as it will making your tests more fragile. And difficult for other devs to understand the intention of your tests, taken together as a whole.
When I have found that I have dependencies across multiple test classes, I have factored out a "test-superclass" to both test classes and do my "setup work" in that superclass. Or you can factor out a utility class that contains static methods for creating somewhat complex test conditions to start with.
But, even using JUnit as a vehicle to run these kind of "integration" tests should be done with caution and careful intent.

- 102
- 1
- 8
-
This doesn't mean test B must run after test A, it may be that test A is fast and checks if the app starts and test B is slow and checks something more specific so why bother with test B if test A failed. – Captain Man May 07 '19 at 18:01
-
@CaptainMan: To clarify, I was specifically addressing the original poster's question: "I am doing some Junit testing and I need to know how to run a Test class only if a specific test from another class was passed." That question is *exactly* "test [class] B must run after test A" – aramcodez May 08 '19 at 13:06
-
I think I poorly worded my point. What I am trying to say is that there is a difference (but I agree, a small one) between "How can I run B after A" and "How can I run B only if A passes". – Captain Man May 08 '19 at 15:21
-