Related
Right clicking on a package and selecting Run Tests... results in the message No tests were found. In earlier versions of Android Studio my tests were found and would run using this method. Running test classes individually seems to work still. I've tried Invalidate Cache / Restart, but still running into the same issue.
Logs:
---- IntelliJ IDEA coverage runner ----
sampling ...
include patterns:
exclude patterns:0 test classes found in package '<default package>'
Process finished with exit code 254
Empty test suite.
Any point in the right direction would be appreciated. Thanks!
I've seen this happen when AS tries to run unit tests (test/ folder) instead of instrumentation tests (androidTest/ folder) or the package specification for the configuration is wrong.
Make sure that when you right click a folder to run the package of tests, and select "Run", that the icon has a little droid on it to indicate it's treating the folder as android tests:
If it doesn't, continue to step 2. If it does, skip to step 3.
Sometimes AS gets confused and thinks Android tests are unit tests. To fix that, click the Run Configuration icon in the toolbar and select "Edit Configurations..."
You should see an option for Android Tests:
Select this and copy that configuration, then on the right, change it from "All in Module" to "All in Package"
Make sure the configuration for the package tests specifies the full package you want to test:
Once your configuration is correctly set up to run Android tests and explicitly indicate the full and correct package of tests to run, it should work.
Hope that helps!
Fixed in Android Studio version 3.2.1
Invalidate caches and restart fixed this for me, always worth trying.
If you are using JUnit 5 and already have JUnit 4 synced in your build.gradle then make sure that #Test annotation should be annotated from import org.junit.jupiter.api.Test
This is classic. Some of these steps might help:
Kill Gradle daemons: ./gradlew --stop
Reimport the project: close the Android Studio window, delete the project from the list of recent projects, Import again.
Report it to the Tools team: https://developer.android.com/studio/report-bugs
In my case I inadvertently had both JUnit4 and JUnit5 on the classpath.
Even though I was trying to use JUnit5 in my tests, one of my test dependencies had a transitive dependency on the older version.
After cleaning up the classpath Android Studio (3.2.1) was able to find the tests again.
I'm trying to test using the following directory structure (which was setup by Android Studio):
I can run some tests just fine, and even the AllTests.java runs fine without the AndroidManifest.xml file even being there. The thing is, for one of my new tests, I need the android.permission.INTERNET permission. So, I added the following to the AndroidManifest.xml file located within the androidTest directory:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.core"
android:versionCode="2"
android:versionName="2.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Unfortunately, this doesn't work. I'm still getting the following error when I run one of my tests:
E/RestAPIRequestTest﹕ Permission denied (missing INTERNET permission?)
I've tried setting the package to be com.example.core.test in my AndroidManifest.xml file (since that is what it shows up as in my Settings->Apps list), but with no joy.
I'm thinking it's not even recognizing the AndroidManifest.xml file, since the version number doesn't show in the Settings for the test app, either.
How can I inject the correct permissions for my test project?
I needed to do something similar. I created a folder named "debug" next to androidTest, which corresponds to the debug variant of the app, and put an AndroidManifest.xml with the permission in that folder. Then the permission works under test since the test app uses the debug variant. It's not ideal because it blurs the line between test and debug, which aren't quite the same thing.
I think what's happening is that the permissions in androidTest/AndroidManifest.xml are going to the test app, not the target app, although it's not 100% clear to me if there are actually two different APKs or what.
In older versions of Android Studio and the Android Gradle plugin the androidTest/AndroidManifest.xml file was ignored. This was documented at the tools.android.com site at the time.
With the Android Studio 1.0+ and Android Gradle 1.0+ plugin launch in December 2014 the AndroidManifest.xml file should now be merged with the normal main/AndroidManifest.xml files (in addition to the debug and release manifest files if they exist). More details regarding the manifest merging rules are here.
If you still run into issues or are just debugging manifest related testing issues try this
(Adapt this slightly for Windows):
Drop to a terminal
change to to your project directory
cd MyApplication
Build your project, assuming 'debug' is the build type you want to test with, but you could also be testing with 'release' or a build script defined one.
./gradlew assembleDebugTest
Then inspect your test APK manifest:
ls app/build/intermediates/manifests/test/debug/AndroidManifest.xml
View your application APK manifest:
ls app/build/intermediates/manifests/full/debug/AndroidManifest.xml
A merge output log can be found detailing the manifest merging process:
ls app/build/outputs/apk/manifest-merger-debug-report.txt
A couple of extra notes:
An instrumentation element is automatically added to your test APK's AndroidManifest.xml so you should only be adding extra activities, permissions, etc that your test APK needs.
If testing with mock locations your application APK will need the ACCESS_MOCK_LOCATION permission. You can add the permission to your debug/AndroidManifest.xml file or you can define that the test APK and the application APK should use the same userId when deployed (sharedUserId attribute in your AndroidManifest.xml).
This is a known problem.
Currently (AGP <= 3.4.X) is not supporting AndroidManifest test merging.
This is reported here: https://issuetracker.google.com/issues/127986458
and here there is the issue created by one of the Roboelectric maintainers.
The workaround as described here its near the same proposed by user3286293 and currently is the only way to have the manifest merged for testing purposes.
Hope to see a fix for AGP 3.5 or 3.6
As specified here, during instrumented tests, there are generated two .apk files. If you take a look, the smaller one it's most probably the one named app-debug-androidTest-unaligned.apk and it actually does include the provided permissions.
Inspecting the file with aapt d permissions <apk_file_path>.apk can be useful to see a list of all of them.
Now, there might be an issue with the context itself where the permission is requested. I had a similar problem, trying to write some screenshots on SD card (thus needing the WRITE_EXTERNAL_STORAGE permission).
This answer helped me to fix the problem, although I cannot fully understand why it's necessary.
In few words, you'll need to declare the same android:sharedUserId in both manifests, in order to merge the permissions when both apks are installed on the same device - that happens when tests are running.
This helped me to separate permissions needed just for testing from the one in production.
You need to define that in build.gradle file:
android {
sourceSets {
androidTest.manifest.srcFile "src/androidTest/AndroidManifest.xml"
}
}
One solution would be like build main apk and test apk in single run.
Example: ./gradlew clean :main:assembleDebug :main:assembleDebugAndroidTest.
This will create a new instrumented main application which has all extra permissions required for test application.
I feel there is much confusion around how to use JUnit to test an Android Library project.
I will describe all the errors I encountered so far(for the sake of other programmers trying to do the same) and at the end I will specify my last and current problem.
Since a Library is not an Application, it does not produce an .apk file in the bin directory, just a .jar file, and you need an .apk to use Android JUnit testing
So you definitely need to create a new project for the tests, follow this guide
Note: Even though the guide tells you to use TestCase and its derived class AndroidTestCase, several posts seem to point that TestCase is used only for JUnit3 tests, JUnit4 tests do not extend any class.
I don't know what I am supposed to use when you want to run a test that uses the Android API, if you want to use it under JUnit4, I have not reached that point yet.
For tests that do not use the Android API, use JUnit4 style class. If you follow only the guides your test will fail when you run it "Invalid layout of ... at value". This is because the run configuration includes Android in its classpath. Go to the JUnit test run configuration for the test (Right click on the test->Properties->Run/Debug Settings->Edit) and remove Android from classPath boostrap entries.
EDIT
When you run the test for the first time, in Eclipse, you have to choose which launcher to use "Eclipse JUnit Launcher" or "Android JUnit Test Launcher" if you choose the Android launcher, no need to remove the bootstrap entry.
From now on I had no success. For tests that use the Android API, extend AndroidTestCase. These tests will take longer to launch, they need an emulator running, even if you are not testing the application itself but a utility class with no interface to the user.
To run Android UNIT case test the launcher looks for the target package APK file, which a Library target project does NOT have.
The target package was automatically inserted in the manifest of your test project when you followed the guide in #2. The manifest for the test project should look like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Common.lib.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.Common.lib" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<uses-library android:name="android.test.runner" />
</application>
</manifest>
Here the target package is the Library I am trying to test. A solution is given in this post It is to use the test project as application which has an APK. So now I changed the target package from
android:targetPackage="com.Common.lib" />
to
android:targetPackage="com.Common.lib.test" />
But still the launcher looks for the Library APK, even though I added the Library as reference in the test project, see the logcat below (Note the CommonLibTest.apk is loaded succesfully):
[2014-02-24 16:17:50 - CommonLibTest] Dx
trouble writing output: already prepared
[2014-02-24 16:17:51 - CommonLibTest] ------------------------------
[2014-02-24 16:17:51 - CommonLibTest] Android Launch!
[2014-02-24 16:17:51 - CommonLibTest] adb is running normally.
[2014-02-24 16:17:51 - CommonLibTest] Performing android.test.InstrumentationTestRunner JUnit launch
[2014-02-24 16:17:51 - CommonLibTest] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'Phone-x86-jb'
[2014-02-24 16:17:51 - CommonLibTest] Uploading CommonLibTest.apk onto device 'emulator-5554'
[2014-02-24 16:17:52 - CommonLibTest] Installing CommonLibTest.apk...
[2014-02-24 16:17:52 - CommonLibTest] Success!
[2014-02-24 16:17:52 - CommonLib] Could not find CommonLib.apk!
[2014-02-24 16:17:52 - CommonLibTest] Launching instrumentation android.test.InstrumentationTestRunner on emulator-5554
[2014-02-24 16:17:53 - CommonLibTest] Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'
This is as far as I got.
If want to believe someone already has tested a Library project succesfully with JUnit and has the solution to my problem.
To complete the tale, I solved the last obstacle and the test now runs.
Lets start with the bottom line:
AndroidTestCase requires JUnit3 tests. The annotation #Test fails the test with ClassNotFound for the org.junit.Test class.
Once I commented out all #Test annotations the test started to run.
One more guideline: test names should start with the prefix "test" for JUnit3
Strangely the JUnit4 annotation #BeforeClass did not trigger a failure.
Conclusion:
JUnit4 test styles is only good for pure Java Unit tests. For Android Unit tests use JUnit3 style, since JUnit4 is backwards compatible it will compile fine.
Please note that this post refers only to Unit testing, I haven't got to the stage where I need to test the Android application itself with functional tests.
Some findings:
The apk is "needed" since the wizard adds the project-under-test as a project dependency (see properties in the test-project). During testing the framework needs to upload that project to the device somehow, hence the need for an apk. Since there is no apk there will be a warning message. Removing this dependency silences the warning. Note, since no apk is uploaded no library is uploaded to the device either, giving NoClassDefFound exceptions later on in the tests.
In my case, similar to the original post, removing the project-under-test dependency from the test-project and adding all needed jars manually, both built and required, from the-project-under-test solved it. I didn't needed the extra "test application" dependent on the project-under-test some posts elsewere suggests. Maybe this is a convinient solution if one otherwise have a lot of jars to add manually.
Both keeping the project dependency and manually adding the required jars led to problems resolving classes yeilding in NoClassDefFound exceptions. I don't now why.
So I'm trying our Android Studio and testing a project that worked in eclipse. I got everything compiling and the application will launch just fine, but I can't get my unit tests up and working. I eventually got them compiling by adding my applications lib folder as a dependency, but I don't think my run configuration is right because whenever I run my tests I get this error
Installing <packagename>
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/<packagename>"
pkg: /data/local/tmp/<packagename>
Success
Running tests
Test running started
Test running failed: Unable to find instrumentation info for: ComponentInfo{<packagename>/android.test.InstrumentationTestRunner}
Empty test suite.
Edit: To all new arrivals, the state of Android Studio has changed a lot since I initially posted this question, but many helpful people have continued to post their particular solution for this error. I'd advise sorting by active and checking out the newest answers first.
If you have a testInstrumentationRunner defined in your build.gradle such as this:
android {
defaultConfig {
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
make sure that in the run configuration window you use the exact same "Specific instrumentation runner" in your Android Studio / IntelliJ run configuration for your test.
In my case, the solution was:
View > Tool Windows > Build Variants
Select a *Debug variant
Explanation and solution
This error "Unable to find instrumentation" appears if targetPackage declared in the manifest of the test application is different from the package declared in the manifest of the application being tested:
Application being tested:
<manifest package="com.example.appbeingtested" … >
Test application :
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.appbeingtested" … />
I solved this problem by changing
android.test.InstrumentationTestRunner
into
com.android.test.runner.MultiDexTestRunner
in EditConfigurations -> Specific Instrumentation Runner (optional) tab.
Turns out, because my app is using MultiDex, I need to change test runner to MultiDexTestRunner as well.
UPDATE:
As #dan comment, InstrumentationTestRunner is deprecated use AndroidJUnitRunner instead.
In my case the Run/Debug Configurations were wrong.
One Solution:
Go to Run/Debug Configurations
Run -> Edit Configurations...
Setup a Android-Test for your test class
Select your Android test configuration on the left side or create a new one with the plus icon and name it e.g. ClassNameTest
Select the module containing your test class. In the simplest case the test class is in your app module so select app.
Select on the next row your test configuration. I use:
Class: to run all tests of one class.
Choice your test class
Finally configure your target device and select ok.
This only works in AndroidStudio Version < 2.3
In my case the wrong instrumentation runner was selected.
I fixed this by specifying the instrumentation runner in the Run/Debug Configuration of the test (see below). There you can select a runner from the list.
You find the Run/Debug Configurations: Run -> Edit Configurations ...
It seams you have not good project structure.
Open AndroidManifest.xml and check does it have
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.YourClass"
android:label="Tests for com.example.YourClass"/>
If NO do next:
Reorganize your directory structure to the following one: (this is the recomendation from official source)
MyProject/
AndroidManifest.xml
res/
... (resources for main application)
src/
... (source code for main application) ...
tests/
AndroidManifest.xml
res/
... (resources for tests)
src/
... (source code for tests)
You see that you need to have inner tests module. For creating it in Idea IDE do next File -> New Module -> Test Module. After creating you can see one new AndroidManifest.xml. And it has instrumentation declaration inside.
I've got same problem as #Iuliia Ashomok and tried everything on the internet.
Still no luck.
After 2 days investigation, I found that the problem is created by the mobile phone. .V.
I originally use Xiaomi Mi4i as testing device(rooted) and tests could not be run. Of course, I got the error below.
Test running failed: Unable to find instrumentation info for: ComponentInfo{<packagename>/android.test.InstrumentationTestRunner}
However, when I use Sony Xperia Z3(No root), everything works well.
I once got this error. I use the sdk tools in CLI mode. And the error happened when i launch 'ant test' in the test project. I later notice that I didn't even build and install the test project before ! (with 'ant debug install')
So you should have try to check your running configuration to see if the test project got effectively build before running the test.
For the other case, Android Studio use Gradle. I don't know it well but try to check the Gradle settings for the project or the Gradle settings file.
For me the problem was having this dependency:
debugCompile 'com.android.support.test:rules:0.2'
After I removed it my tests were found and run again.
Note that I didn't get the "Unable to find instrumentation" message but a "No tests found" message.
It turned out to be a delay problem. It fixed automatically after I waited a while reading the Internet solutions. I recompiled the code, and it worked.
Since none of these answers helped me, I wanted to share my solution for anyone who is as desperate as I was. :)
Because of the testing libraries that I was using, I needed to enable multidex support by adding multiDexEnabled true to my Gradle build. I'm not sure I had multidex support fully implemented to begin with (the proper way of doing it has changed since I last implemented it) but ultimately, I didn't end up needing it and removing that line from my build fixed the error. My team at work has had a few testing issues related to enabling multidex support… in typical Android style.
In my case in same classes some test cases were picking correct test runner ( android.support.test.runner.AndroidJUnitRunner ) which was defined in build.gradle, and some test cases were picking up android.test.InstrumentationTestRunner, which was not defined at least in Manifest or build.gradle or edit configuration. Ideally it should have been resolved by Sync Project with Gralde option, though it didn't work.
At last I found wrong test runner defined against a method in .idea/workspace.xml, I changed it manually, and issue got resolved.
Generally we are not supposed to edit this Android Studio generated file, but for me it did worked as last option.
In my case, the issue was in device too. Other devices were working fine. Simple uninstall and reinstall fixed it.
This answer is going to explain the history issues and summarise all related settings.
Basically there are 3 possible places for instrumentation test runner configurations.
In EditConfigurations -> General tab -> Specific Instrumentation Runner (optional)
This is existing in Android Studio 2.2 and version before only, in latest version, it is removed already.
In manifest file, the test runner is configured as below.
< instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
android:targetPackage="com.mytestapp.test"/>
Since "android.test.InstrumentationTestRunner" is deprecated in API level 24, this setting is also not necessary already, as long as you configure the runner in gradle file.
If you want to keep this setting, please make sure the runner name should match to the one you set in the gradle file, otherwise you will got this error also.
In gradle file.
This is the only recommended way if you use Android Studio v2.3 above.
android {
.......
defaultConfig {
.......
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
I am banging my head against the wall here trying to figure out why IntelliJ/Android is reporting "Empty test suite". I have a small project with two IntelliJ Modules ("Projects" in Eclipse). The Unit test module has its own AndroidManifest.xml, which I have pasted at the bottom. I am trying to run an ActivityUnitTestCase, since the tests will be dependent upon the Context-object.
The package name of the main module is nilzor.myapp. The pacakge name of the test module is nilzor.myapp.tests
Why is not the test runner detecting the testBlah()-method as a test?
<?xml version="1.0" encoding="utf-8"?>
<!-- package name must be unique so suffix with "tests" so package loader doesn't ignore us -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nilzor.myapp.tests"
android:versionCode="1"
android:versionName="1.0">
<!-- We add an application tag here just so that we can indicate that
this package needs to link against the android.test library,
which is needed when building test cases. -->
<application>
<uses-library android:name="android.test.runner"/>
</application>
<!--
This declares that this application uses the instrumentation test runner targeting
the package of nilzor.myapp. To run the tests use the command:
"adb shell am instrument -w nilzor.myapp.tests/android.test.InstrumentationTestRunner"
-->
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="nilzor.myapp"
android:label="Tests for nilzor.myapp"/>
</manifest>
And here is my test class:;
package nilzor.myapp.tests;
public class NilzorSomeTest<T extends Activity> extends ActivityUnitTestCase<T>{
public NilzorSomeTest(Class<T> activityClass){
super(activityClass);
}
#SmallTest
public void testBlah(){
assertEquals(1,1);
}
}
I have read the testing fundamentals, the activity testing document, and tried following this Hello world test blog, even though it is for Eclipse. I cannot get the test runner to find and run my test. What am I doing wrong?
Some of the questions I still feel unsure about are:
Do I need an Annotation above the Unit test method?
Do I need to prefix the method with "test", or is that just for JUnit tests?
Can I have tests in sub-packages of nilzor.myapp.tests?
But the main question of this post is why does not the test runner detect my test?
You need to provide default constructor for your test class, for example:
package nilzor.myapp.tests;
public class NilzorSomeTest extends ActivityUnitTestCase<ActivityYouWantToTest>{
public NilzorSomeTest(){
super(ActivityYouWantToTest.class);
}
#SmallTest
public void testBlah(){
assertEquals(1,1);
}
}
about your other questions:
No. My tests still run without any annotations, but I guess it's a good practice to have them. It allows you to specify size of tests to run. See What is the purpose of #SmallTest, #MediumTest, and #LargeTest annotations in Android? for more detail.
Yes, you need "test" prefix. InteliJ gives "method never used" warning when there's no "test" prefix, and skips that method during test run.
Yes. I have my tests organized into subpackages and it seems to be working well.
If this is happening "all of a sudden" or "it was working 5 minutes ago" my solution was to go into Run/Debug configurations and remove any configurations under "Android Tests". Sometimes these configurations get corrupted if I refactor the class under test (for example by moving to an new package).
None of the above fixed it for me. What helped was following the instructions:
Create a test configuration
In Android Studio:
Open Run menu -> Edit
Configurations Add a new Android Tests
configuration Choose a module Add a specific
instrumentation runner:
android.support.test.runner.AndroidJUnitRunner
Run the newly created configuration.
I had a similar issue. Not sure why this is occurring but I was able to fix it by going to: "File" > "Invalidate Caches/Restart" in Android Studio.
I don't know if it helps for Android Studio, but I had some kind of Intellij-Gradle conflict.
Solved it by "right-clicking" on the test-file and hit "compile file ...Test.java". After that I could run single tests again.
I had the same issue on Android Studio 2.3.1, turns out it was just a bug with AS. Running the same test on version 2.2.1 performs fine.
If you're only running Android Studio on the Cannary channel, I recommend you also install a stable version as well. http://tools.android.com/tips/using-multiple-android-studio-versions
I had tests that were running fine until gradle and android studio got upgraded.
Apart from adding a default constructor to your tests, you might need to do some of these things to get your test suite to work
Under src/ create androidTest/java/<your-package-name>/test . Note the androidTest. Anything else including instrumentTest will not work.
Add this to build.gradle
sourceSets {
testLocal {
java.srcDir file('src/androidTest/java')
resources.srcDir file('src/androidTest/resources')
}
}
android{
sourceSets {
instrumentTest.setRoot('src/androidTest/')
}
}
dependencies{
testLocalCompile 'junit:junit:4.11'
}
task localTest(type: Test, dependsOn: assemble) {
testClassesDir = sourceSets.testLocal.output.classesDir
android.sourceSets.main.java.srcDirs.each { dir ->
def buildDir = dir.getAbsolutePath().split('/')
buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')
sourceSets.testLocal.compileClasspath += files(buildDir)
sourceSets.testLocal.runtimeClasspath += files(buildDir)
}
classpath = sourceSets.testLocal.runtimeClasspath
}
check.dependsOn localTest
Add this to the AndroidManifest.xml
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:label="Tests for my packaged app"
android:targetPackage="<my-package-name>.test" />
For Intellij 15 I resolved this issue by:
Opening the 'Project Structure' settings
Clicking 'Modules' (on left)
'Sources' Tab
a. Right click on your source directory (usually src) click 'Source'.
b. Right click on your test directory click 'Test'
c. Right click on your out directory click 'Excluded'
Go to 'Paths' tab
a. Click 'Use module compile output path' radio button
b. Select your output path directory for 'Output Path'
c. Select
your test path directory for 'Test output Path'
Click Ok
Obviously, you need a target device as to run your tests as they are instrumented tests. For some reasons, Android studio sometimes does not ask you to point to this target device and just prompt the "Empty Test Suite" message.
There are different ways to fix this, here are a few :
run your main app and select a target device or
go to the Run (Run/Run.../Edit Configurations) configuration and modify the Deployement Target Options
I had this problem because I had this in my build.gradle:
testOptions {
execution "ANDROID_TEST_ORCHESTRATOR"
}
Even though I wasn't using the Android Test Orchestrator (must have copyied from the tutorials by mistake).
Commenting that out solved it for me.
In my case, none of the previous answers worked. The solution was to simply move the test class to another package.
This happened under androidTest/
In my case that problem was caused due to mistake in my code, actually that was in application class, so target activity wasn't opened and test output prints
Empty test suite error
I have tried run tests directly from terminal with adb shell am instrument -w -r -e package your.package -e debug false android.support.test.runner.AndroidJUnitRunner. With this it prints for you much more about exception.
None of the other solutions worked for me, but I was able to get this working simply by uninstalling the existing app or test suite, then running the tests.
In my case, the project I was working on had a couple of modules. None of the solutions I found for this error helped me, and then somehow I realized that if I added the testing dependencies in BOTH of the build.gradle files, the tests magically started working. It doesn't matter if your tests live in only 1 of the modules, both gradle files must include the dependencies and the testInstrumentationRunner value.
So, if like me, none of the other answers have helped you, try adding these lines to the build.gradle file of each of your modules:
android {
....
defaultConfig {
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
and then also add:
dependencies {
...
// Test
androidTestCompile 'com.android.support:support-annotations:23.4.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
}
I just renamed the file and the problem fixed.
I had the same issue, and the reason was my test class did not have Test at the end of the class name!
My issue was caused by an exception being thrown in the #BeforeClass method of my test case. It some how wasn't causing the test to fail - I only found it by inspecting the logcat output.
I fixed the exception and suddenly my tests were running!
After facing the problem today - not being able to run the instrumented android tests with Empty suite error - I found a git issue about this problem and thanks to Stephan Linzner, I could run the tests.
tl;dr You have to right click the test package and not the class in order to make the tests run.
Reference: https://github.com/googlecodelabs/android-testing/issues/27#issuecomment-219074863
This article helped me: Empty test suite
Basically I had to create a package - instrumentTest/java - under my src directory, and put all the tests there. Then I could execute these tests individually.
I had a raw Java project where this was occurring. Simply Java + JUnit4. It definitely resides with something in your .idea/ or .iml files. I scrapped mine, re-imported, and finally the tests ran again.
The test class may excluded from the compilation. Fix it in setting-compiler-exclude.
Here are my debugging steps I go through when Android Studio all of a sudden decides to stop running / debugging tests (And boy does this happen embarassingly often!!):
Build: → Rebuild project
Restart Device: Restart your device/emulator and try again
Device switch: if you have both a regular phone and an emulator unplug one and try running it with just one of the devices
Android Studio: File--> Invalidate caches and restart
Activity Monitor / Task Manager: sort processes by name, see if there is a nameless processes that's using up a lot of ram, this is a "ghost" process from Android studio that must be killed
git revert: try stashing /reverting your latest code. Sometimes there is a compile error that Android Studio / gradle misses and it will just try to run uncompilable code.
Uninstall then reinstall Android Studio.
I will add more fixes as I run into them!
I did nothing and the problem went away after half a day of pain, I opened and closed the projects many times, ran each class tests manually, maybe that fixed my it.
In Android studio with spock framework I've changed my gradle's version from 2.2.2 to 3.2.1 and all goes well.
The accepted answer didn't solve my problem. So I decided to copy ExampleInstrumentedTest which is created by default in Android Studio and runs without any problems, renamed it during the copy process (no Refactor->Rename after copying!) and pasted the contents of my unit test into it. After that the error disappeared.
I experienced the "Empty test suite" error when trying to run local unit tests in my Android Studio 3.0 project.
After reading the Android Developer documentation, I quickly realised that the issue was caused by my gradle config which included the following lines.
testImplementation 'com.android.support.test:runner:0.5'
testImplementation 'com.android.support.test:rules:0.5'
The AndroidJUnitRunner class is a JUnit test runner that lets you run JUnit 3- or JUnit 4-style test classes on Android devices.
Since my tests were local and therefore not required to run on any device, removing the above com.android.support.test... entries enabled me to execute the unit tests.
I was doing some insertions in a db in the #BeforeClass method.
I realised I had an object/database mapping problem. This data mapping problem was the cause of this issue for me.
In my case, I had my instrumented tests in androidTest/java/<package.name>/MyTestingClass, but I had set my current build variant to "preproduction". And there's the point! As specified in Android Studio documentation:
By default, all tests run against the debug build type.
The message Class not found. Empty test suite. kept appearing until I did this:
Add this line to my build.gradle:
android{
[...]
testBuildType "preproduction"
}
Synchronised gradle.
Delete my previous test configurations since they don't take this Gradle synchronisation into account.
Then I executed the tests again and this time they run just perfect!!!
I had this happen to me when I mistakenly marked a non mock class variable with the annotation #Mock
Removed the annotation and the tests ran successfully.
This happened with Junit 4.5 on Android Studio
Not a solution but a workaround that will get you back on track quickly:
Firstly, find a test that works. I was writing a new test where I got the 'empty test suite' error. I ran other tests and they were working as usual.
Copy the test file that does work. Run it to make sure this copy works like the original.
Remove the body and replace it with your new test code.
The test should now work.
We spent about two hours trying to find the cause but to no avail.