asp.net网站恢复,做淘客网站多少钱,西安网络建站,wordpress页面管理插件文章目录 了解测试相关库导入依赖库新建测试文件示例执行查看结果网页结果其他 本片讲解的重点是unitTest#xff0c;而不是androidTest哦 了解测试相关库 androidx.compose.ui:ui-test-junit4: 用于Compose UI的JUnit 4测试库。 它提供了测试Compose UI组件的工具和API。 and… 文章目录 了解测试相关库导入依赖库新建测试文件示例执行查看结果网页结果其他 本片讲解的重点是unitTest而不是androidTest哦 了解测试相关库 androidx.compose.ui:ui-test-junit4: 用于Compose UI的JUnit 4测试库。 它提供了测试Compose UI组件的工具和API。 androidx.test.ext:junit-ktx: JUnit 4的Kotlin扩展库。 它提供了Kotlin友好的JUnit4注解和扩展函数。 com.google.truth:truth: 一个用于编写简洁、可读性强的断言的库。 它提供了比JUnit自带的断言更丰富和更易用的API。 io.mockk:mockk: 一个用于Kotlin的Mockito风格的模拟框架。 它允许你在测试中创建和使用mock对象。 org.robolectric:robolectric: 一个用于Android的单元测试框架。 它允许你在JVM上运行测试而不是在真实的Android设备或模拟器上从而加快测试速度。 androidx.arch.core:core-testing: Android Architecture Components的测试库。 它提供了用于测试LiveData、Room等组件的工具。 org.jetbrains.kotlinx:kotlinx-coroutines-test: Kotlin协程的测试库。 它提供了用于测试协程的工具和API。 androidx.test.ext:junit: AndroidX的JUnit扩展库。 它提供了额外的JUnit注解和功能特别是针对Android测试。 androidx.test.espresso:espresso-core: 一个用于Android UI测试的框架。 它允许你编写测试来模拟用户与UI组件的交互。 导入依赖库
// 依赖Android环境
// androidTestImplementation Dependencies.junit4
// androidTestImplementation Dependencies.junitExtensionsKtx
// androidTestImplementation Dependencies.truth
// androidTestImplementation Dependencies.mockk
// androidTestImplementation Dependencies.coroutinesTest// 依赖JAVA环境testImplementation Dependencies.junit4testImplementation Dependencies.junitExtensionsKtxtestImplementation Dependencies.truthtestImplementation Dependencies.mockktestImplementation Dependencies.coroutinesTest新建测试文件 示例
package com.guide.module_mainimport androidx.test.ext.junit.runners.AndroidJUnit4
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import junit.framework.TestCase.assertEquals
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith/*** date 2024/12/25* description ftp测试用例* version 1.0*/// UserService.kt
interface UserService {fun getUserById(id: Int): User?
}// UserServiceImpl.kt
class UserServiceImpl : UserService {override fun getUserById(id: Int): User? {// 实际的实现会从数据库或其他服务中获取用户return null}
}// User.kt
data class User(val id: Int, val name: String)class FtpActivityTest {Beforefun setUp() {}Testfun testFtp() {assertEquals(1, 1)}Testfun getUserById() {// 创建UserService的mock对象val userServiceMock mockkUserService()// 定义mock对象的行为val expectedUser User(1, John Doe)every { userServiceMock.getUserById(1) } returns expectedUser// 调用mock对象的方法val actualUser userServiceMock.getUserById(1)// 验证mock对象的方法是否被调用verify { userServiceMock.getUserById(1) }//断言返回的用户是否符合预期assertEquals(expectedUser, actualUser)}
}
执行 查看结果 网页结果 其他
点灯篇⭐⭐⭐ https://blog.csdn.net/Agg_bin/article/details/120768579
mockk的使用⭐⭐⭐⭐ https://blog.csdn.net/rikkatheworld/article/details/115823178 https://juejin.cn/post/7304236588148752436