Gradle 1.11 androidTest classes not running - android

So I've set up a gradle project with android and tried to get some tests to run. Unfortunately they don't seem to. It's possible that I'm missing something obvious but here goes...
I am running gradle 1.11 and as I understand the documentation that's the new folder (since 0.9 I believe?) that should be used for tests.
So I have my testclass ::
package se.coinhunter.multigradle.test;
import org.junit.Test;
import static org.junit.Assert.*;
public class HelloAndroidTests {
#Test
public void testHelper() {
assertEquals(1,1);
}
}
}
That lives in src/androidTest and here is my build.gradle:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 11
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':MultiGradleSubmodule')
}
This is a multi-project build and the submodule mentioned in the dependencies block is a plain java project that has its' own scource and unit tests running quite smoothly. I was able to specify that it should tell me when it runs its' tests and give me some feedback and that works fine. That was achieved for that project using
test {
testLogging {
events 'started', 'passed'
}
}
in its' build.gradle. I havn't come across anything like this for android projects. The whole project builds and runs, but I either can't get the tests to run, or they're running but I'm not getting any output.

You're using jUnit 4 (package name "org.junit" with #Test annotation). Android gradle only works with jUnit 3 (package name "junit.framework" with no annotations).
Android tests run in the Dalvik virtual machine on a device or emulator so your test class should also extend "AndroidTestCase" (or one of the other junit subclasses in Android - depending on what you're testing).
UPDATED: also add the following to default config:
testInstrumentationRunner "android.test.InstrumentationTestRunner"
testFunctionalTest true
Run the test using
gradle connectedCheck

Related

Cannot resolve symbol 'test' in Android Support library

I'm just starting to play around with Android and I'm having trouble with some androidTest examples that I've come across. Basically, I get a Cannot resolve symbol 'test' error message from Android Studio in the import statements for the InstrumentationRegistry and AndroidJUnit4 classes of my instrumented test file:
ExampleInstrumentedTest.java
package com.example.androidtestexample;
import android.content.Context;
import android.support.test.platform.app.InstrumentationRegistry;
import android.support.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
#RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
#Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.example.androidtestexample", appContext.getPackageName());
}
}
As a consequence, the AndroidJUnit4 symbol of the #RunWith statement can't be resolved either and when I try to run the instrumentation test I get an 'Edit configuration' window with an 'Instrumentation runner class not specified' error.
I'm using Android Studio 3.5.3, but this example uses 'com.android.tools.build:gradle:2.2.3'. The app gradle.build is as follows (I had to add the aaptOptions statements as suggested here to get the minimum reproducible example working):
build.gradle (Module: app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.example.androidtestexample"
minSdkVersion 22
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.0.1'
testCompile 'junit:junit:4.12'
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}
As suggested in this question, I have double-checked that the debug build variant was selected and that the instrumentation test source file was in the src/androidTest/java/ folder.
I understand that this example is quite outdated and, from another of the answers to the aforementioned question and from this page, that the android.support.test package is deprecated and AndroidX should be used instead. However, from this page I also understand that it should still be possible to use the Android Support library for historical artifacts, hence my question:
Is there a way for me to fix this problem and run such old examples with the android.support.test package without needing to migrate them to AndroidX?

How to run specific test using using gradlew on command line?

