How to include multiple categories in junit4?

2019-08-05 05:24发布

问题:

I want to include multiple categories for a junit runner.

Java Code

@RunWith(Categories.class)
@IncludeCategory(IMAP.class , POP.class)
@SuiteClasses({MailTestSuites.class})
public class TestSuiteRunner{

}

I want to run my test suite for these two categories only. I have categories for mail suite like "IMAP,POP , SMTP , POP3". Now , I want to run only "IMAP , POP" category suites only. How can I run with junit?

回答1:

Up to version JUnit 4.11, @IncludeCategory and @ExcludeCategory only support one value. However, starting with the upcoming release 4.12, it will be allowed to pass multiple values.

According to the documentation in the source code, you can then write:

@RunWith(Categories.class)
@IncludeCategory({IMAP.class, POP.class})
@SuiteClasses({MailTestSuites.class})


回答2:

No, you couldn't define multiple values of @IncludeCategory in one Categories.

I recommend you use multiple Categories to run your test cases. (Separate IMAP and POP test cases)

//IMAP tests
@RunWith(Categories.class)
@IncludeCategory(IMAP.class)
@SuiteClasses({MailTestSuites1.class})
public class TestSuiteRunner1{}

//POP tests
@RunWith(Categories.class)
@IncludeCategory(POP.class)
@SuiteClasses({MailTestSuites2.class})
public class TestSuiteRunner2{}

//Merge into one Test
@RunWith(Suite.class)
@Suite.SuiteClasses({TestSuiteRunner1.class, 
    TestSuiteRunner2.class})
public class AllTests {}


标签: junit junit4