Health Connect Testing 库 (androidx.health.connect:connect-testing
) 简化了自动化测试的创建。您可以使用此库验证应用的行为,并验证其是否正确响应手动难以测试的罕见情况。
您可以使用此库创建本地单元测试,这些测试通常用于验证应用中与 Health Connect 客户端交互的类的行为。
要开始使用此库,请将其添加为测试依赖项
testImplementation("androidx.health.connect:connect-testing:1.0.0-alpha01")
此库的入口点是 FakeHealthConnectClient
类,您可以在测试中使用它来替换 HealthConnectClient
。FakeHealthConnectClient
具有以下功能
- 记录的内存中表示,因此您可以插入、移除、删除和读取它们
- 更改令牌的生成和更改跟踪
- 记录和更改的分页
- 聚合响应支持存根
- 允许任何函数抛出异常
- 可用于模拟权限检查的
FakePermissionController
要详细了解如何在测试中替换依赖项,请阅读Android 中的依赖注入。要详细了解伪对象,请阅读在 Android 中使用测试替身。
例如,如果与客户端交互的类名为 HealthConnectManager
并且它将 HealthConnectClient
作为依赖项,则它看起来会像
class HealthConnectManager(
private val healthConnectClient: HealthConnectClient,
...
) { }
在测试中,您可以将伪对象传递给待测试的类
import androidx.health.connect.client.testing.ExperimentalTestingApi
import androidx.health.connect.client.testing.FakeHealthConnectClient
import kotlinx.coroutines.test.runTest
@OptIn(ExperimentalTestingApi::class)
class HealthConnectManagerTest {
@Test
fun readRecords_filterByActivity() = runTest {
// Create a Fake with 2 running records.
val fake = FakeHealthConnectClient()
fake.insertRecords(listOf(fakeRunRecord1, fakeBikeRecord1))
// Create a manager that depends on the fake.
val manager = HealthConnectManager(fake)
// Read running records only.
val runningRecords = manager.fetchReport(activity = Running)
// Verify that the records were filtered correctly.
assertTrue(runningRecords.size == 1)
}
}
此测试验证 HealthConnectManager
中虚构的 fetchReport
函数是否按 activity 正确过滤记录。
验证异常
几乎所有对 HealthConnectClient
的调用都可能抛出异常。例如,insertRecords
的文档中提到了以下异常
@throws android.os.RemoteException
表示任何 IPC 传输失败。@throws SecurityException
表示未经允许的访问请求。@throws java.io.IOException
表示任何磁盘 I/O 问题。
这些异常涵盖了连接不良或设备上没有剩余空间等情况。您的应用必须对这些运行时问题做出正确响应,因为它们随时可能发生。
import androidx.health.connect.client.testing.stubs.stub
@Test
fun addRecords_throwsRemoteException_errorIsExposed() {
// Create Fake that throws a RemoteException
// when insertRecords is called.
val fake = FakeHealthConnectClient()
fake.overrides.insertRecords = stub { throw RemoteException() }
// Create a manager that depends on the fake.
val manager = HealthConnectManager(fake)
// Insert a record.
manager.addRecords(fakeRunRecord1)
// Verify that the manager is exposing an error.
assertTrue(manager.errors.size == 1)
}
聚合
聚合调用没有伪实现。相反,聚合调用使用存根,您可以对其进行编程以使其以某种方式行为。您可以通过 FakeHealthConnectClient
的 overrides
属性访问这些存根。
例如,您可以编程聚合函数以返回特定结果
import androidx.health.connect.client.testing.AggregationResult
import androidx.health.connect.client.records.HeartRateRecord
import androidx.health.connect.client.records.ExerciseSessionRecord
import java.time.Duration
@Test
fun aggregate() {
// Create a fake result.
val result =
AggregationResult(metrics =
buildMap {
put(HeartRateRecord.BPM_AVG, 74.0)
put(
ExerciseSessionRecord.EXERCISE_DURATION_TOTAL,
Duration.ofMinutes(30)
)
}
)
// Create a fake that always returns the fake
// result when aggregate() is called.
val fake = FakeHealthConnectClient()
fake.overrides.aggregate = stub(result)
然后,您可以验证您的待测试类(在本例中为 HealthConnectManager
)是否正确处理了结果
// Create a manager that depends on the fake.
val manager = HealthConnectManager(fake)
// Call the function that in turn calls aggregate on the client.
val report = manager.getHeartRateReport()
// Verify that the manager is exposing an error.
assertThat(report.bpmAverage).isEqualTo(74.0)
权限
该测试库包含一个 FakePermissionController
,可以作为依赖项传递给 FakeHealthConnectClient
。
您的待测试对象可以使用 PermissionController
(通过 HealthConnectClient
接口的 permissionController
属性)来检查权限。这通常在每次调用客户端之前完成。
要测试此功能,您可以使用 FakePermissionController
设置哪些权限可用
import androidx.health.connect.client.testing.FakePermissionController
@Test
fun newRecords_noPermissions_errorIsExposed() {
// Create a permission controller with no permissions.
val permissionController = FakePermissionController(grantAll = false)
// Create a fake client with the permission controller.
val fake = FakeHealthConnectClient(permissionController = permissionController)
// Create a manager that depends on the fake.
val manager = HealthConnectManager(fake)
// Call addRecords so that the permission check is made.
manager.addRecords(fakeRunRecord1)
// Verify that the manager is exposing an error.
assertThat(manager.errors).hasSize(1)
}
分页
分页是错误的一个非常常见的来源,因此 FakeHealthConnectClient
提供了机制来帮助您验证您的记录和更改的分页实现是否行为正确。
您的待测试对象(在我们的示例中是 HealthConnectManager
)可以在 ReadRecordsRequest
中指定页面大小
fun fetchRecordsReport(pageSize: Int = 1000) }
val pagedRequest =
ReadRecordsRequest(
timeRangeFilter = ...,
recordType = ...,
pageToken = page1.pageToken,
pageSize = pageSize,
)
val page = client.readRecords(pagedRequest)
...
将页面大小设置为一个较小的值(例如 2),可以轻松测试分页。例如,您可以插入 5 条记录,以便 readRecords
返回 3 个不同的页面
@Test
fun readRecords_multiplePages() = runTest {
// Create a Fake with 2 running records.
val fake = FakeHealthConnectClient()
fake.insertRecords(generateRunningRecords(5))
// Create a manager that depends on the fake.
val manager = HealthConnectManager(fake)
// Read records with a page size of 2.
val report = manager.generateReport(pageSize = 2)
// Verify that all the pages were processed correctly.
assertTrue(report.records.size == 5)
}
测试数据
该库尚未包含生成伪数据的 API,但您可以在Android 代码搜索中使用该库使用的数据和生成器。
存根
FakeHealthConnectClient
的 overrides
属性允许您编程(或存根化)其任何函数,以便在调用时抛出异常。聚合调用也可以返回任意数据,并且支持将多个响应排队。有关详细信息,请参阅 Stub
和 MutableStub
。
边缘情况摘要
- 验证当客户端抛出异常时您的应用是否按预期运行。检查每个函数的文档以确定您应该检查哪些异常。
- 验证您对客户端的每次调用之前都进行了正确的权限检查。
- 验证您的分页实现。
- 验证当您获取多个页面但其中一个页面的令牌已过期时会发生什么。