有没有办法为目标编写单元测试 API

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

我正在编写 Android 某个区域的仪器测试 Robolectric 自定义阴影不足。

理想情况下,我们希望编写在所有版本的 Android SDK 中都灵活的代码,但是我处于边缘情况,我需要为仅适用于 Marshmallow 的方法编写单独的单元测试.

我还需要编写一个仅适用于 Lollipop 及以下版本的单元测试,因为操作系统依赖性存在差异(即 java.lang.NoClassDefFoundError)

我是否可以通过类似 Junit4 的注释或类似于 Robolectric 所做的事情来做到这一点,如果 SDK 不合适,它可以忽略 运行 测试?

我不想写这样的代码:

// 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...
    }
}

我对单元测试或 Robolectric 不是很熟悉,但是因为在我编写单元测试时不支持 API 23 我使用了那个配置:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21) //this guy
public class MainActivityTest {

    MainActivity_ activity = Robolectric.setupActivity(MainActivity_.class);

}

就像您看到的那样,有一个注释可以用于您的测试 classes.


编辑:

抱歉,我只关注 Robolectric 测试框架,而不是主要问题。

为了注释特定 API 的仪器测试,我会使用:

1。 Class 带@Before 注解

创建一个带有@Before 注释的class,它将检查API 被测试设备。如果错误,测试将在此方法中失败。使用fail();方法。

2。使用@SdkSuppress注解

Indicates that a specific test or class requires a minimum API Level to execute.

Test(s) will be skipped when executed on android platforms less than specified level.

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

所以如果你设置 @SdkSuppress(minSdkVersion=23) 它只会在 Android Marshmallow 设备上 运行 而如果 @@SdkSuppress(minSdkVersion=20) 它只会在更高的 5.0 上 运行 API 台设备。

另请阅读:http://www.vogella.com/tutorials/AndroidTesting/article.html

3。创建您自己的注释,如 @SdkOnly

也许这篇文章会有用:http://help.testdroid.com/customer/portal/articles/1256803-using-annotations-in-android-instrumentation-tests

4.为您的特定仪器测试创建套件

为此,您将使用 @RunWith()Suites.SuiteClasses() 注释。

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

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

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

The following example shows how you might implement a test suite called UnitTestSuite that 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.有用的资源

希望对您有所帮助