简体   繁体   中英

How can I run a test class only if another test was passed?

我正在做一些Junit测试,只有在通过另一个类的特定测试时,我才需要知道如何运行Test类。

There is 'Categories' feature in JUnit. (See: https://github.com/junit-team/junit/wiki/Categories )

This question has already been replied in this post

In the @Before function you need to retreive a runtime value from your system and check that it matches your requirement. Everything will stop if it doesn't

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM