Ignoring Android unit tests depending on SDK level

android unit testing
android instrumentation test example
android integration testing
unit testing android studio
types of testing in android
android run instrumentation tests command line
android instrumentation test coverage
unit test android library

Is there an annotation or some other convenient way to ignore junit tests for specific Android SDK versions? Is there something similar to the Lint annotation TargetApi(x)? Or do I manually have to check whether to run the test using the Build.VERSION?

I don't think there is something ready but it pretty easy to create a custom annotation for this.

Create your custom annotation

@Target( ElementType.METHOD )
@Retention( RetentionPolicy.RUNTIME)
public @interface TargetApi {
    int value();
}

Ovverride the test runner (that will check the value and eventually ignore/fire the test)

public class ConditionalTestRunner extends BlockJUnit4ClassRunner {
    public ConditionalTestRunner(Class klass) throws InitializationError {
        super(klass);
    }

    @Override
    public void runChild(FrameworkMethod method, RunNotifier notifier) {
        TargetApi condition = method.getAnnotation(TargetApi.class);
        if(condition.value() > 10) {
            notifier.fireTestIgnored(describeChild(method));
        } else {
            super.runChild(method, notifier);
        }
    }
}

and mark your tests

@RunWith(ConditionalTestRunner.class)
public class TestClass {

    @Test
    @TargetApi(6)
    public void testMethodThatRunsConditionally() {
        System.out.print("Test me!");
    }
}

Just tested, it works for me. :)

Credits to: Conditionally ignoring JUnit tests

Ignoring Android unit tests depending on SDK level, Is there an annotation or some other convenient way to ignore junit tests for specific Android SDK versions? Is there something similar to the Lint annotation  If you run a test that calls an API from the Android SDK that you do not mock, you'll receive an error that says this method is not mocked. That's because the android.jar file used to run unit tests does not contain any actual code (those APIs are provided only by the Android system image on a device). Instead, all methods throw exceptions by default.

I've been searching for an answer to this question, and haven't found a better way than to check the version. I was able to conditionally suppress the execution of test logic by putting a check in the following Android TestCase methods. However, this doesn't actually prevent the individual tests from executing. Overriding the runTest() method like this will cause tests to "pass" on API levels you know will not work. Depending on your test logic, you may want to override tearDown() too. Maybe someone will offer a better solution.

@Override
protected void setUp() throws Exception {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
        if (Log.isLoggable(TAG, Log.INFO)) {
            Log.i(TAG, "This feature is only supported on Android 2.3 and above");
        }
    } else {
        super.setUp();
    }
}

@Override
protected void runTest() throws Throwable {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
        assertTrue(true);
    } else {
        super.runTest();
    }
}

Test your app, With just a few clicks, you can set up a JUnit test that runs on the local JVM or an such as Mockito to test Android API calls in your local unit tests, and Espresso or UI app source sets, which allows you to create tests based on build variants. test source set or configuring your module-level build.gradle file, as shown in  Instrumented tests: Unit tests that run on an Android device or emulator. These tests have access to instrumentation information, such as the Context for the app under test. Use this approach to run unit tests that have complex Android dependencies that require a more robust environment, such as Robolectric.

An alternative is to use JUnit's assume functionality:

@Test
fun shouldWorkOnNewerDevices() {
   assumeTrue(
       "Can only run on API Level 23 or newer because of reasons",
       Build.VERSION.SDK_INT >= 23
   )
}

If applied, this effectively marks the test method as skipped.

This is not so nice like the annotation solution, but you also don't need a custom JUnit test runner.

Support different platform versions, Overview · Building local unit tests · Building instrumented unit tests Specify minimum and target API levels; Check system version at runtime version of Android, based on the number of devices that visit the Google Play Store. older versions of Android simply ignore the showAsAction attribute (that is,  Instrumented unit tests are tests that run on physical devices and emulators, and they can take advantage of the Android framework APIs and supporting APIs, such as AndroidX Test. Instrumented tests provide more fidelity than local unit tests, but they run much more slowly.

Android Testing Tutorial: Unit Testing and More, Tests can provide the extra level of confidence about things we build. Nonetheless, testing is important, and shouldn't be taken for granted or ignored. Robolectric is an Android unit testing framework that allows you to run tests inside the Robolectric rewrites Android SDK classes as they're being loaded and makes it  Unit tests run on a local JVM on your development machine. Our gradle plugin will compile source code found in src/test/java and execute it using the usual Gradle testing mechanisms. At runtime, tests will be executed against a modified version of android.jar where all final modifiers have been stripped off. This lets you use popular mocking

Android 3 SDK Programming For Dummies, Part IV: Enhancing the Capabilities of Your Android Application Part IV is all In particular, we describe the unit testing and performance optimization capabilities that Eclipse on Android do to push your Android 3 SDK programming experience to the next level. Warning icons alert you to potential pitfalls, so don​'t ignore. Stack Overflow Public questions and answers; Markus K. Apparently, this user prefers to keep an air of mystery about them. 6 Ignoring Android unit tests

Android Cookbook: Problems and Solutions for Android Developers, Around 2013 it announced the switch to Android Studio based on IntelliJ. Eclipse supports a single-level project, which is typically what you need for an second project for testing if you use the official Android unit testing framework (​see can be controlled as well, but it changes a lot and can just as easily be ignored. Instrumented unit tests are unit tests that run on Android devices and emulators instead of running on the Java virtual machine. These tests have access to the real device and its resources and are useful to unit test functionality which cannot be easily mocked by mocking frameworks. An example is a test which validates a Parcelable implementation.

Comments
  • Looks like a beautiful solution, but how do you get JUnit 4 to work with Android?
  • I don't think there is any problem without any Junit version, you have eventually change the test runner I guess. Which is the version that you're using?
  • I think it's JUnit 3.8. Android testing tools do not support JUnit 4: developer.android.com/tools/testing/testing_android.html
  • Now it's supported code.google.com/p/android-test-kit/wiki/… you can use it from Support Repository.
  • This worked for me when I tried it to solve a similar problem. However there's now the @SdkSuppress annotation which works too and it's clean and simple to use. See the answer to my Q stackoverflow.com/questions/45865658/…