Android Studio running unit test shows 'Empty test suite'
Solution 1
The constructor should look like this:
public MyActivityTest()
{
super(MyActivity.class);
}
I will need to learn not to rely so much on the IDE's code template that provided constructor with parameter. This was resolved thanks to a colleague and http://siemprepreguntando.blogspot.de/2013/07/running-tests-test-running-startedtest.html
Solution 2
I also ran into "empty test suite" problem recently. After checking a few similar questions and answers, and my problem, I can possibly conclude that the problem results from an error preventing the tests being added to the test suite, such as an error in static initialization.
For example I'm using a popular approach to adding all tests as shown below, but it's the same scenario with different approaches to adding test cases to the suite:
public class FullTestSuite extends TestSuite {
public static Test suite() {
return new TestSuiteBuilder(FullTestSuite.class)
.includeAllPackagesUnderHere().build();
}
public FullTestSuite() {
super();
}
}
And apparently my test file had a problem in a static {} block which prevented .includeAllPackagesUnderHere() to run successfully.
So I would suggest anyone facing this error to first check your app logs to see if your test runs into a problem that prevents test cases being added to the test suite (like similar examples of wrong constructor being called or static initialization problems).
Solution 3
In my case, the "empty test suite" message was directly related to the target API Level of the Android Emulator I was running. I had set up an emulator with API level 19 and was using that while trying to run my instrumentation tests. I also had just recently migrated my codebase to use the JUnit4 framework along with the AndroidJUnitRunner instrumentation runner.
I was banging my head against the wall for a while before I started to look into issues with the actual emulator. Sure enough, as soon as I set up an emulator with API Level 23, the tests started to run fine.
Further experimentation found that my tests ran fine on API Level 22 & 23 emulators, but not on anything below that. I suspect it has something to do with my test dependencies and minimum API level requirements.
I'll update this answer if I discover more.
![dragi](https://i.stack.imgur.com/Xp0Zv.jpg?s=256&g=1)
dragi
I love programming and have been dealing with it since I was 12. Professionally I have spent more than 5 years developing embedded systems in C, then another 5 years developing Android applications, and since late 2018 I am focused on Java backend development.
Updated on July 25, 2022Comments
-
dragi almost 2 years
I want to start writing unit tests for my applications but I cannot get one simple test to run. I have created a small application just to try how the unit test should be setup and run, but no test is actually run and I get 'Empty test suite'.
I am using Android Studio 0.6.1 with gradle 1.12
Here is my folder structure:
MyActivityTest.java
package com.vist.testableapp.tests; import android.content.Intent; import android.test.ActivityUnitTestCase; import android.test.suitebuilder.annotation.SmallTest; import android.widget.Button; import com.vist.testableapp.MyActivity; import com.vist.testableapp.R; public class MyActivityTest extends ActivityUnitTestCase<MyActivity> { public MyActivityTest(Class<MyActivity> activityClass) { super(activityClass); } Button btn1; @Override public void setUp() throws Exception { super.setUp(); startActivity(new Intent(getInstrumentation().getTargetContext(), MyActivity.class), null, null); btn1 = (Button)getActivity().findViewById(R.id.button1); } @SmallTest public void testFirst() { assertEquals("Btn1",btn1.getText()); } }
application's build.gradle
apply plugin: 'android' android { compileSdkVersion 19 buildToolsVersion "19.1.0" defaultConfig { applicationId "com.vist.testableapp" minSdkVersion 15 targetSdkVersion 15 versionCode 1 versionName "1.0" testApplicationId "com.vist.testableapp.tests" } buildTypes { release { runProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) }
Could anyone point out what am I doing wrong or what I am missing? I searched in SO but none of the answers helped me.