Saturday, November 14, 2009

Categories in JUnit 4.8

This is from the tests for the upcoming JUnit 4.8 release.  It's the simplest-to-implement framework we could find for labelling tests with named categories, and running only tests with a given label.


public interface SlowTests extends CategoryType {

}

public static class A {
@Test
public void a() {
fail();
}

@Category(SlowTests.class)
@Test
public void b() {
}
}

@Category(SlowTests.class)
public static class B {
@Test
public void c() {

}
}

public static class C {
@Test
public void d() {
fail();
}
}

@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses( { A.class, B.class, C.class })
public static class SlowTestSuite {
}