簡體   English   中英

Gradle測試任務不使用@Category和@RunWith注釋運行JUnit測試

[英]Gradle test task does not run JUnit test with @Category and @RunWith annotations

Gradle沒有使用@Category和@RunWith注釋運行我的JUnit測試。

Java 8,Gradle 4.2.1。

我的JUnit類:

public interface FastTest {
}

@Category(FastTest.class)
@RunWith(PowerMockRunner.class)
public class MyTest {
    @Test
    public void testMyMethod() {
        // Test method should fail
        assertTrue(false);
    }
}

我的build.gradle:

apply plugin: 'java'

repositories { mavenCentral() }

dependencies {
    compile "junit:junit:4.12"
    compile "org.powermock:powermock-core:1.6.5"
    compile "org.powermock:powermock-api-mockito-common:1.6.5"
    compile "org.powermock:powermock-module-junit4:1.6.5"
    compile "org.powermock:powermock-api-mockito:1.6.5"
}

test {
    scanForTestClasses = false

    useJUnit { includeCategories 'FastTest'  }
}

如果我刪除RunWith注釋,Gradle將運行測試。 scanForTestClasses = false設置無效。

報告的Gradle問題: https//github.com/gradle/gradle/issues/3189

PowerMock用代理替換Category注釋: RunWith(PowerMockRunner.class)不適用於包注釋

解決方法:將PowerMockIgnore注釋添加到JUnit類:

@PowerMockIgnore({ "org.junit.experimental.categories.Category", "mypackage.FastTest" })
@Category(FastTest.class)
@RunWith(PowerMockRunner.class)
public class MyTest {
    @Test
    public void testMyMethod() {
        // Test method should fail
        assertTrue(false);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM