Error "No tests found" when running Android instrumentation tests - android

I am a beginner to testing. I have created a simple test case for login activity in android studio. But I got an error and I could not solve it. Here is my test code. Help will be really appreciated.
package com.example.hassidiczaddic.testinglist;
import android.app.Application;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.ApplicationTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
#RunWith(AndroidJUnit4.class)
#LargeTest
public class ApplicationTest {
public static final String STRING_TO_BE_TYPED = "Wolfmatrix";
#Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);
#Test
public void LoginActivity() {
onView(withId(R.id.etFName))
.perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
onView(withId(R.id.etLName))
.perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
onView(withId(R.id.btnSubmit))
.perform(click());
onView(withId(R.id.tvView))
.check(matches(withText(STRING_TO_BE_TYPED)));
}
}
This is my error:
Running tests
$ adb shell am instrument -w -r -e debug false -e class
com.example.hassidiczaddic.testinglist.ApplicationTest
com.example.hassidiczaddic.testinglist.
test/android.test.InstrumentationTestRunner
Client not ready yet..Test running started
junit.framework.AssertionFailedError: No tests found in
com.example.hassidiczaddic.testinglist.ApplicationTest
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at
android.test.InstrumentationTestRunner.onStart
(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run
(Instrumentation.java:1619)
Tests ran to completion.
Here is my gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.example.hassidiczaddic.testinglist"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
// App dependencies
compile 'com.android.support:support-annotations:23.0.1'
compile 'com.google.guava:guava:18.0'
androidTestCompile 'com.android.support:support-annotations:23.0.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
}
}

You have forgotten to set AndroidJUnitRunner as the default test instrumentation runner.
https://developer.android.com/topic/libraries/testing-support-library/index.html
To set AndroidJUnitRunner as the default test instrumentation runner in your Gradle project, specify this dependency in your build.gradle file:
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
Update
with androidx things have changed a little, check https://developer.android.com/training/testing/set-up-project

If you are targeting SDK Version 28, you'll be using the re-packaged androidx classes from the support library, so AndroidJUnitRunner is configured like so...
android {
defaultConfig {
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
}

check build.gradle
replace testInstrumentationRunner "androidx.test.runner.AndroidJUnit4"
android {
defaultConfig {
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
}
Look for app/src/androidTest/AndroidManifest.xml
change android:name to androidx.multidex.MultiDexApplication
<application
android:allowBackup="true"
android:label="Components Tests App"
android:supportsRtl="true"
android:name="android.support.multidex.MultiDexApplication">
To
<application
android:allowBackup="true"
android:label="Components Tests App"
android:supportsRtl="true"
android:name="androidx.multidex.MultiDexApplication">
More suggestions:
No tests found when running instrumented tests with AndroidX

If you landed here by generating the tests from the method... Try renaming the test method in your test class.. to something similar to actualMethod() to testActualMethod()

Check the logcat!
This can happen for a multitude of reasons and the build logs aren't always that helpful, even when using the --stracktrace --info, or --debug flags.
In my case, this was due to a missing runtime dependency. I only figured this out after checking the logcat and seeing the error there.
If you've checked all the obvious things mentioned in the other answers and your still having this issue, check the logcat for errors.

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?

No tests found when running instrumented tests with AndroidX

I'm trying to run the standard ExampleInstrumentedTest in my Android project (which uses AndroidX), but get "No tests found" error instead. I've looked through the other questions and the documentation and I'm pretty sure I've done everything right, but maybe I'm overlooking anything?
Here is my app's build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 24
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.ext.junit.runners.AndroidJUnit4"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// Gradle automatically adds 'android.test.runner' as a dependency.
useLibrary 'android.test.runner'
useLibrary 'android.test.base'
useLibrary 'android.test.mock'
}
repositories {
mavenCentral()
maven {
url("https://oss.sonatype.org/content/repositories/snapshots")
}
maven { url 'https://jitpack.io' }
}
dependencies {
// Core library
androidTestImplementation 'androidx.test:core:1.0.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test.ext:truth:1.0.0'
androidTestImplementation 'com.google.truth:truth:0.42'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}
// More dependencies...
and ExampleInstrumentedTest.java:
import org.junit.Test;
import org.junit.runner.RunWith;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import static org.junit.Assert.*;
#RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
#Test
public void useAppContext() {
Context appContext = ApplicationProvider.getApplicationContext();
assertEquals("XXX", appContext.getPackageName());
}
}
when I run the code, I get "No tests found".
I replaced -
testInstrumentationRunner "androidx.test.runner.AndroidJUnit4"
with -
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
I needed to replace my runner
"androidx.test.ext.junit.runners.AndroidJUnit4"
with
"androidx.test.runner.AndroidJUnitRunner"
Look for app/src/androidTest/AndroidManifest.xml
change android:name to androidx.multidex.MultiDexApplication
<application
android:allowBackup="true"
android:label="Components Tests App"
android:supportsRtl="true"
android:name="android.support.multidex.MultiDexApplication">
To
<application
android:allowBackup="true"
android:label="Components Tests App"
android:supportsRtl="true"
android:name="androidx.multidex.MultiDexApplication">
I got the "No tests found" issue fixed by following the instructions of Jim Andreas here: https://github.com/googlecodelabs/android-testing/issues/27
"If I click on the package and not the test, then AS does find the test and executes it. "
In that way, the test was run as "Android Instrumented Tests" rather than "Android JUnit". I think that is the root cause of the issue.
Its a silly mistake, but for me I forgot to add
#Test
above my test function.
For me this was caused by running Instrumented tests on an emulator with API 30 (Android 'R').
When I switch to another emulator with API 29 (Android 'Q') the error goes away.
Be sure to have this
android {
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
and this in dependencies
androidTestImplementation 'androidx.test:runner:1.2.0'

Android Studio Junit tests package org.junit does not exist

I'm trying to implement a test class in Android Studio to make some test on a DBAdapter. Since I need to run the test on my mobile phone in order to use the database, I have created an Instrumentation Unit Test (cause I've tried to do it just with a Unit test but I need to use the database and so, and those are only run locally).
The problem is that when I try to run the test class, using my mobile phone as running device, the compiler throws the following error:
error: package org.junit does not exist
I've been looking for a solutione, but I found none.
This is my test class (just the skeleton):
import org.junit.Test;
import android.support.test.runner.AndroidJUnit4;
import static org.junit.Assert.*;
public class DbAdapterTest {
#Test
public void testCreateSeries() throws Exception {
}
}
And this one is the build.gradle script:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.w2w.whattowatch"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests.returnDefaultValues = true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
}
I also have another issue. As you can see, I also imported this:
import android.support.test.runner.AndroidJUnit4;
But, even before running, it says of "runner" that "cannot resolve the symbol". I've added the TestInstrumentationRunner on build.gradle, but still not working.
OK, I solve it so, this is the solution that worked for me.
I didn't have this dependences, so I add them to the build.gradle script:
androidTestCompile 'com.android.support:support-annotations:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'

