/android-testng

TestNG instrumentation wrapper for Android

Primary LanguageJavaApache License 2.0Apache-2.0

TestNG runner for Android

This is a minimal implementation of an Android Instrumentation executing unit tests based on TestNG (the best testing framework for Java).

Download

Usage

Depending on your build system, your mileage might vary, but with Gradle the only required changes to your build files should be limited to adding our repository, then declaring the dependency and modifying your testInstrumentationRunner:

// Our Bintray repository
repositories {
  maven {
    url 'http://dl.bintray.com/lemonade/maven'
  }
}

// TestNG dependency, remember to update to the latest version
dependencies {
  androidTestCompile 'de.lemona.android:android-testng:X.Y.Z'
}

// Android setup
android {
  defaultConfig {
    testInstrumentationRunner 'de.lemona.android.testng.TestNGRunner'
  }
}

Packages

The runner will ONLY look for classes in the package specified by the targetPackage entry in your AndroidManifest.xml file.

In Gradle this defaults to your application package plus ....test.

If no tests can be found, verify the parameter in the manifest of your APK.

For example in our manifest the declared package is de.lemona.android.testng, henceforth after the build processes it, all our tests will be automatically searched for in the de.lemona.android.testng.test package.

XML Suites

Test suites can also be defined using a testng.xml file from your assets directory.

This is useful when tests do not reside in the standard application package plus ....test.

One caveat, though, is that the <package /> element does not work (yet), as TestNG expects JAR files, while Android bundles everything into a DAX file.

For an example see the testng.xml file included alongside these sources.

Contexts

In order to have access to the Android's application Context please refer to the AndroidTestNGSupport utility class. The two static getContext() and getInstrumentation() methods allow retrieval of the instances.

Google Guice injection is also supported. Take a look at the GuiceInjectionTest for an example of how to configure your tests.

Options

The options to enable some features on testing are same as adb instrument. Current supported options are as below:

  • debug
  • coverage
  • coverageFile

If you need to run tests from Android Studio, please use Android Tests Configuration. Running the following Gradle tasks from Android Studio will typically work with these tests, where YourBuildName is your build name in camel-case:

  • :build
  • :installYourBuildNameAndroidTest
  • :connectedYourBuildNameAndroidTest

Compatability with Android UI testing frameworks

This framework has been tested to be fully compatable with the UIAutomator framework; just make sure to import the AndroidTestNGSupport class' getInstrumentation() and getContext() methods. It's also compatable with the Espresso framework. Make sure to set up your Espresso tests with methods such as the following (this is just the code I got to work; it may be suboptimal!):

@Inject
public YourTestNameHere(Instrumentation instrumentation) {
    this.mInstrumentation = instrumentation;
}

@BeforeTest
public void setUp() {
    InstrumentationRegistry.registerInstance(mInstrumentation,
            new Bundle());
    Intent intent = new Intent(getContext(), YourAppActivityNameHere.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    mActivity = YourAppActivityNameHere.class.cast(mInstrumentation.startActivitySync(intent));
    mInstrumentation.waitForIdleSync();
}

License

Licensed under the Apache License version 2