【发布时间】:2022-11-30 13:13:53
【问题描述】:
我希望能够创建一个单独的测试共享模块,以便能够在 androidTest 和测试包之间共享。我的问题是假货需要来自应用程序模块(存储库、数据模型、DTO 等)的代码,如果将它放在单独的测试共享模块中,则看不到该代码。
我需要制造假货吗?一切能够在测试包之间共享代码?好像过分了
或者实际上有没有办法让测试共享模块从应用程序模块导入依赖项?那会很方便。
我是新手,所以我确定我遗漏了一些明显的东西。
资源:https://blog.danlew.net/2022/08/16/sharing-code-between-test-modules/
这是我的一个假货的例子,它需要来自应用程序模块的代码。 -(加密存储库、CoinDto、CoinDetailsDto)
class FakeRepository : CryptoRepository {
override suspend fun getCoinData(): List<CoinDto> {
val data = CoinDto(
id = "id",
is_active = true,
is_new = true,
name = "Test Coin",
rank = 1,
symbol = "Test Symbol",
type = "Test Type"
)
return listOf(data)
}
override suspend fun getCoinDetails(id: String): CoinDetailsDto {
return CoinDetailsDto(
description = "Test Description",
developmentStatus = "Test Status",
firstDataAt = "Test Data At",
hardwareWallet = true,
hashAlgorithm = "Test Hash Algorithm",
id = "id",
isActive = true,
isNew = true,
lastDataAt = "Test Last Data At",
links = fakeLink,
linksExtended = fakeLinksExtended,
logo = "https://static.coinpaprika.com/coin/bnb-binance-coin/logo.png",
message = "Test Message",
name = "Test Coin Name",
openSource = true,
orgStructure = "Test Org Structure",
proofType = "Test Proof Type",
rank = 1,
startedAt = "Test Started At",
symbol = "TTC",
tags = listOf(fakeTag),
team = listOf(fakeTeamMember),
type = "Test Type"
)
}
override suspend fun getPriceCoinDetails(id: String): CoinPriceDetailsDtoItem {
return CoinPriceDetailsDtoItem(
beta_value = 0.5,
circulating_supply = 1000L,
first_data_at = "Test First Data At",
id = "id",
last_updated = "Test Last Data At",
max_supply = 10000L,
name = "Tes Coin Name",
quotes = fakeQuotes,
rank = 1,
symbol = "TTC",
total_supply = 1000L
)
}
}
【问题讨论】:
-
一种方法是将可重用的东西从应用程序模块中提取到他们自己的模块(业务内容)中。这样你的共享模块就可以依赖它而不依赖于应用程序。
-
只需使用Java Test Fixtures。
标签: android android-studio kotlin testing android-testing