Android Studio runs unit test shows 'Empty test suite'

I want to start writing unit tests for my applications, but I cannot run one simple test. I created a small application to try setting up and running unit test, but the test fails and I get an โ€œempty test packageโ€.

I am using Android Studio 0.6.1 with gradle 1.12

Here is my folder structure:

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 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']) } 

Can someone point out what I'm doing wrong or what I am missing? I searched in SO but none of the answers helped me.

+6
source share
3 answers

The constructor should look like this:

 public MyActivityTest() { super(MyActivity.class); } 

I need to learn not to rely so much on the IDE code template that provided the constructor with a parameter. This was solved thanks to a colleague and http://siemprepreguntando.blogspot.de/2013/07/running-tests-test-running-startedtest.html

+6
source

I recently ran into the "empty test suite" problem. After checking several similar questions and answers and my problem, I can conclude that the problem arises due to an error preventing the addition of tests to the test package, for example, to an error during static initialization.

For example, I use the popular approach to add all tests, as shown below, but this is the same scenario with different approaches for adding test cases to the package:

 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 the static block {}, which prevented the successful execution of .includeAllPackagesUnderHere ().

So, I suggest to everyone who encounters this error, first check the application logs to see if your test is running in a problem that prevents adding test cases to the test suite (for example, similar examples of incorrect constructor, called, or static initialization problems) .

+2
source

In my case, the message "empty test suite" was directly related to the target API level in the Android emulator that I was running. I created an emulator with API level 19 and used this while trying to run my control tests. I also recently migrated my code base to use the JUnit4 framework with the AndroidJUnitRunner software.

I banged my head on the wall for a few seconds before I began to study problems with a real emulator. Of course, as soon as I configured the emulator with API level 23, the tests started to work fine.

Further experimentation showed that my tests worked perfectly on emulators of API level 22 and 23, but not at all below. I suspect this has something to do with my test dependencies and minimum API level requirements.

I will update this answer if I find out more.

+1
source

Source: https://habr.com/ru/post/971293/


All Articles