拆分Android单元和集成测试 [英] Splitting unit and integration tests for android

查看:56
本文介绍了拆分Android单元和集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我有一个 test/src/java 文件夹,其中存储了针对android应用程序的所有测试(测试使用junit,mockito和robolectric完成).

Currently I have a test/src/java folder where all of the tests for the android application are stored (tests are done using junit, mockito and robolectric).

我可以使用 ./gradlew测试

我想要实现的是拥有两个文件夹:

What I'd like to achieve is having two folders:

  1. integrationTest/src/java -用于集成测试
  2. test/src/java -用于单元测试
  1. integrationTest/src/java - for integration tests
  2. test/src/java - for unit tests

我也想分别运行它们,例如 ./gradlew test ./gradlew integrationTest .

And also I'd like to run them separately, like ./gradlew test and ./gradlew integrationTest.

我已经设法使用 sourceSets 这样的测试来拆分目录:

I've managed to split directories with tests using sourceSets like this:

    sourceSets {
        test {
            java {
                srcDirs = ['src/test/java', 'src/integrationTest/java', 'src/commonTest/java']
            }
            resources {
                srcDirs = ['src/test/resources', 'src/integrationTest/resources', 'src/commonTest/resources']
            }
        }
    }

我在Google上搜索了许多有关如何创建自定义测试任务的示例,但其中大多数与Java而不是android有关,而其他则与时俱进.我现在已经花了整整一天的时间,所以如果有人可以帮助我,我将非常感激.

And I had googled many examples on how to create custom test tasks, but most of them are related to java instead of android and the others are out-of-date. I've spent on that the whole day now and so if someone can help me I would really appreciate that.

推荐答案

如果您的集成测试是经过测试的测试,则只需使用默认文件夹 test androidTest 并使用 ./gradlew test ./gradlew connectedAndroidTest

If your integration tests are instrumented tests, then you can just use the default folders test and androidTest and run them separately using ./gradlew test and ./gradlew connectedAndroidTest

另一种方法(如果可以在 test 文件夹中进行集成测试)将是在 test 文件夹中使用单独的程序包,并使用以下命令分别运行测试:/p>

Another way (if you can have the integration tests inside the test folder) would be to use separate packages inside the test folder and run the tests separately using:

./gradlew testDebug --tests="com.yourapplication.unittests.*"
./gradlew testDebug --tests="com.yourapplication.integrationtests.*"
...

这篇关于拆分Android单元和集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