【问题标题】:How to add testCompile dependencies to IDE classpath如何将 testCompile 依赖项添加到 IDE 类路径
【发布时间】:2014-01-09 20:53:25
【问题描述】:

我已经为 unit-test-android 问题苦苦挣扎了很长时间。我见过thisthisthat,最后我找到了gradle-android-test-plugin,甚至让它工作了。我现在可以使用gradlew test 命令运行测试。

但是,在 IDE(Android Studio 或 IntelliJ 13)中编写这些测试非常不方便,因为它看不到使用 testCompile 依赖项添加的 junit 和 Robolectric 依赖项。

有没有办法将这些依赖添加到 IDE 的类路径中,但仍然避免将它们打包到生产应用程序中(因此,无法使用 AFAIU compile 依赖)?

【问题讨论】:

标签: android junit gradle gradle-android-test-plugi


【解决方案1】:

我今天在 IntelliJ 14.1.3 上遇到了同样的问题。解决方案是运行here 概述的步骤。基本上:

  1. 通过testCompile 'junit:junit:4.+', etz 添加 JUnit 和其他依赖项
  2. 将测试源放入src/test/java/...
  3. 要让 IDE 找到测试依赖项(gradle 会很好地找到它们),打开“Build Variants”-视图并将“Test Artifact”设置为“Unit Test”。在“项目结构”中,测试依赖项应该以“测试”范围显示在您的模块中
  4. 运行测试的命令行是 testXxx,其中 Xxx 是构建类型 (debug/release/etz)。

这里重要的一步是“构建变体”视图中的一步。将其更改为“单元测试”后,它将索引并且您的库和完全自动完成功能可用。

【讨论】:

    【解决方案2】:

    对于我的 Android 测试依赖项,我使用 instrumentTestCompile 而不是 testCompile。在 Android Studio 中运行我的测试时,这对我有用。希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      您可以使用内置的idea 插件。那应该为您设置测试依赖项。您需要导入插件:

      apply plugin: 'idea'
      

      然后运行gradle idea,生成模块文件(*.iml)并重新加载您的项目。请注意,您必须使用基于非目录的想法配置才能使其工作。

      【讨论】:

        【解决方案4】:

        在 IntelliJ IDEA 中,您需要在 build.gradle 中配置一些东西

        // add idea plugin
        apply plugin: 'idea'
        // make sure `configurations.testCompile` is added to idea.module
        idea {
            module {
                scopes.TEST.plus += [ configurations.testCompile ]
            }
        }
        

        有关详细信息,请参阅: http://www.gradle.org/docs/current/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html

        testCompile 包含的任何依赖项都将自动导入 IDEA。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-11-18
          • 1970-01-01
          • 2017-01-14
          • 1970-01-01
          • 2015-05-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多