I am converting all of my tests over to the Testing Support Library. However, when I try to import the LargeTest annotation like this
import android.support.test.filters.LargeTest;
I get Cannot resolve symbol 'LargeTest'. What dependency do I need to add to my gradle file to resolve this error?
Have you gone through this documentation?
You need to add some of these dependencies based on your need.
dependencies {
androidTestCompile 'com.android.support.test:runner:0.4'
// Set this dependency to use JUnit 4 rules
androidTestCompile 'com.android.support.test:rules:0.4'
// Set this dependency to build and run Espresso tests
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
// Set this dependency to build and run UI Automator tests
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}
And add :
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
The filter annotation lives under the following dependency
androidTestCompile 'com.android.support.test:runner:0.5'
If it's still not working please file a bug.
Edit: The issue is that it was introduced in version 0.5 so you have to update your dependency.
For AndroidX:
import androidx.test.filters.LargeTest
https://developer.android.com/reference/androidx/test/filters/LargeTest
Related
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.
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.
I'm trying to import
import android.support.test.InstrumentationRegistry;
my build.gradle file
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test:runner:0.2'
androidTestCompile 'com.android.support.test:rules:0.2'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
in default config:
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Is there a library I'm missing here? I'm trying to import InstrumentationRegistry but it doesn't recognise it!
Check what kind of test do you use.
InstrumentationRegistry used for Instrumented tests
that use emulator or device and they are placed in src/androidTest and use config androidTestCompile.
If you use Local unit tests for JVM from folder src/test you should use
config testCompile
testImplementation 'com.android.support.test:runner:1.0.2'
After that you can import InstrumentationRegistry, but you will get other errors at run-time.
try
compile 'com.android.support.test:runner:0.2'
instead of
testCompile 'com.android.support.test:runner:0.2'
it seems, com.android.support.test had been recently excluded from some other package (no clue which one), which also resulted in android.support.test.InstrumentationRegistryto be unknown; not excluding it from com.android.support.test:runner fixed the issue for me.
androidTestImplementation ("com.android.support.test:runner:1.0.2") {
// exclude group: "com.android.support.test"
exclude group: "com.android.support"
}
basically, androidTestImplementation needs to contain com.android.support.test once.
Another alternative - you can also add androidx.test:monitor dependency where the class InstrumentedRegistry is.
androidTestImplementation 'androidx.test:monitor:x.y.z'
Running tests via Android Studio, Gradle
Espresso:
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
Library:
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
Runner:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Error:A problem occurred configuring project ':application'.
> Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app and test app differ.
Just remove the androidTestCompile 'com.android.support.test:testing-support-lib:0.1' from your build.gradle. Take a look on example here.
Get comfortable with command-line Gradle. This tool help you find any conflicting dependencies when you run from console that command gradle -q app:dependencies.
Instead of deleting androidTestCompile 'com.android.support.test:testing-support-lib:0.1' from your app's build.gradle file, you can add exclude part like here.
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.0') {
exclude module: 'support-annotations'
}
Hope it help.
import static android.support.test.espresso.Espresso.onView;
import static android.support.test..assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import android.support.test.espresso.action.ViewActions;
#org.junit.runner.RunWith(android.support.test.runner.AndroidJUnit4.class)
#android.test.suitebuilder.annotation.LargeTest
Build.gradle
dependencies
{
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
androidTestCompile 'com.android.support.test:rules:0.3'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'
}
}
Q : I am trying to add imports in to a testing class . i have added all the required dependencies in build.gradle of project module .
it is an old project built into eclipse and now migrated to android studio project.
there is no androidTest folder in android studio project , so i manually crated src/androidTest/java/example/Test.java
Added Junit4 dependency too but still imports are not working .
Followed -: https://code.google.com/p/android-test-kit/wiki/EspressoSetupInstructions
any help would be appriciated .
Specify your variant explicitly if it is not debug
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Basics-and-Configuration
Currently only one Build Type is tested. By default it is the debug
Build Type, but this can be reconfigured with:
android {
...
testBuildType "staging" }
Since you are doing androidTestCompile the libraries will be compiled for builds which are testable.
Try changing your build variant to a debug one and sync your project with gradle files.