0

When creating a new project in Android Studio, I notice that it automatically creates an /androidTest directory under /src, where there is "ApplicationTest.java" class with the following code:

public class ApplicationTest extends ApplicationTestCase<Application> {
    public ApplicationTest() {
        super(Application.class);
    }
}

I'm guessing this is what Google wants us to use, but after searching for hours, I couldn't figure out how to use this class that was generated for me. Google's official doc seems to only list how to run on Eclipse IDE (not Android Studio), and I couldn't find any code that would let me perform a simple test (say like assertEquals(1,2)). Can someone show me how to write a simple test code using the above default template, and steps on how to run it, preferably from the command line?

EDIT: I was able to write a simple test that is intended to fail. ApplicationTest.java in /androidTest/java/path/to/package/

public class ApplicationTest extends ApplicationTestCase<Application> {
    public ApplicationTest() {
        super(Application.class);
    }

    @Override
    protected void setUp() throws Exception {
        createApplication();
    }

    @SmallTest
    public void testMultiply() {
        assertEquals("This should not pass ", 50, 49);
    }
}

I am able to run this from Android Studio, but I just cannot figure out how to run that from the command line. Any help?

baekacaek
  • 1,527
  • 3
  • 22
  • 45
  • There is no test. That is just an example setup. – Jared Burrows Aug 12 '15 at 02:19
  • I understand that it's a boilerplate for a test setup. My question was more about how to write test using the above setup; for instance, what do the functions need to be called? and how do I run the test after I write them? – baekacaek Aug 12 '15 at 17:36

1 Answers1

0

As Jared already said, this is an example setup for an instrumentation test. I guess, you've already taken a look on this: Testing Fundamentals

Simpliest way to run these tests in android studio is to right click the class and click run. It is also possible to add tests in your run/debug configurations.

UPDATE:

To run the instrumentation tests on command line, use ./gradlew assembleAndroidTest. This command will run all tests in your src/androidTest (instrumentation test) folder.

As njzk2 mentioned, there also is a ./gradlew assembleTest command. This command is for running all unit tests (which should be placed in the src/test folder). For more information about unit testing in android take a look on this: Android Unit Testing Support

EXAMPLE:

Here an example for an instrumentaion test in android:

@Override
public void setUp() throws Exception {
  super.setUp();
  InputStream is = getContext().getAssets().open("test.xml");
  XmlParser parser = new XmlParser(getContext());
  parser.parse(is);
  ...
}

@MediumTest
public void testSomething() throws Exception {      
  // test some data your parser extracted from the xml file
  ...
}

As you can see, i need the context for creating the input stream, therefor i have to go for an instrumentation test. The @MediumTest signals e.g. i'm accessing files from storage (see Test Annotations).

Community
  • 1
  • 1
Rich
  • 1,015
  • 1
  • 10
  • 22
  • 1
    `./gradlew tasks` lists the tasks. `assembleTest` assembles the test application. then you have to run it on a device. – njzk2 Aug 12 '15 at 17:57