简体   繁体   English

我们如何排除一些单元测试在 Android 项目中运行

[英]How do we exclude some unit tests from running in an Android project

We are in我们在

  • gradle version 5.5 gradle 版本 5.5
  • android gralde plugin 3.5.0 android grade 插件 3.5.0

And I can run a subset of tests with我可以运行测试的子集

./gradlew :app:testDebug --tests="*Fragment*" --tests="*Activity*"

But I want to find a way to do the inverse ie not run the tests that is matching these options, something like但我想找到一种方法来做相反的事情,即不运行与这些选项匹配的测试,比如

./gradlew :app:testDebug --notTests="*Fragment*" --notTests="*Activity*"

There is an excludeTests option https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/TestFilter.html but I can't use these in an android project, these are pure java options.有一个 excludeTests 选项https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/TestFilter.html但我不能在 android 项目中使用这些,这些是纯 java 选项。

Some context: On our CI we are seeing memory issues with unit tests and we are suspecting roboelectric, so we are trying separate pure unit tests vs roboelectric tests to try and isolate the issues or apply specific fixes for the jobs.一些背景:在我们的 CI 上,我们看到单元测试存在内存问题,我们怀疑是机器人电气,所以我们正在尝试单独的纯单元测试与机器人电气测试,以尝试隔离问题或为作业应用特定的修复程序。

I know it's not the nicest solution but I did manage to make it work by doing the following.我知道这不是最好的解决方案,但我确实通过执行以下操作设法使其工作。

Annotate your test class with注释您的测试类

@Category(my.package.WhateverClass) // I personally used Robolectric

Add the following to your root build.gradle将以下内容添加到您的根build.gradle

if (project.gradle.startParameter?.taskRequests?.args[0]?.remove("--robolectric")) {
    subprojects
            .all {
        it.tasks
                .withType(Test)
                .configureEach {
            println "Configuring task $it.name"
            useJUnit {
                includeCategories 'my.package.WhateverClass'
            }
        }
    }
} else if (project.gradle.startParameter?.taskRequests?.args[0]?.remove("--unit")) {
    subprojects
            .all {
        it.tasks
                .withType(Test)
                .configureEach {
            println "Configuring task $it.name"
            useJUnit {
                excludeCategories 'my.package.WhateverClass'
            }
        }
    }
}

Run tests as ./gradlew project:testDebug --robolectric or ./gradlew project:testDebug --unit作为./gradlew project:testDebug --robolectric./gradlew project:testDebug --unit运行测试

If you don't add anything it will run all tests by default如果您不添加任何内容,它将默认运行所有测试

If you have more time, you could probably create a task, that extends from test and adds the same configurations.如果您有更多时间,您可能可以创建一个任务,从测试扩展并添加相同的配置。

Note: The .remove('--robolectric') is there because Gradle will think its a task since it's not defined as a flag in any task注意: .remove('--robolectric')存在是因为 Gradle 会认为它是一个任务,因为它没有在任何任务中定义为标志

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

相关问题 如何构建我的 Android Studio 项目以避免在运行集成了单元测试的应用程序时出现编译问题 - How do I structure my Android Studio project to avoid compilation issues with running the app with unit tests integrated 如何从Android部署中排除单元测试 - How do I exclude unit test from my android deploy 如何从功能测试报告中排除单元测试? - How to exclude unit tests from the report of functional tests? 如何排除在单元测试中运行的错误? - How to exclude error prone from being run on unit tests? 如何使用Google App Engine插件从WAR中排除单元测试? - How to exclude unit tests from the WAR in with Google App Engine plugin? 从 IntelliJ IDEA 中的 gradle 项目运行单元测试时,如何仅显示 class 名称? - How to show only class name when running unit tests from gradle project in IntelliJ IDEA? Gradle:如何排除一些测试? - Gradle: how to exclude some tests? 在运行JBehave测试时,如何从Serenity报表中排除@skips? - How do you exclude @skips from Serenity reports while running JBehave tests? 如何从 spring rest 文档中排除一些测试? - How to exclude some tests from spring rest docs? 将 Junit 从战争中排除; 保留用于单元测试 - Exclude Junit from War; Keep for Unit Tests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM