Julian Suarez
Julian Suarez

Reputation: 4521

How to make instrumented tests that execute only when running on a device with an OS version higher than KitKat

I have a complete class that extends from AndroidTestCase.

The things that I test there should only be tested on KitKat and above.

However if I run my tests on an older device (using gradle connectedCheck) those tests obviously fail.

How can I explicitly tell the framework to execute those tests only if running on KitKat or above.

One solution would be to encapsulate the code inside each test with an if like this:

public void testA() {
    if (android.os.Build.VERSION >= Build.VERSION_CODES.KIT_KAT) {
      // test stuff
    }
}

But I want a way to do this for all tests inside my class.

Upvotes: 0

Views: 67

Answers (1)

Phil
Phil

Reputation: 4069

You should be able to look at the android.os.Build.VERSION constant. For KitKat I believe it would be API level 19. So you would only execute your code if the constant returns 19 or higher

if (android.os.Build.VERSION >= 19) {
     // do your stuff
}

Upvotes: 1

Related Questions