Skip to content

Wrong branch coverage for composables #756

@FelberMartin

Description

@FelberMartin

Describe the bug
For functions annotated with @Composable it seems that the branch coverage can not exceed 50%. This might be caused by the androidx.compose.runtime.Composer (and Integer) argument added to every composable function by the compose compiler.
This issue was also mentioned here in this comment.

Expected behavior
When all parameters of the composable function are covered in tests, the reported branch coverage should be 100%.

Reproducer
Very simplistic example:

ComposableBranchCoverageBug.kt

@Composable
internal fun MyText(
    text: String
) {               // <-- Line number 9 with the reported 2/4 missed branches
    Text(
        text = text,
    )
}

Test file:

@RunWith(AndroidJUnit4::class)
class ComposableBranchCoverageBugTest {
    @get:Rule
    val composeTestRule = createComposeRule()

    @After
    fun tearDown() {
        stopKoin()
    }

    @Test
    fun `simple test`() {
        composeTestRule.setContent {
            MyText(
                text = "Abc"
            )
        }

        composeTestRule.onNodeWithText("Abc")
            .assertExists()
    }
}

Reports
xmlReport:

        <class name=".../ComposableBranchCoverageBugKt"
            sourcefilename="ComposableBranchCoverageBug.kt">
            <method name="MyText" desc="(Ljava/lang/String;Landroidx/compose/runtime/Composer;I)V">
                <counter covered="80" missed="2" type="INSTRUCTION" />
                <counter covered="2" missed="2" type="BRANCH" />
                <counter covered="4" missed="0" type="LINE" />
            </method>
            <counter covered="80" missed="2" type="INSTRUCTION" />
            <counter covered="2" missed="2" type="BRANCH" />
            <counter covered="4" missed="0" type="LINE" />
            <counter covered="1" missed="0" type="METHOD" />
        </class>

...

        <sourcefile name="ComposableBranchCoverageBug.kt">
            <line cb="2" ci="42" mb="2" mi="2" nr="9" />
            <line cb="0" ci="5" mb="0" mi="0" nr="10" />
            <line cb="0" ci="23" mb="0" mi="0" nr="11" />
            <line cb="0" ci="10" mb="0" mi="0" nr="13" />
            <counter covered="80" missed="2" type="INSTRUCTION" />
            <counter covered="2" missed="2" type="BRANCH" />
            <counter covered="4" missed="0" type="LINE" />
        </sourcefile>

Note that the MyText method has the Composer and an integer (I) as additional arguments and the reported branch coverage is 2/4.

Environment

  • Kover Gradle Plugin version: 0.9.1
  • Gradle version: 8.10.1
  • Kotlin project type: Kotlin/Multiplatform + Android
  • Coverage Toolset: Kover

Metadata

Metadata

Assignees

Labels

BugBug issue typeS: untriagedStatus: issue reported but unprocessed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions