[英]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:
基本上:
testCompile 'junit:junit:4.+'
, etz testCompile 'junit:junit:4.+'
,etz添加JUnit和其他依赖项 src/test/java/...
src/test/java/...
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.