gpt4 book ai didi

Android gradle 测试框架 : single class

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:50:55 25 4
gpt4 key购买 nike

是否可以使用新的 Android gradle 构建框架运行单个测试类?

我有一个包含多个测试类的测试包(它们都是 InstrumentationTestCase 类)。我已经能够设置我的 build.gradle 文件来运行测试包

defaultConfig{
testPackageName "com.company.product.tests"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}

但是有没有办法只测试那个包中的一个测试用例呢?否则我将使用古老的 adb shell am instrument -w ......

附言我现在没有时间切换到 Roboelectric,但我确实看到它现在几乎是事实上的框架。

最佳答案

使用 android.test.InstrumentationTestRunner 不,这是不可能的。但是,您确实有选择:

自定义测试运行程序

  1. 扩展android.test.InstrumentationTestRunner
  2. 添加一个 buildConfigField 'String', 'TEST_NAME', '${testName}',其中 testName'"${project.properties.get ("test")}"' 如果设置,否则为 null
  3. 在您的运行器中,仅运行与 BuildConfig.TEST_NAME 匹配的测试(如果为 null,则运行所有测试)
  4. testInstrumentationRunner 替换为您的自定义运行器
  5. 使用 ./gradlew connectedCheck -Ptest=com.example.Test.testSomething 运行测试

使用勺子

Spoon是一个出色的测试运行程序扩展,除其他功能(如漂亮的多设备测试报告)外,它还允许您运行单独的测试。由于您使用的是 gradle,我推荐 Spoon Gradle Plugin ,它在其 README 中有一个确切说明您想要做什么的示例。

更新:使用 Android Gradle 插件运行单独的单元测试

通过添加单元测试支持,Android 现在支持运行单独的单元测试。

This is just an anchor task, actual test tasks are called testDebug and testRelease etc. If you want to run only some tests, using the gradle --tests flag, you can do it by running ./gradlew testDebug --tests='*.MyTestClass'.

Source

编辑:我还应该补充一点,Spoon 是运行测试的直接替代品。您无需修改​​构建脚本中的几行即可使用它。

关于Android gradle 测试框架 : single class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20661516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com