Android Espresso not working with Multidex gives "No tests found"

My Espresso tests were running until I had to support multidex.
My build.gradle, I have
minSdkVersion 14
targetSdkVersion 23
multiDexEnabled = true
testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
dexOptions {
jumboMode true
javaMaxHeapSize "4g"
incremental true
}
Test1AuthenticationEspressoTest
#RunWith(AndroidJUnit4.class)
#SmallTest
public class Test1AuthenticationEspressoTest {
#Rule
public ActivityTestRule<WelcomeActivity> mActivityRule = new ActivityTestRule(WelcomeActivity.class);
}
Here is the Error I get
junit.framework.AssertionFailedError: No tests found in
com.livestrong.tracker.test.Test1AuthenticationEspressoTest
Any help will be appreciated. Any one has espresso working with multidex ?
I was having this same problem and it turns out you need to build a custom runner that enables MultiDex and extends from the AndroidJUnitRunner. You then need to set that runner as your testInstrumentationRunner in the build.gradle, and as your runner in your run configuration. There is no need to modify the test class (keep the #RunWith(AndroidJunit4.class)).
Here's a step-by-step of what to do:
Create a class for your custom runner:
package com.bla.bla.bla; // your package
import android.os.Bundle;
import android.support.multidex.MultiDex;
import android.support.test.runner.AndroidJUnitRunner;
public class CustomTestRunner extends AndroidJUnitRunner
{
#Override
public void onCreate(Bundle arguments)
{
MultiDex.install(getTargetContext());
super.onCreate(arguments);
}
}
In your build.gradle, set the runner to your custom runner:
android {
// ...
defaultConfig {
// ...
testInstrumentationRunner "com.bla.bla.bla.CustomTestRunner"
}
}
In your run configuration, make sure the instrumentation runner is also set to the same runner.. Note: This step should not be required on Android Studio 3.x and maybe also some previous versions. This option does not exist anymore.
Using the above, I was able to run Espresso tests on our multi-dex enabled app.
I should note that many other posts on the net regarding this topic, suggest setting your runner to com.android.test.runner.MultiDexTestRunner and exculde some dependencies in com.android.support:multidex-instrumentation:1.0.1 in your build.gradle. That solution appears to no longer be the case and doesn't work as of gradle 1.5.0. If you have any of that stuff set, then it'll prevent the above from working. See the comments in this stack overflow post for more information.

"Empty suite" with espresso test

I am trying to run a simple "hello!" test but I get the empty test suit, I can find the problem this is my gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "sku1l.eccc"
minSdkVersion 17
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
packagingOptions {
exclude 'LICENSE.txt'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:support-annotations:22.2.0'
androidTestCompile 'com.android.support:support-annotations:22.2.0'
androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2'){
exclude group: 'javax.inject'
}
androidTestCompile 'com.android.support.test:runner:0.3'
// androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2'
}
And this is the test:
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
#RunWith(AndroidJUnit4.class)
public class ApplicationTest {
#Rule
public ActivityTestRule<MainActivity> mActivityTestRule= new ActivityTestRule<>(MainActivity.class);
#Test
public void testVisibility() {
onView(withText("Hello"))
.check(matches(isDisplayed()));
onView(withId(R.id.Hello)).check(matches(withText("Hello")));
}
}
this is the logcat
Success
Uploading file
local path: C:\Users\sku1l\AndroidStudioProjects\elparche\app\build\outputs\apk\app-debug-androidTest-unaligned.apk
remote path: /data/local/tmp/sku1l.elparche.test
No apk changes detected. Skipping file upload, force stopping package instead.
DEVICE SHELL COMMAND: am force-stop sku1l.elparche.test
Running tests
Test running startedFinish
Empty test suite.
I also edit configuration an set the specific instrument runner : android.support.test.runner.AndroidJUnitRunner
when I do it I get this in the logcat
Running tests
Test running startedTest running failed: Unable to find instrumentation info for: ComponentInfo{sku1l.elparche.test/android.support.test.runner.AndroidJUnitRunner}
Empty test suite.
I hope you can help me... Thanks
ok finally i get the solution for this, the proguard was the problem, you can exclude or remove the proguard while testing and all will be running just fine, hope it will help more people

Categories

Resources