I'm quite new with the Android gradle build.
I built and application and it work quite ok. Now I would like to add unit test and instrument test into it, and execute them on command line.
I have to test files:
MainActivityTest.java <== This used instrumentation test which should be executed on a device.
CustomerFragmentTest.java <== This used junit test which should be executed on JVM only.
Now from the command line in Project Directory I called
gradlew test --continue
And it executed all tests (both JVM tests and Device Tests) while I expect only the JVM tests to be run.
So:
How can I run the junit test cases on JVM only (Ignore the instrumentation test cases)?
When I call gradlew connectedCheck I got the execution failed message:
"Task 'connectedCheck' not found in root project'Android5Camera'"
How can I execute the instrumentation tests
(on Device) using command line?
Thank for any help and advice.
Here by my app.gradle config in case you need:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.huynhle.android5camera"
minSdkVersion 16
targetSdkVersion 22
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
java.srcDirs = ['src/main/java']
}
main.setRoot('src/main')
androidTest.setRoot('src/test')
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
}
As i can assume, you have put both class-files to the same directory src/test. This directory is appropriate only for unit tests, and test runner don't distinguish test files between Intsrumentation or simple JUnit. That's why the gradlew test --continue command launches both classes.
Android Instrumentation Tests classes should be located in the src/androidTest directory and should be heirs (direct or no) of InstrumentationTestCase or AndroidTestCase, then you can launch instrumentation tests using gradlew connectedCheck.

Android Studio Build Failed - Compiling local project errors

A few months ago, I decided to take some custom methods and classes that I was using in more than one project and put them in their own project called "FarmSoftLibraries" that all of my projects could reference. This allowed me to make changes or additions once. It has been working very well up until yesterday (8/23/2015) when i started getting an odd error on a new method that Gradle could not find. However Android Studio had no problem finding this method. See: https://stackoverflow.com/questions/32167734/cannot-find-symbol-method-for-custom-static-class-method. However older, non-new methods continued to work.
Today the entire build system stopped working and I'm getting hundreds of Gradle errors saying that packages and methods in FarmSoftLibraries do not exist. Still, Android Studio has no problem locating these.
D:\Scott\Android\Studio\SpellingTutor\app\src\main\java\com\farmsoft\spellingtutor\Learn.java:12: error: package com.farmsoft.farmsoftlibraries.Utils does not exist
import com.farmsoft.farmsoftlibraries.Utils.FarmUtils;
^
D:\Scott\Android\Studio\SpellingTutor\app\src\main\java\com\farmsoft\spellingtutor\Learn.java:14: error: package com.farmsoft.farmsoftlibraries.Utils does not exist
import com.farmsoft.farmsoftlibraries.Utils.Logg;
^
D:\Scott\Android\Studio\SpellingTutor\app\src\main\java\com\farmsoft\spellingtutor\utils\KeyValueDB.java:6: error: package com.farmsoft.farmsoftlibraries.Utils does not exist
import com.farmsoft.farmsoftlibraries.Utils.CsvUtil;
etc. etc. etc...
I want to stress that I've changed NOTHING in the gradle files. I have been upgrading Android Studio on the Canary path - I'm on 1.4 Preview 3 now. When this issue began I was on 1.4 Preview 2. I have no idea if this is related to this version of Studio.
My gradle scripts:
project settings.gradle:
include ':app'
include ':farmsoftlibs'
project(':farmsoftlibs').projectDir = new File('../FarmSoftLibraries/app')
app build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.farmsoft.spellingtutor"
minSdkVersion 15
targetSdkVersion 22
versionCode 28
versionName "1.15"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
applicationIdSuffix '.debug'
versionNameSuffix '.debug'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.android.gms:play-services-gcm:7.8.0'
compile project(':farmsoftlibs')
}
farmsoftlibs build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/gson-2.3.1.jar')
compile 'com.google.android.gms:play-services-vision:7.8.0'
}
I do want to admit I pretty much am ignorant about gradle - I follow the instructions for how to set it up in Android Studio, but I really don't know what I'm doing and have gotten lost in the Gradle documentation I tried to read. So this is incredibly frustrating and more so knowing that this is probably due to some stupid mistake I've made out of ignorance.
EDIT:
This seems to have something to do with ProGuard - if I change the library's release settings to say minifyEnabled false, then the error goes away... for now. I'd like to know why.

enabling Robotium in Android

