简体   繁体   English

如何将testCompile依赖项添加到IDE类路径

[英]How to add testCompile dependencies to IDE classpath

I have been struggling with unit-test-android problem for quite a long time. 很长一段时间以来,我一直在努力解决unit-test-android问题。 I have seen this , this and that , finally I found the gradle-android-test-plugin and even got it working. 我已经看到了这个这个那个 ,最后我找到了gradle-android-test-plugin甚至让它运行起来。 I can now run the tests with gradlew test command. 我现在可以使用gradlew test命令运行测试。

But, writing those tests in IDE (Android Studio or IntelliJ 13) is far from comfortable, because it does not see the junit & Robolectric dependencies added with testCompile dependency. 但是,在IDE(Android Studio或IntelliJ 13)中编写这些测试是不太舒服的,因为它没有看到junit和Robolectric依赖项添加了testCompile依赖项。

Is there any way to add these dependencies to the IDE classpath but still avoid to package them in the production app (thus, AFAIU compile dependency cannot be used)? 有没有办法将这些依赖项添加到IDE类路径中,但仍然避免将它们打包到生产应用程序中(因此,不能使用AFAIU compile依赖项)?

I had the same problem with IntelliJ 14.1.3 today. 我今天遇到了与IntelliJ 14.1.3相同的问题。 The solution was to run the steps outlined here . 解决方案是运行此处概述的步骤。 Basically: 基本上:

  1. Add JUnit and other dependencies via testCompile 'junit:junit:4.+' , etz 通过testCompile 'junit:junit:4.+' ,etz添加JUnit和其他依赖项
  2. Put test sources in src/test/java/... 将测试源放在src/test/java/...
  3. To make the IDE find the test-dependencies (gradle will find them fine), open the "Build Variants"-view and set "Test Artifact" to "Unit Test". 要让IDE找到测试依赖项 (gradle会发现它们没问题),打开“Build Variants” - 查看并将“Test Artifact”设置为“Unit Test”。 In "Project Structure", the testing dependencies should show up in your module with the "Test"-scope 在“项目结构”中,测试依赖项应该在模块中显示“Test”-scope
  4. The commandline to run a test is testXxx , where Xxx is the build-type (debug/release/etz). 运行测试的命令行是testXxx ,其中Xxx是构建类型(debug / release / etz)。

The important step here is the one in the "Build Variants" view. 这里重要的一步是“Build Variants”视图中的一个。 After you change it to "Unit Test", it will index and your libraries and full auto-completion are available. 将其更改为“单元测试”后,它将编制索引,并且您的库和完全自动完成功能可用。

For my Android test dependencies, I use instrumentTestCompile instead of testCompile. 对于我的Android测试依赖项,我使用instrumentTestCompile而不是testCompile。 This works for me when running my tests in Android Studio. 在Android Studio中运行测试时,这适用于我。 Hope this helps. 希望这可以帮助。

You can use the built-in idea plugin. 您可以使用内置的idea插件。 That should set up test dependencies for you. 这应该为您设置测试依赖项。 You'll need to import the plugin: 你需要导入插件:

apply plugin: 'idea'

Then run gradle idea , to generate module file ( *.iml ) and re-load your project. 然后运行gradle idea ,生成模块文件( *.iml )并重新加载项目。 Note you'll have to be using non-directory based idea configuration for this to work. 请注意,您必须使用非基于目录的构思配置才能使用此功能。

In IntelliJ IDEA you need to configure couple things in your build.gradle 在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 ]
    }
}

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

Any dependency included with testCompile will be automatically imported into IDEA. testCompile包含的任何依赖项都将自动导入到IDEA中。

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

相关问题 Android lint:如何禁用“NewerVersionAvailable”检查 testCompile 依赖项? - Android lint: How to disable “NewerVersionAvailable” check for testCompile dependencies? 如何在依赖项部分中将Dokka添加到Android项目而无需其他classpath? - How to add Dokka to Android project without additional classpath in the dependencies section? Gradle - 在buildscript依赖项中添加条件类路径 - Gradle - Add conditional classpath in buildscript dependencies testCompile导致Android studio无法解析依赖关系 - testCompile results in Android studio not resolving dependencies 是否将testCompile和androidTestCompile依赖项放入内置的APK中? - Do testCompile and androidTestCompile dependencies go into the built apk? Android库模块的使用者必须添加类路径依赖项 - Consumer of Android library module having to add classpath dependencies 错误:无法在Robolectric中解析配置':app:testCompile`的所有依赖项 - Error : Could not resolve all dependencies for configuration ':app:testCompile` in Robolectric 如何添加依赖? - How to add dependencies? 无法添加名称为“testCompile”的配置作为具有该名称的配置 - Cannot add a configuration with name 'testCompile' as a configuration with that name already exists 如何在 Android Studio 项目中添加类路径 - How to add classpath in an Android Studio project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM