0

I am using newest version of android studio gradle plugin 1.2.3. I am unable to understand how to create unit tests in it; all the available answers are for older version of android. Help me.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
supriya
  • 11
  • 1
  • 4

1 Answers1

1

first of all you need a different folder structure for your unit tests. android studio automatically generats the androidTest folder for instrumentation tests, but you can't put your unit tests in there. so you have to create a "test" folder:

- src
  -- androidTest //for instrumentation tests
  -- main        //source code
  -- test        //for unit tests

use the same package structure for your tests as for the class you want to test.

you can switch in your build variants of android studio between Android Instrumentation Tests and Unit Tests.

enter image description here

depending on your selection test or androidTest folder will be show in your project tab.

finally you have to add junit to your dependencies in gradle:

dependencies {
  testCompile 'junit:junit:4.12'
}

the test classes in your test folder can for example look like this:

package package.of.class.to.test

import org.junit.Before;
import org.junit.Test;
...
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class TestSomething{

  @Before
  public void setup(){
    // test setup
  }

  @Test
  public void testSomething(){
    // your unit tests like these simple examples
    assertThat(anyBooleanResult, is(expectedBooleanResult));
    assertEquals(anyIntResult, expectedIntResult);
  }
}

for further information you can also take a look on this thread.

Community
  • 1
  • 1
Rich
  • 1,015
  • 1
  • 10
  • 22
  • I am using Android Studio 1.2.2 and gradle 2.2.1 with plugin 1.2.3. As you said I had tried that before but while running it shows "Test running failed: Unable to find instrumentation info for: ComponentInfo{com.user.test.myapplication.test/android.test.InstrumentationTestRunner} and since its new version few of the steps mentioned in thread doesn't work like: On clicking a java class in AS it doesn't show "Go to" to create a test class like in Eclipse. Empty test suite." – supriya Jul 17 '15 at 06:58
  • what does your unit test look like? it seems you created an instrumentation test – Rich Jul 17 '15 at 06:59
  • i updated my answer and added an example for the test class – Rich Jul 17 '15 at 07:06
  • Ok so the TestSomething class doesn't extend to anything – supriya Jul 17 '15 at 07:12
  • no, there is no need to extend something for junit tests – Rich Jul 17 '15 at 07:14
  • Than I cant add assertions and also cant create "Android Test"-"Run/Debug configuration" to run it – supriya Jul 17 '15 at 07:21
  • that's because it is not an android test, but a unit test. android tests should be in your androidTest folder. to run the tests, just right click it and run as unit test (as described in the link i provided). for assertions use the standard junit assertions like assertThat, assertEquals, etc. – Rich Jul 17 '15 at 07:30
  • i added 2 examples for junit assertions (incl. imports) – Rich Jul 17 '15 at 07:40