I recently decided to switch from Eclipse to Android Studio. While i was able to import my android project, i keep having problems setting up and transfering my unit tests. For testing purposes i made a directory in src folder (java folder and test package). For enabling robotium i followed another stack topic by adding androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.2.1'. Eventually i added my test java file from Eclipse. And when i try to run tests, i keep getting "Cannot resolve symbol Solo".
I have little understanding about how AS and gradle works, so probably i am missing something else.
The contents of my build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.colormindapps.work_rest__scheduler"
minSdkVersion 8
targetSdkVersion 21
testApplicationId "com.colormindapps.work_rest__scheduler.test"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
sourceSets {
main {
java.srcDirs = ['src/main/java', 'src/tests/java']
}
}
}
dependencies {
androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.2.1'
compile 'com.android.support:support-v4:19.0.+'
compile 'com.android.support:appcompat-v7:19.0.+'
}
I think the easiest way to make it work is to install Robotium Recorder for Android Studio. After you record a test you can see how Robotium Recorder sets up the gradle files etc.
http://robotium.com/pages/installation-android-studio
You must use separate source set for you android-specific tests, i.e.:
sourceSets {
main {
java.srcDirs = ['src/main/java']
}
androidTest {
java.srcDirs = ['src/tests/java']
}
}
Use androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.5.4' which resolve all test cases issues

Test running failed: Unable to find instrumentation info for: ComponentInfo{} -- error trying to test in IntelliJ with Gradle

