简体   繁体   English

在Android gradle项目中使用junit Category对robolectric测试进行分组

[英]Grouping robolectric tests using junit Category in an Android gradle project

I want to use Junit Category annotation to group the robolectric unit tests so that some tests will not run in a certain situation. 我想使用Junit类别注释对robolectric单元测试进行分组,以便某些测试不会在某种情况下运行。

In a normal java project, I know I can use 在一个普通的java项目中,我知道我可以使用

apply plugin: 'java'
test {
    useJUnit {
        includeCategories 'FastTest'
    }
}

to specify the category. 指定类别。

but apparently the 'java' plugin is not compatible with the Android plugins 'com.android.application'. 但显然'java'插件与Android插件'com.android.application'不兼容。

Error:The 'java' plugin has been applied, but it is not compatible with the Android plugins.

I tried to create a custom gradle task 我试图创建一个自定义gradle任务

sourceSets {
    test {
        java.srcDir file('src/test/java')
        resources.srcDir file('src/test/resources')
    }
}

task testWithFastCategory(type: Test) {
    group = "test"
    testClassesDir = sourceSets.test.output.classesDir
    classpath = sourceSets.test.runtimeClasspath
    useJUnit {
        includeCategories 'FastTest'
    }
}

but gradle seems not able to locate the right classpath for the test. 但gradle似乎无法找到测试的正确类路径。

Could someone provider a sample gradle setting to run robolectric tests grouping by Category under an Android project? 有人可以提供样本gradle设置来在Android项目下按类别运行robolectric测试吗?

Forgot to post my solution: 忘了发布我的解决方案:

android {
  testOptions {
    unitTests.all {
        useJUnit {
            // Include or exclude unit tests with specific categories
            // ex. use -DincludeCategories=com.ooxx.test.FastRun
            // to run tests with 'FastRun' category on CI server.
            String defaultPackage = "com.ooxx.test."
            if (System.properties['includeCategories'] != null) {
                def categories = System.properties['includeCategories'].split(',')
                categories = categories.collect {
                    it.charAt(0).isUpperCase() ? defaultPackage + it : it
                }
                println name + ": include category"
                categories.each { category ->
                    println " - " + category
                }
                includeCategories categories as String[]
            }
            // ex. use -DexcludeCategories=com.ooxx.test.DoNotRun
            if (System.properties['excludeCategories'] != null) {
                def categories = System.properties['excludeCategories'].split(',')
                categories = categories.collect {
                    it.charAt(0).isUpperCase() ? defaultPackage + it : it
                }
                println name + ": exclude category"
                categories.each { category ->
                    println " - " + category
                }
                excludeCategories categories as String[]
            }
        }
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM