How to include multiple categories in junit4?

2019-08-05 05:26发布

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?

标签: junit junit4
2条回答
仙女界的扛把子
2楼-- · 2019-08-05 05:57

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 {}
查看更多
放我归山
3楼-- · 2019-08-05 06:16

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})
查看更多
登录 后发表回答