Task 1 — Automated Testing Deep Dive & Mapping
1. Research & Define:
◦ Write definitions (1–2 lines each) for: unit test, instrumented UI test,
ComposeTestRule, semantics tree.
◦ For each, provide one example scenario in a Compose app (for instance: verifying
a business card layout vs. verifying button click behavior).
1. Unit Test
Definition: A unit test checks individual functions or logic components in isolation
without launching the full Android UI or device environment.
Example Scenario: Testing a function that calculates a discount price in a Compose
shopping app without rendering the UI.
2. Instrumented UI Test
Definition: An instrumented UI test runs on a real device or emulator and verifies
actual UI behavior and interactions.
Example Scenario: Testing whether a Button click in a Compose screen updates the
displayed counter text correctly.
3. ComposeTestRule
Definition: ComposeTestRule is a testing rule provided by Jetpack Compose that sets
up and controls the Compose UI environment for testing.
Example Scenario: Using ComposeTestRule to launch a composable screen and assert
that a Text element displays “Welcome User” when the app starts.
4. Semantics Tree
Definition: The semantics tree is a structured representation of UI elements in
Compose used for accessibility and testing, exposing properties like text, content
descriptions, and actions.
Example Scenario: Verifying that an Image in a Compose profile card has the correct
contentDescription for accessibility testing.
2. Select a UI Feature to Test:
◦ Choose one small feature (for example: in your Art Space app, a “Next Artwork”
button updates the artwork displayed).
◦ Identify the UI elements involved (Button, Image, Text) and the expected
behavior when the user taps “Next”.
Selected UI Feature: “Next Artwork” Button (Art Space App)
Feature Description: When the user taps the “Next Artwork” button, the app displays
the next artwork along with its updated title and artist name.
UI Elements Involved:
1. Button
o Label: “Next”
o Action: Triggers state update to load the next artwork.
2. Image
o Displays the current artwork image.
o Changes to the next artwork image when the button is tapped.
3. Text (Title)
o Displays the artwork title.
o Updates to the next artwork’s title.
4. Text (Artist Name / Year)
o Displays artist name and year.
o Updates accordingly when the artwork changes.
Expected Behavior When User Taps “Next”:
• The app updates the internal state (e.g., currentArtworkIndex++).
• The Image changes to the next artwork.
• The Title Text updates to the correct new artwork title.
• The Artist Text updates to match the new artwork.
• If the last artwork is reached, it either:
o Loops back to the first artwork, or
o Disables the button (based on implementation).
3. Design a Test Strategy Document:
◦ Create a table or annotated flow diagram, including: | Test Type | What to Test |
Setup Needed | Assertion / Verification | For example: UI Test → tap Next button
→ new image displayed & title updated.
◦ Specify whether it should be a unit test or instrumented UI test, and why.
Test Type What to Test Setup Needed Assertion / Verification
Artwork index Verify that index
Isolated function that
update logic increases correctly and
Unit Test increments artwork index
(next artwork wraps to first artwork
and returns next artwork data
selection) when reaching the end
Perform click on “Next”
Instrumented Tap “Next” ComposeTestRule, launch
button and verify UI
UI Test button interaction ArtSpaceScreen composable
recomposes
Test tag or Assert that the displayed
Instrumented Image change
contentDescription on Image image is different from
UI Test after button click
composable the previous one
Check that title text
Instrumented Artwork title Semantics node with text tag
matches the next
UI Test update (e.g., artworkTitle)
artwork’s title
Verify that artist text
Instrumented Artist name/year
Semantics node for artist text updates according to the
UI Test update
new artwork data
Annotated Test Flow (Step-by-Step)
1. Launch the composable screen using ComposeTestRule.
2. Confirm initial state (first artwork image, title, and artist displayed).
3. Locate the “Next” button using a test tag or text.
4. Perform a click action on the button.
5. Observe recomposition of UI.
6. Verify:
o Image has changed
o Title text is updated
o Artist text is updated
Test Type Selection & Justification
• Unit Test: Used for testing the artwork index logic because it focuses only on business
logic (state change) and does not require UI rendering. This makes the test faster and
isolated.
• Instrumented UI Test: Required for verifying button clicks, image updates, and text
recomposition because these involve real UI interaction and Compose rendering on a
device/emulator.
4. Reflection:
◦ Write 4-5 lines on: why writing tests early improves code quality; what happens
if state changes but UI isn’t verified; one challenge you anticipate in writing
Compose UI tests.
Writing tests early forces me to design cleaner state logic and well-structured
composables, which directly improves code quality and reduces bugs later. If the state
changes but the UI isn’t verified, the app may recompose incorrectly, showing outdated
images or text even though the logic is correct, leading to a poor user experience. Early
testing also helps catch recomposition and state management issues before the UI
becomes complex. One challenge I anticipate in Compose UI tests is properly
identifying UI elements using test tags or semantics, especially when multiple
recompositions occur. Another difficulty could be handling asynchronous state updates
and ensuring the test waits for the UI to stabilize before making assertions.