2014-08-18

Level Up - Android: How to get started with Unit Testing in Android Studio

Recently, I finally got around to implementing the native unit testing that is provided within Android Studio (not third-party programs like Robolectric). So, after reading a few different sources, the following is the bare minimum that I would have needed in order to add the basic testing features.



Following the quickstart, there will be a section just for more notes about the process in the walkthrough. It may answer some questions if you run into any trouble, which hopefully you won't. ;)

Quickstart guide to test
1. Create a new Android project in Android Studio.
2. Add a `public static` method to /app/src/main/java/com.your.package.name/. For example, I added the following to a new class called "MyClass".

    public static int add(int a, int b) {
        return a + b;
    }

3. There should already be a directory auto-generated for /app/src/androidTest/java/com.your.package.name/, if not, then create it. In that directory, create a new Java class called "MainTest" and add the following code to it.

package com.your.package.name;

import android.test.InstrumentationTestCase;

/**
 * Sample use of native Android unit testing.
 */
public class MainTest extends InstrumentationTestCase {

    public void testAdd_positive() {
        int expected = 42;
        int reality = MyClass.add(40, 2);
        assertEquals(expected, reality);
    }

    public void testAdd_negative() {
        int expected = -42;
        int reality = MyClass.add(-40, -2);
        assertEquals(expected, reality);
    }

    public void testAdd_overflow() {
        int expected = Integer.MIN_VALUE;
        int reality = MyClass.add(Integer.MAX_VALUE, 1);
        assertEquals(expected, reality);
    }

}

4. Right-click on `MainTest` in the Project view (Alt+1), then expand "Run" and choose the option with the Android icon. (This will set up the run configurations instead of doing in manually. The following more info section will explain how to set up using a more manual method.)
Choose the Android icon.
4.1. Sidenote: You'll have to connect the project to emulator or device, but for this simple test, the app won't display.
4.2. Sidenote: Once ran, the results will show as a popup window in Android Studio, with a green bar for all success and red bar for a failure. Play around with the asserts and see what happens. :)

Successfully tested 3 of 3 tests.



Extra notes on the above quickstart
1. When creating the new Android project, any way that you go through it should be able to support the native Android testing, as long as you have the latest version.
1.1. You can also add testing to an old project, but it may not have the directory already created automatically. In that case, just manually create it by right-clicking on the folders that you do see and select New->Directory and name them as shown for step 2.
2. --
3. The directory "androidTest" is a specific/key name that Android Studio for more advanced reasons mentioned at: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing
3.1. You may also put more test methods there, but each one of them needs to start with "test" in order for Android Studio to detect that it is a test method that should run. (In a future version, they may change this rule and use annotations (i.e. @Test) instead, like other popular unit testing frameworks.)
4. If you don't see the same options as in the screenshot in the quickstart step 4, then here's how to set the run configurations manually:
4.1. In Android Studio menu bar, click Run->Edit Configurations, which will bring up a window called "Run/Debug Configurations".
4.2. In the left section, if you see your MainTest class anywhere, delete it, so that we can add a new config for it.
4.3. At the top-left, click the green plus icon for "Add New Configuration" (Alt+insert), then select "Android Tests".
4.4.A. Provide the name "MainTest". For the Test option select "Class". For the Class input box put in .MainTest
4.4.B. (easier) Provide the name "test-all". For the Test option select "All in Module". That's it, and this run configuration will cover all future test classes, but you may want something more specific than this if you have a "significantly large" project.
4.5. Click Apply, then OK.
4.6. In the toolbar, towards the middle, from the run configs drop-down box, select the config that you just added, then click the green run (Shift+F10).


Further resources:
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing
- For more pictures, but still the same basic information: http://rexstjohn.com/unit-testing-with-android-studio/


~ Danial Goodwin ~



No comments: