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

标签 android unit-testing robolectric android-instrumentation

我正在一个区域编写 Android 插桩测试,但 Robolectric 自定义阴影不足。

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

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

无论如何,我是否可以通过类似 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);

}

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


编辑:

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

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

<强>1。带有@Before 注解的类

创建一个带有@Before 注释的类,它将在其中检查被测设备的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。有用的资源

希望对你有帮助

关于android - 有没有办法为目标 API 编写单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34532451/

相关文章:

android - 在 android Test 文件夹中创建 Dummy Activity 进行测试

java - 如何模拟 Dao 两次但使用同一个对象调用?

java - Robolectric - AndroidStudio - 找不到参数的方法 testCompile()

android - 如何以编程方式更改 TTS 默认引擎

android - 在维度中分配包装内容

unit-testing - 我是否应该使用不应在函数中传递的数据(无效输入)进行单元测试?

android - 单元测试 BroadcastReceiver

android - Robolectric, Gradle, Espresso with Flavours

android - 如何使用 Parse 在 Android 中进行查询?

java - super 新手问题 : Getting a returned integer value from a Method