Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting UnitTest to Instrumentation Test in android studio

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.

like image 638
Nouvel Travay Avatar asked Jan 06 '23 02:01

Nouvel Travay


2 Answers

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.

like image 174
murf Avatar answered Jan 11 '23 02:01

murf


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

like image 38
Gaston Flores Avatar answered Jan 11 '23 01:01

Gaston Flores