espresso dependencies always confusing what to add - android

Android Studio 2.1 Preview 5
I am using wondering what should I be including in my build.gradle file to use espresso. Everything I google I get different combinations of dependencies to include.
I have people do this:
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
And from this web site https://google.github.io/android-testing-support-library/docs/espresso/setup/
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
Can anyone give a standard way I should be following?
Many thanks for any suggestions,

It depends on the version of espresso you are using. testing-support-lib:0.1 was correct for the (older) version 2.0.
If you want to use Espresso 2.2.2, this is correct:
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
For newer versions, instructions will be updated here.

Related

How to figure out which version of com.android.support.test dependencies I need?

I need to write some automated tests in the project I'm working on. So I just copied the required dependencies from some Google's example:
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
// Espresso UI Testing dependencies.
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'
The problem I ran into was something you could find in a million questions on SO:
Warning:Conflict with dependency 'com.android.support:recyclerview-v7'. Resolved versions for app (25.3.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
Here I am not asking about the required versions of the test runner, test rules, and the other test dependencies, to be able to sync Gradle and move on.
My question. Is there any quick way to see which versions of com.android.support.test:runner, com.android.support.test:rules, etc. depend on Support Library 25.3.0?
Of course, I could keep replacing 0.5 with 0.6, 0.7, etc, until Gradle syncs successfully, but it's boring and would take me long.
The short answer is that currently there isn't a version that depends on Support Library 25.3.0 since that version of the support library was released long after the official stable testing artifacts.
If you want to be able to discern this with future versions, you would need to look at the transitive dependencies of each of the artifacts. You can see these in the maven pom files, or via gradle by running:
gradle :your_module_name:dependencies
This being said, you can usually use any version of the support library by telling gradle to ignore Espresso's transitive dependencies like this:
androidTestCompile("com.android.support.test.espresso:espresso-contrib:2.2.2") {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
you can use the gradle dependencies command above in order to figure out which espresso artifacts are pulling in the transitive dependencies that are causing conflicts.

Android. #RunWith(AndroidJUnit4.class) - cannot resolve in package "androidTest"

Android Studio 2.2.3
Install Android Support Repository - ver. 44.0.0
I setup all as in official site for Espresso:
https://google.github.io/android-testing-support-library/docs/espresso/setup/index.html
I try to write instrumentation test (Espresso) in package androidTest. So I create StringUtilAndroidTest in folder src/androidTest/java/com/mycompany/
My StringUtilAndroidTest code:
#RunWith(AndroidJUnit4.class)
#LargeTest
public class StringUtilAndroidTest {
#Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);
#Test
public void myTest() {
assert(true);
}
}
In my build.gradle:
android.defaultconfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
my dependencies:
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-library:1.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1
But in StringUtilAndroidTest I get compile error:
#RunWith(AndroidJUnit4.class)
Cannot resolve symbol RunWith
Why?
Short answer: add this to your dependencies and you're golden.
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
Long answer:
In the default configuration, an Android Studio project has two different testing "variants": test & androidTest. The former uses the 'src/test/java', and the latter 'src/androidTest/java' (which is your scenario).
There's a big difference between the two: androidTest needs an emulator or a device to run, and test doesn't. This means that test is much faster to run (usually a couple seconds on the IDE), but it doesn't have access to the Android Framework (like Activities, Contexts & etc). On the other hand, androidTest takes much longer to run (not to mention the waiting time for the emulator itself), but it does have the Android framework (since it's running in one).
Since they're two separate variants, you need to declare their dependencies separately as well. testCompile and androidTestCompile each adds that dependency only to their own variant. To have JUnit on both, you have to declare to dependency on both - essentially "repeating" the line.
P.S.: Note that when you use compile, that adds it to all variants, so you don't have to repeat non-test dependencies.
You probably have missed some dependency.
//App's dependencies, including test
compile 'com.android.support:support-annotations:22.2.0'
// Testing-only dependencies
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'junit:junit:4.12'
testCompile 'junit:junit:4.12'
Hope this will fix your code.

Unable to resolve Android JUnit test runner

I am trying to add an instrumentation test to our project, but it seems that Gradle doesn't properly add the Android JUnit Test Runner to the project's classpath. The test depenencies of my gradle build file looks like this:
testCompile 'junit:junit:4.12'
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
We are using the latest version of the support library (24.0.0), but the current version of the test runner (JUnit runner) and Espresso use version 23.1.0. To resolve the version conflict, I force the runner (and Espresso) to use the newer version (I understand the implications, but we can't use the older version):
androidTestCompile 'com.android.support:support-v4:24.0.0'
androidTestCompile 'com.android.support:appcompat-v7:24.0.0'
androidTestCompile 'com.android.support:support-v13:24.0.0'
androidTestCompile 'com.android.support:recyclerview-v7:24.0.0'
androidTestCompile 'com.android.support:design:24.0.0'
androidTestCompile 'com.android.support:support-annotations:24.0.0'
and:
configurations.all {
resolutionStrategy {
force 'com.android.support:support-annotations:24.0.0'
}
}
However, for some reason, Gradle doesn't add the runner package (under android.support.test). So
import android.support.test.runner.AndroidJUnit4;
throws an error: cannot resolve symbol 'runner'. Have cleared Android Studio's cache, restarted the IDE, cleared Gradle's cache (both project and global), all with no luck. Does anyone know what's going on?
try adding:
androidTestCompile 'com.android.support.test:rules:0.5'
This is my old question but thought it might help to explain the issue and the solution. The issue was with the name of the debug build variant: if you name your debug build variant anything but debug, make sure to notify Android's Gradle plugin by adding testBuildType "yourDebugBuildVariantName" to your build.gradle script (your app module's build.gradle not the project global) in the android{} section, or rename your debug build variant to just debug. If you have multiple debug build variants, you need to specify one of them on which you'd like to run your tests, like: testBuildType armDebug:
apply plugin: 'com.android.application'
...
android {
testBuildType "myDebug" <-
compileOptions { ... }
sourceSets { ... }
signingConfigs { ... }
}
Even with this explicit config, Gradle occasionally seems to have issues with running instrumentation tests. The best way to workaround this is to rename your debug build variant (the one you run your tests on) to debug if this is a feasible option for you.

dependencies compiled using androidTestCompile are not recognized

I am trying to integrate robotium-solo 5.5.4 into my project.
If i run this line:
compile 'com.jayway.android.robotium:robotium-solo:5.5.4'
then everything is fine,
however if I run this line:
androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.5.4'
it is like the dependency is not there.
What can I do to fix the problem ?
Try
testCompile
instead of
androidTestCompile

Differences between testCompile and androidTestCompile for junit imports

What is the difference between these two import statements? (in build.gradle):
testCompile 'junit:junit:4.12'
androidTestCompile 'junit:junit:4.12'
There's a great answer here explaining the difference:
Simply testCompile is the configuration for unit tests (located in src/test) and androidTestCompile is used for the test API (located in src/androidTest).
...
The main distinction between the two is the test sourceset runs in a regular Java JVM, whereas the androidTest sourceset tests run on an Android device (or an emulator).

Categories

Resources