简体   繁体   中英

How to add testCompile dependencies to IDE classpath

I have been struggling with unit-test-android problem for quite a long time. I have seen this , this and that , finally I found the gradle-android-test-plugin and even got it working. I can now run the tests with gradlew test command.

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.

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)?

I had the same problem with IntelliJ 14.1.3 today. The solution was to run the steps outlined here . Basically:

  1. Add JUnit and other dependencies via testCompile 'junit:junit:4.+' , etz
  2. Put test sources in 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". In "Project Structure", the testing dependencies should show up in your module with the "Test"-scope
  4. The commandline to run a test is testXxx , where Xxx is the build-type (debug/release/etz).

The important step here is the one in the "Build Variants" view. 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. This works for me when running my tests in Android Studio. Hope this helps.

You can use the built-in idea plugin. 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. 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

// 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

Any dependency included with testCompile will be automatically imported into IDEA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM