4

So I started with a UnitTest called MyCoolObjectTest. Needing to do some instrumentation testing, I moved the class right within the Android project view from the test directory to the androidTest directory by dragging. My UnitTest used to work fine; except for the instrumentation portions. But now it does not work at all. I keep getting

Class not found: "com.mypkg.MyCoolObjectTest"Empty test suite.

I have been looking all over stackOverflow and at the official docs for a solution. No luck so far. Has anyone experienced something similar?

(if I drag it back to the test folder it works; if I re-drag to the androidTest folder it stops working again)

I remember the was a way to set the folder under test. But I no longer remember.

For some insight, I want to use some android library in my test such as

public String stripFormatting(String input){
    return Html.fromHtml(input).toString();
}

That's why I need instrumentation.

Here is my unit test class with one test for an example:

@RunWith(AndroidJUnit4.class)
public class MyCoolObjectTest {
    @Test
    public void testJustToKnow() {
        String actual = "<b>0</b>";
        String expected = "0";
        assertThat(stripFormatting(actual), equalTo(expected));
    }
    public String stripFormatting(String input) {
        return Html.fromHtml(input).toString();
    }
}

update

Here is a different trace that I got. If I click on the method instead of the class, I get the following trace:

Just now I clicked on the method instead of the whole class, and got the following trace:

11/04 13:51:16: Launching testJustToKnow() $ adb push /Users/me/StudioProjects/Myapp/app/build/outputs/apk/app-debug.apk /data/local/tmp/com.mypkg.myapp $ adb shell pm install -r "/data/local/tmp/com.mypkg.myapp" pkg: /data/local/tmp/com.mypkg.myapp Success

$ adb push /Users/me/StudioProjects/Myapp/app/build/outputs/apk/app-debug-androidTest-unaligned.apk /data/local/tmp/com.mypkg.Myapp.test $ adb shell pm install -r "/data/local/tmp/com.mypkg.myapp.test" pkg: /data/local/tmp/com.mypkg.myapp.test Success

Running tests

$ adb shell am instrument -w -r -e debug false -e class com.mypkg.myapp.utils.MyCoolObjectTest#testJustToKnow com.mypkg.myapp.test/android.test.InstrumentationTestRunner Client not ready yet..Test running started

junit.framework.AssertionFailedError: No tests found in com.mypkg.myapp.utils.MyCoolObjectTest at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1959)

Tests ran to completion.

1
  • Can you post the test class ? Commented Nov 4, 2016 at 19:08

2 Answers 2

8

I just had the same problem. Here is how I solved it:

  • In Android Studio go to Run > Edit Configurations...
  • Under the heading Android JUnit, select your test and delete it (minus button)
  • Add a new test configuration (plus button) and select Android Instrumented Tests
  • Set the following options:
  • Module: app
  • Test: Class
  • Class: select or enter your class with the instrumentation tests here
  • Click OK

Everything should work now.

Sign up to request clarification or add additional context in comments.

2 Comments

In my case there was "step 0" where I had to close and reopen the project. In step 2, the configuration was under "unknown".
@murf This one worked. Its weird why Studio is not doing all these automatically... Unfortunately awful with these kinda issues... Thanks murf for pointing this out
1

Maybe this will be useful:

  • src/test: only for unit test (nothing that involve android framework).

  • src/androidTest: for android instrumentation tests.

Please see here a better explanation for this.

Example of instrumentation test (Please check your annotations, for example @RunWith(AndroidJUnit4.class) or @SmallTest or @Test)

I hope this will be useful for you.

(update)

Maybe you forgot this on your gradle (in the main module):

android {
    defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

If you has using Espresso, you must add a dependency (Please check the link above this "...Example of instrumentation test...")

1 Comment

@RunWith(AndroidJUnit4.class) is not necessary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.