Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AndroidJUnit4 and Parameterized tests

Tags:

Google provide new classes to write tests for Android, and especially using jUnit 4: https://developer.android.com/tools/testing-support-library/index.html

I was wondering if it is possible to use the AndroidJUnit4 runner, as well as the Parameterized one, from jUnit?

like image 951
Gaëtan Avatar asked Apr 09 '15 12:04

Gaëtan


People also ask

What is a parameterized test?

Test parameterization is a type of data-driven testing that allows you to execute the same test, multiple times using different parameters. Xray Cloud has a parameterized tests feature, that executes the same test with different input values, multiple times, without ever having to clone or replicate it.

What is androidjunit4?

The AndroidJUnitRunner class is a JUnit test runner that lets you run instrumented JUnit 4 tests on Android devices, including those using the Espresso, UI Automator, and Compose testing frameworks. Note: AndroidJUnitRunner is not needed for local tests.

Does JUnit support parameterized tests?

JUnit 4 has introduced a new feature called parameterized tests. Parameterized tests allow a developer to run the same test over and over again using different values. There are five steps that you need to follow to create a parameterized test. Annotate test class with @RunWith(Parameterized.

How does JUnit parameterized test work?

Parameterized test is to execute the same test over and over again using different values. It helps developer to save time in executing same test which differs only in their inputs and expected results. Using Parameterized test, one can set up a test method that retrieves data from some data source.


1 Answers

The current accepted answer doesn't provide an explanation, and the linked example isn't great at showing what needs to be done. Here's a more complete explanation that will hopefully save someone from spending the time I did figuring this out.


While the documentation doesn't make this super obvious, it's actually surprisingly easy to set up! You are able to use another runner with instrumented Android tests as long as you set testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" in your module build.gradle file. If that is set, you do not need to explicitly set @RunWith(AndroidJUnit4.class)in your instrumented tests.

A minimal example would look like this:

build.gradle:

apply plugin: 'com.android.application'  android {     compileSdkVersion 26     buildToolsVersion "26.0.1"      defaultConfig {         minSdkVersion 19         targetSdkVersion 26          testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"     } } 

SampleParameterizedTest.java:

@RunWith(Parameterized.class) public class SampleParameterizedTest {      @Parameter(value = 0)     public int mTestInteger;      @Parameter(value = 1)     public String mTestString;      @Parameters     public static Collection<Object[]> initParameters() {         return Arrays.asList(new Object[][] { { 0, "0" }, { 1, "1" } });     }      @Test     public void sample_parseValue() {         assertEquals(Integer.parseInt(mTestString), mTestInteger);     } } 

If you also have the need to run some tests individually and others parameterized in the same test class, see this answer on using the Enclosed runner: https://stackoverflow.com/a/35057629/1428743

like image 158
PseudoPsyche Avatar answered Sep 21 '22 13:09

PseudoPsyche