Android unit testing: connectedAndroidTest does not execute my test case - android

I am trying to add some unit tests to my app. I am developing my app in Android Studio
This is what I did.
Added a new package
Created a class in new package which extends TestCase
Added following method to created class
#SmallTest
public void basicTest() {
assertEquals("abc", "abc");
}
Added following to defaultConfig section in build.gradle
testApplicationId "newly.added.package.name"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
Executed following command in Android Studio Terminal
gradlew.bat connectedAndroidTest
But, when I checked the generated html report, it shows that 0 tests were run.
I tried following, but none of them made a difference to the output of gradlew.bat connectedAndroidTest command.
Added an incorrect package name to testApplicationId in build.gradle.
Disconnected device
Why doesn't my test case get executed? What have I missed?
Following is my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "my.package.name"
minSdkVersion 12
targetSdkVersion 18
testApplicationId "my.package.name.tests"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile project(':my_sub_module1')
compile project(':my_sub_module2')
compile 'com.android.support:support-v4:21.0.2'
compile files('libs/my_dependent_lib-1-7-4.jar')
compile 'com.google.code.gson:gson:2.3'
}
repositories {
mavenCentral()
}

You should use the following testInstrumentationRunner:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Also, click Build Variants on the left side of Android Studio, and select
Android Instrumentation Tests
in the dropdown.

Related

Cannot resolve symbol (after Android Studio update) but the project builds for app/library project

I have an an Android Studio project A which is an app, and a project B which is a library project. I have included project B in project A. Project B has some dependencies (android async http, and org.json) that are used in both project B and project A. I upgraded Android Studio to 2.2 Preview 2, and am now having an issue where all of my usages of android async http are saying Cannot Resolve Symbol. However, my JSON usages are just fine. When I build the project, I get no errors and can run it just fine. How can I fix the Cannot Resolve Symbol issue in Android Studio?
I have tried
cleaning & rebuilding both projects
making a change in the app's build.gradle & syncing grade
re-arranging the order of dependencies
sync project with gradle files
delete android async http.xml from .idea/libraries & sync project with gradle files
opening and closing android studio
setting minifyEnabled=true in the app & library
Is there something I am missing?
app > build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
minSdkVersion 19
targetSdkVersion 23
versionCode 33
versionName "2.3"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
}
useLibrary 'org.apache.http.legacy'
}
dependencies {
compile project(':mylibrary')
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:support-v13:21.0.3'
}
library > build.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
versionCode 2
versionName "1.3"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
useLibrary 'org.apache.http.legacy'
}
dependencies {
compile files('libs/org.json-20120521.jar')
compile files('libs/android-async-http-1.4.5.jar')
}

Debugging test case in Android Studio

How do you debug JUnit test case in Android studio? I placed a breakpoint and used "Debug Test case" but it just runs the code and the breakpoint is ignored.
Update:
This is content of my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.****.mancala"
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
}
Depending on the Run/Debug Configuration used, the same code with the same breakpoint will stop or not.
For a configuration with Test kind: All in package, the execution
will not stop at breakpoint
For a configuration with Test kind: Class or Test kind: Method, the execution will stop at breakpoint
For a configuration with Test kind: All in directory with Directory: [myProjectPath]/app/src/test, the execution will stop at breakpoint
Make sure that in your build.gradle file (one with all your dependencies) that you have debuggable true within the debug build type.
Also just for good measure make sure its a valid breakpoint (not an empty line etc.)

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.

Android test cannot find source class

I'm trying to configure a android app to run tests with robolectric.
I'm new to gradle, and I can't find out what the problem is.
When I run ./gradlew robolectric on the command line, I get a cannot find symbol error.
The error I get is:
MainActivityTest.java:18: error: cannot find symbol
Robolectric.buildActivity(MainActivity.class).get().getResources();
^
symbol: class MainActivity
location: class MainActivityTest
This is my build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'robolectric'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.app.id"
minSdkVersion 16
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
sourceSets{
main.setRoot('src/main')
androidTest.setRoot('src/test')
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile 'org.robolectric:robolectric:2.3'
androidTestCompile 'junit:junit:4.11'
}
So, the problem seems to be that I cannot access the source file when I run the tests, but I couldn't find a way to fix it.
Can anyone tell me what i'm doing wrong?
Thanks!
main.setRoot('src/main') => Is it really a main folder / package in your project?
You can test again by using the fully file directory, ex: src/helloword/mainActivity.java

Gradle build fails in Android Studio

I am using Android Studio and created a new project, which created the standard "Hello world" mock.
My gradle.build file has the following contents:
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.example.whatever.blabla"
minSdkVersion 19
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
Even though I did not change anything, Gradle still fails when trying to build it with the error:
Failed to complete Gradle execution.
Cause:
A fatal exception has occurred. Program will exit.
First of all, is there any way of getting extra info about the problem? And secondly, why is this happening since I only created a standard project?
I had the same problem and could fix it by following solution provided in the below link -
https://code.google.com/p/android/issues/detail?id=67946
Though I am not sure how setting the maxheapsize has fixed the issue.

Categories

Resources