创建插桩测试类

game365备用网址 📅 2026-06-17 15:41:44 👤 admin 👁️ 8867 ❤️ 920
创建插桩测试类

插桩测试在 Android 设备上运行,无论是真机还是模拟设备。如

因此,它们可以利用 Android 框架 API插桩测试

因此能提供比本地测试更高的保真度

慢慢来。

我们建议,只有在必须针对以下各项进行测试时使用插桩测试

真实设备的行为AndroidX Test 提供了多个库

可以在必要时更轻松地编写插桩测试。

注意 :插桩测试(也称为插桩测试)会初始化为插桩测试,

在特殊环境中可以访问

插桩。此类提供对应用上下文和

用于操控被测应用并为插桩测试命名的 API。

设置测试环境

在 Android Studio 项目中,您可以将用于插桩测试的源文件

测试(位于 module-name/src/androidTest/java/ 中)。此目录已存在,

您可以创建一个新项目,其中包含一个插桩测试示例。

在开始之前,您应该添加 AndroidX Test API,以便快速

为您的应用构建和运行插桩测试代码。AndroidX Test 包含一个

JUnit 4 测试运行程序、AndroidJUnitRunner 和用于功能界面测试的 API

例如 Espresso、UI Automator 和 Compose 测试。

您还需要为项目配置 Android 测试依赖项,

使用 AndroidX Test 提供的测试运行程序和规则 API。

在应用的顶级 build.gradle 文件中,您需要指定这些库

依赖项:

dependencies {

androidTestImplementation "androidx.test:runner:$androidXTestVersion"

androidTestImplementation "androidx.test:rules:$androidXTestVersion"

// Optional -- UI testing with Espresso

androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"

// Optional -- UI testing with UI Automator

androidTestImplementation "androidx.test.uiautomator:uiautomator:$uiAutomatorVersion"

// Optional -- UI testing with Compose

androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"

}

您可以在 AndroidX 版本说明和 Compose 中找到最新版本

界面版本说明。

要使用 JUnit 4 测试类并访问测试过滤等功能,

确保将 AndroidJUnitRunner 指定为默认测试插桩

为项目运行程序,只需在应用的

模块级 build.gradle 文件:

android {

defaultConfig {

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

}

}

创建插桩测试类

插桩测试类应该是 JUnit 4 测试类,该类类似于

关于如何构建本地测试的部分中介绍的类。

如需创建 JUnit 4 插桩测试类,请将 AndroidJUnit4 指定为

默认测试运行程序。

注意 :如果您的测试套件依赖于 JUnit3 和 JUnit4 库的混合搭配,请将

测试开头的 @RunWith(AndroidJUnit4::class) 注解

类定义。

以下示例展示了如何编写插桩测试来验证

Parcelable 接口已针对

LogHistory 类:

Kotlin

import android.os.Parcel

import android.text.TextUtils.writeToParcel

import androidx.test.filters.SmallTest

import androidx.test.runner.AndroidJUnit4

import com.google.common.truth.Truth.assertThat

import org.junit.Before

import org.junit.Test

import org.junit.runner.RunWith

const val TEST_STRING = "This is a string"

const val TEST_LONG = 12345678L

// @RunWith is required only if you use a mix of JUnit3 and JUnit4.

@RunWith(AndroidJUnit4::class)

@SmallTest

class LogHistoryAndroidUnitTest {

private lateinit var logHistory: LogHistory

@Before

fun createLogHistory() {

logHistory = LogHistory()

}

@Test

fun logHistory_ParcelableWriteRead() {

val parcel = Parcel.obtain()

logHistory.apply {

// Set up the Parcelable object to send and receive.

addEntry(TEST_STRING, TEST_LONG)

// Write the data.

writeToParcel(parcel, describeContents())

}

// After you're done with writing, you need to reset the parcel for reading.

parcel.setDataPosition(0)

// Read the data.

val createdFromParcel: LogHistory = LogHistory.CREATOR.createFromParcel(parcel)

createdFromParcel.getData().also { createdFromParcelData: List> ->

// Verify that the received data is correct.

assertThat(createdFromParcelData.size).isEqualTo(1)

assertThat(createdFromParcelData[0].first).isEqualTo(TEST_STRING)

assertThat(createdFromParcelData[0].second).isEqualTo(TEST_LONG)

}

}

}

Java

import android.os.Parcel;

import android.util.Pair;

import androidx.test.runner.AndroidJUnit4;

import com.google.common.truth.Truth.assertThat;

import java.util.List;

import org.junit.Before;

import org.junit.Test;

import org.junit.runner.RunWith;

// @RunWith is required only if you use a mix of JUnit3 and JUnit4.

@RunWith(AndroidJUnit4.class)

public class LogHistoryAndroidUnitTest {

public static final String TEST_STRING = "This is a string";

public static final long TEST_LONG = 12345678L;

private LogHistory mLogHistory;

@Before

public void createLogHistory() {

mLogHistory = new LogHistory();

}

@Test

public void logHistory_ParcelableWriteRead() {

// Set up the Parcelable object to send and receive.

mLogHistory.addEntry(TEST_STRING, TEST_LONG);

// Write the data.

Parcel parcel = Parcel.obtain();

mLogHistory.writeToParcel(parcel, mLogHistory.describeContents());

// After you're done with writing, you need to reset the parcel for reading.

parcel.setDataPosition(0);

// Read the data.

LogHistory createdFromParcel = LogHistory.CREATOR.createFromParcel(parcel);

List> createdFromParcelData

= createdFromParcel.getData();

// Verify that the received data is correct.

assertThat(createdFromParcelData.size()).isEqualTo(1);

assertThat(createdFromParcelData.get(0).first).isEqualTo(TEST_STRING);

assertThat(createdFromParcelData.get(0).second).isEqaulTo(TEST_LONG);

}

}

注意 :只有设备支持在 Kotlin 中使用反引号为测试命名

运行 API 30 及更高版本。例如:

@Test fun `everything works`() { ... }

运行插桩测试

插桩测试可以在真实设备或模拟器上运行。在 Android 中

通过 Studio 指南,您可以了解如何:

在 Android Studio 中测试

从命令行进行测试

其他资源

界面测试通常是插桩测试,用于验证

界面它们使用 Espresso 或 Compose Test 等框架。学习内容

请阅读界面测试指南。

如需详细了解如何使用插桩测试,请参阅

资源。

示例

插桩单元测试代码示例

Codelab

Android 测试 Codelab

相关推荐

九阴真经失魂刀法打法套路技巧
365足球打水封号还严重嘛

九阴真经失魂刀法打法套路技巧

📅 08-13 👁️ 1560
于洪亮硬笔行楷
365足球打水封号还严重嘛

于洪亮硬笔行楷

📅 02-20 👁️ 6561
word文档第一页空白页怎么删除
365足球打水封号还严重嘛

word文档第一页空白页怎么删除

📅 02-11 👁️ 6771