從給定的一組測試類集,Categories runner 僅僅是運行類 和 方法 ,他們都會被**@IncludeCategory ** 註解標記分類,或被 category 的子類型標記。 類和接口都能被 用於 category 。分類的做品,若是你說是@IncludeCategory(SuperClass.class), 那麼測試被標記爲 @Category({SubClass.class}) 將會被運行。java
public interface FastTests { /* category marker */ } public interface SlowTests { /* category marker */ } public class A { @Test public void a() { fail(); } @Category(SlowTests.class) @Test public void b() { } } @Category({SlowTests.class, FastTests.class}) public class B { @Test public void c() { } } @RunWith(Categories.class) @IncludeCategory(SlowTests.class) @SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite public class SlowTestSuite { // Will run A.b and B.c, but not A.a } @RunWith(Categories.class) @IncludeCategory(SlowTests.class) @ExcludeCategory(FastTests.class) @SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite public class SlowTestSuite { // Will run A.b, but not A.a or B.c }
<build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <groups>com.example.FastTests,com.example.RegressionTests</groups> </configuration> </plugin> </plugins> </build>
test { useJUnit { includeCategories 'org.gradle.junit.CategoryA' excludeCategories 'org.gradle.junit.CategoryB' } }
SBT's junit-interface 容許特定的 JUnit categories 例如: --include-categories=<CLASSES> 和 --exclude-categories=<CLASSES>.git
Categories 經常使用於在測試中添加元數據。github
常見的分類用法是: 自動測試的類型: UnitTests(單元測試), IntegrationTests(集成測試), SmokeTests(冒煙測試), RegressionTests(迴歸測試), PerformanceTests(性能測試) ... 測試執行快慢: SlowTests, QuickTests 在CI的哪部分構建測試應該被執行: NightlyBuildTests(每日構建的測試) 測試狀態: UnstableTests(不穩定測試), InProgressTests(進行測試) 這也用於添加特定於項目的元數據,如項目的哪些特性被測試覆蓋。maven
看到JUnit在GitHub託管項目類別用途: See usages of Junit Categories on github hosted projects性能
Categories javadoc : https://github.com/junit-team/junit4/wiki/Categories單元測試