Domanda

In JUnit 3, I could get all of the tests within a test suite with the following code:

TestSuite allTestsSuite = (TestSuite) AllTests.suite()
Enumeration enumeration = allTestsSuite.tests();
ArrayList listOfTests = Collection.list(enumeration);

However, I can't find an equivalent way of doing this in JUnit 4. Classes no longer have a .suite() method; they simply use the @Suite annotation. This wouldn't be a problem except that the Suite class no longer has a tests() method. There is a children() method, but that returns a list of Runners, which seem to be something different than why I'm looking for.

So how can I get the tests within a test suite in JUnit 4, like I could with JUnit 3?

È stato utile?

Soluzione 2

After a bit of experimentation, I discovered the following solution:

SuiteClasses suiteClassesAnnotation = AllTests.class.getAnnotation(SuiteClasses.class);
if (suiteClassesAnnotation == null)
    throw new NullPointerException("This class isn't annotated with @SuiteClasses");
Class<?>[] classesInSuite = suiteClassesAnnotation.value();

Basically, it gets the classes the same way that JUnit itself gets them: by looking into the annotation and determining which values are included within it.

The category solution provided by dkatzel is also a good option if you're ultimately wanting to filter these classes, but if you need a list of classes in a suite for some other purpose such as code analysis, this is the simplest and most direct way to do it.

Altri suggerimenti

The simplest way to perform any kind of filering is to create your own JUnit Categories.

See this Junit Category tutorial for more details but basically, you create your own categories named whatever you want

public interface GuiTest{ }

public interface DbTest {  }

And now you can annotate either entire test classes or individual tests with that category:

@Category(GuiTest.class)
public void myJPanelTest{


    @Test
    public void testFoo(){
        ...
    }

    //look we can have other categories too
    @Test
    @Category(DbTest.class)
    public void accidentalDbTest(){

    }
}

Then in your test suite, you can specify to include or exclude tests that match the given category

@RunWith(Categories.class)
@IncludeCategory(GuiTest.class)
@ExcludeCategory(DbTest.class)  //not sure if we need both but can't hurt
@SuiteClasses( {
     ...
 })
public class GuiTestsOnlySuite{}

Using Categories is much better than having to come up with manually filtering tests based on ad-hoc naming conventions because it that is hard to remember to do (and to make sure everyone in your group adheres to the naming conventions) and since the categories are classes, you can use your IDE to search/refactor/ compile time check your category names.

The only downside I've seen is at least in my IDE, the tests take a little longer to run because there is extra reflection work to do to make sure the test matches your category filter criteria before it runs.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top