Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio running unit test shows 'Empty test suite'

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:

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.

like image 843
dragi Avatar asked Jun 24 '14 14:06

dragi


People also ask

How long should a test suite take to run?

Still, it seems as though a 10 second short-term attention span is more or less hard-wired into the human brain. Thus, a unit test suite used for TDD should run in less than 10 seconds. If it's slower, you'll be less productive because you'll constantly lose focus.

How run all test cases in Android?

Run all tests in a single classRight click on a test class name in the editor (or a filename in the project explorer) to run all tests in a single class. The default keyboard shortcut for this is Ctrl+Shift+F10 on Linux. I suggest you map it to something easier to remember like Ctrl+Alt+R.

Can I delete the test folder in Android Studio?

Yes You can delete them ,you can go to build. gradle and then remove following dependencies which comes preloaded in any new android project. and then click on the androidTest and Test package and Press Delete they will be removed !

What is Android unit runner?

The AndroidJUnitRunner class is a JUnit test runner that lets you run instrumented JUnit 4 tests on Android devices, including those using the Espresso, UI Automator, and Compose testing frameworks. Note: AndroidJUnitRunner is not needed for local tests.


3 Answers

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

like image 187
dragi Avatar answered Oct 20 '22 13:10

dragi


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).

like image 44
AlbusMPiroglu Avatar answered Oct 20 '22 13:10

AlbusMPiroglu


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.

like image 25
SBerg413 Avatar answered Oct 20 '22 12:10

SBerg413