【问题标题】:How can I share code between test module and app module? - Fakes depend on code from app module如何在测试模块和应用程序模块之间共享代码? - 假货取决于来自应用程序模块的代码
【发布时间】: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


【解决方案1】:

我几周前刚做过这个。 创建共享测试模块后。您必须提供共享测试所需的每个实现。然后还有

implementation(project(":app"))

如果您的项目有多种产品风味,那么您必须像这样在共享模块中定义所有产品风味:


android {
    flavorDimensions.add("env")
    productFlavors {
        create("dev") {
            dimension = "env"
        }

        create("staging") {
            dimension = "env"
        }

        create("production") {
            dimension = "env"
        }
}

并将其添加到 app build.gradle

    testImplementation(project(":SharedTest"))
    androidTestImplementation(project(":SharedTest"))

希望它对你有用!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 2016-10-19
    • 2017-05-21
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多