Monday 12 September 2016

JUnit Categories

JUnit Categories are only applicable for JUnit 4.8 and above. In this java interfaces are used to make different categories and add your test cases in different categories. This allows you to run your test cases as per different categories.

Whenever we are creating a automation framework. We write all the test cases  covering the entire application but at the time execution we realize that we want to run some test cases during Feature Testing, Some test cases while User Acceptance Testing and some test cases during production release so in this case you can categories your test case and execute as per categories.

Let us see an example of JUnit categories as shown below -



public interface FastTests 
{ 
/* category created */
}

public interface SlowTests
 {
 /* category created */
}

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

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

@Category({ Feature1Tests.class, Feature2Tests.class })
public class B {
        @Test
        public void c() {
        }
}

@RunWith(Categories.class)
@IncludeCategory(Feature1Tests.class)
@SuiteClasses({ A.class, B.class })
// Note that Categories is a kind of Suite
public class Feature1TestSuite {
        // Will run A.b and B.c, but not A.a
}

@RunWith(Categories.class)
@IncludeCategory(Feature1Tests.class)
@ExcludeCategory(Feature2Tests.class)
@SuiteClasses({ A.class, B.class })
// Note that Categories is a kind of Suite
public class Feature1TestSuite {
        // Will run A.b, but not A.a or B.c
}

0 comments:

Post a Comment