Everytime I try to run my tests the console says this:
Running tests
Test running startedTest running failed: Unable to find instrumentation info for:
ComponentInfo{com.employeeappv2.employeeappv2.test/android.test.InstrumentationTestRunner}
Empty test suite.
I've been stuck on this for a while and the solutions I've seen online so far have not helped.
My project structure is set up like this:
*Main Module
-src
*instrumentTest
-java
*main
-java
-manifest
*build.gradle
My build.gradle file looks like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
minSdkVersion 16
targetSdkVersion 19
versionCode 1
versionName "2.1.0"
testPackageName "login.test"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard- rules.txt'
}
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/scandit.zip')
compile project(':pullToRefresh')
compile 'com.android.support:appcompat-v7:19.+'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.1+'
compile 'org.springframework.android:spring-android-rest-template:1.0.1+'
compile 'org.json:json:20090211'
compile 'com.fasterxml.jackson.core:jackson-databind:2.3.1'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.3.0'
compile 'com.fasterxml.jackson.core:jackson-core:2.3.1'
compile 'com.android.support:support-v4:19.1.+'
compile 'com.mcxiaoke.volley:library:1.0.+#aar'
androidTestCompile 'junit:junit:3.8'
}
Do you need to have a separate manifest for your tests directory? If so what would that look like?
Edit: I tried adding a manifest to my instrumentTest directory with no luck. Note that I could not get IntelliJ to resolve the targetPackage name, so it appears red.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.employeeappv2.employeeappv2.src.instrumentTest"
android:versionCode="1"
android:versionName="1.0.0">
<application>
<uses-library android:name="android.test.runner" />
</application>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.employeeappv2.employeeappv2.src.main"/>
</manifest>
I had the same error when I tried adding multiDexEnabled true to build.gradle.
I'm adding my experience here, because this is one of the first Google hits when searching with the ... Unable to find ... ComponentInfo ... error message.
In my case adding testInstrumentationRunner like here did the trick:
...
android {
...
defaultConfig {
...
testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
}
}
(I have com.android.tools.build:gradle:1.5.0)
I am using Android Studio 1.1 and the following steps solved this issue for me:
In Run - Edit Configurations - Android Tests
Specify instrumentation runner as android.test.InstrumentationTestRunner
Then in the "Build variants" tool window (on the left), change the test artifact to Android Instrumentation Tests.
No testInstrumentationRunner required in build.gradle and no instrumentation tag required in manifest file.
When I created a new package, Studio created an ApplicationTest class. Using us.myname.mypackage as an example, the following directory structure was created:
app/[myPackage]/src/androidTest/java/us/myname/mypackage/ApplicationTest.class
Initially it worked out of the box. It quit working after I installed product flavors. I subsequently made the following changes to build.gradle:
defaultConfig {
...
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
some prefer
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
(With my current configuration, I have to use ...InstrumentationTestRunner when in debug, and AndroidJUnitRunner while using release build type.)
The above configuration only works with the debug build type. If you wish to use it with release or with a custom build type, you can include the following in build.gradle:
buildTypes {
release {
...
}
debug {
...
}
}
testBuildType "release"
In the Build Variants tab on the lower left side of Studio, make sure you have Android Instrumentation Tests and the correct buildType selected.
I had to do a combination of VikingGlen's answer, Liuting's answer, and this answer. This answer works for Android Studio version 2.1.
Run -> Edit Configurations... -> General -> Specific instrumentation runner (optional): "android.support.test.runner.AndroidJUnitRunner"
In build.gradle (the one with all your dependencies), put this:
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Then it could run tests of this type:
#RunWith(AndroidJUnit4.class)
#LargeTest
public class ApplicationTest {
#Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);
#Test
public void login() {
//Open Drawer to click on navigation.
onView(withId(R.id.drawer_layout))
.check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed.
.perform(open()); // Open Drawer
}
}
For what it's worth, AS 2.3 got hung up when i created a custom test runner after using the regular test runner. I got the same error as posted in the question.
Deleting the Debug Configurations for ALL Android Instrumented Tests and rebuilding fixed it. I believe the problem lied in the fact you no longer can choose a custom runner in the Debug Configurations because it's most likely built in via gradle.
So the main problem was that when I created an androidTest folder under /src/, it wasn't being picked up by IntelliJ as a source folder for testing (java subdirectory should turn green). I was using IntelliJ 13.0.3 and after upgrading to 13.1.3, all of my troubles went away.
*Note: do not try to add a manifest to your androidTest folder, the Gradle docs specifically state that the manifest should be auto-generated when you create the androidTest folder. The problem for me was that the file wasn't being generated as androidTest wasn't being recognized by IntelliJ/Gradle, thus throwing the no instrumentation error.
I had this problem and fixed it by going to Run -> Edit Configurations -> Green '+' button at the top left -> JUnit
From there, set the 'use the classpath mod...' to 'app' (or your default app name, which is the one that appears to the left of the run (play button) when you run the app)
Finally, put your test class name in the 'class:' textbox. Click apply and okay.
At this point, if the test class doesn't have other errors, it should work.
This is what I noticed in my project, in my app(main) module build.gradle I had the following buildType configuration
buildTypes {
debug {
multiDexEnabled true
}
mock {
initWith(buildTypes.debug)
}
}
testBuildType "mock"
When I used AndroidJUnitRunner as the test runner(both from Android Studio) and as testInstrumentationRunner in build.gradle, tests ran without hitch.
In a submodule that had multiDexEnabled true as defaultConfig
defaultConfig {
multiDexEnabled true
....
}
I ran into the problem of
Test running startedTest running failed: Unable to find instrumentation info for:{mypackage.x.y/android.support.test.runner.AndroidJUnitRunner"}
when I specified AndroidJUnitRunner in IDE and the submodule build.gradle. And this was fixed by specifying MultiDexTestRunner as the test runner in IDE/build.gradle.
To summarize, Use MultiDexTestRunner to run tests when multiDexEnabled true is specified in build.gradle, else use AndroidJUnitRunner as the test runner.
Make sure the app has been uninstalled for all users.
Go to settings -> apps (all apps) -> If your app is there then tap on it -> menu -> uninstall for all users.
My issue was that the app was at one point uninstalled, however still on the device; meaning it was not uninstalled for all users. (Another issue, not sure how to resolve. I'm the only user on my device)
Because of this, the app wouldn't re-install, and the test suite had nothing to run against.
The solution for my problem is to change the method name from
#Test
public void test() {
...
}
to
#Test
public void testSomething() {
...
}
Hope it helps someone.

Categories

Resources