Is there a way to write unit test for the target API

I am in the process of writing tests on Android hardware tools in an area that does not meet Robolectric requirements.

Ideally, we want to write code that is flexible in all versions of the Android SDK, however, I am in a situation where I need to write a separate unit test for a method that works only with Marshmallow.

I also need to write a unit test that only works for Lollipop and due to differences in the operating system dependencies (i.e. java.lang.NoClassDefFoundError)

Anyway, can I do this with Junit4 comments or something similar to what Robolectric does, where can it ignore running tests if the SDK is not suitable?

I do not want to write code like this:

// MarshmallowWidgetTest.java @Test public void testPrintName() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // ...asserts... } } // LollipopWidgetTest.java @Test public void testPrintName() { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) { // ...asserts... } } 
+6
source share
1 answer

I am not very good at unit testing or Robolectric , but since I did not have API 23 support when writing unit tests, I used this configuration:

 @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) //this guy public class MainActivityTest { MainActivity_ activity = Robolectric.setupActivity(MainActivity_.class); } 

So, you see there an annotation that you can use for your test classes.


EDIT:

Sorry that I focused only on the Robolectric test structure, not the main problem.

To annotate benchmarks for a specific API, I would use:

1. Class with @Before annotation

Create a class with @Before annotation, where it will check the API of the devices under test. If this is not the case, the tests will fail in this method. Use the fail(); method fail(); .

2. Use the @SdkSuppress annotation

Indicates that a specific test level or class requires a minimum level of API.

The test will be skipped when executed on Android platforms below the specified level.

From: http://developer.android.com/reference/android/support/test/filters/SdkSuppress.html

So, if you install @SdkSuppress(minSdkVersion=23) , it will only work on Android Marshmallow devices, and if @ @SdkSuppress(minSdkVersion=20) , it will only work on higher-level APIs.

Read also: http://www.vogella.com/tutorials/AndroidTesting/article.html

3. Create your own annotation, for example @SdkOnly

Perhaps this article would be helpful: http://help.testdroid.com/customer/portal/articles/1256803-using-annotations-in-android-instrumentation-tests

4. Create kits for your special benchmarks

For this purpose, you used the annotations @RunWith() and Suites.SuiteClasses() .

To organize the execution of unit tests, you can group a set of test classes in a test set class and run these tests together. A set of tests can be nested; your test suite can group other test suites and all component testing classes together.

The test suite is contained in a test package similar to the main application package. By convention, the package name of the test package usually ends with the suffix .suite (for example, com.example.android.testing.mysample.suite).

To create a test suite for your unit tests, import JUnit RunWith and Suite . In your test suite, add @RunWith(Suite.class) and @Suite.SuitClasses() annotations. In the @Suite.SuiteClasses() annotations, list individual test classes or test Arguments.

The following example shows how to implement a test suite called UnitTestSuite , which groups and runs the CalculatorInstrumentationTest and CalculatorAddParameterizedTest test classes together.

 import com.example.android.testing.mysample.CalculatorAddParameterizedTest; import com.example.android.testing.mysample.CalculatorInstrumentationTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; // Runs all unit tests. @RunWith(Suite.class) @Suite.SuiteClasses({CalculatorInstrumentationTest.class, CalculatorAddParameterizedTest.class}) public class UnitTestSuite {} 

From: http://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html

5. Useful resources

Hope this helps

+6
source

All Articles