断言与执行操作
验证状态并模拟用户输入。
断言与执行操作 是 CoddyKit 上的免费 Jetpack Compose Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Jetpack Compose Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Jetpack Compose Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Find, Then Verify
Finding a node is only half the job. To prove your UI behaves, you add an assertion that states what should be true about that node. ✅
assertIsDisplayed
The most common assertion is assertIsDisplayed. It confirms the node is actually visible to the user, not just present in the tree.
composeTestRule.onNodeWithText("Welcome")
.assertIsDisplayed()assertExists vs assertIsDisplayed
Watch the difference: assertExists checks the node is in the tree, while assertIsDisplayed also checks it is on screen and not clipped away.
composeTestRule.onNodeWithText("Footer")
.assertExists()Chaining Assertions
You can chain several checks on one node. Each returns the node again, so the test reads like a clear sentence about your UI state.
rule.onNodeWithText("Save")
.assertIsDisplayed()
.assertIsEnabled()Performing a Click
To simulate a tap, call performClick on a found node. Compose dispatches the same click your real users would trigger.
composeTestRule.onNodeWithText("Add")
.performClick()Typing Text
For input fields, performTextInput types characters into the focused field, letting you test forms exactly as a user would fill them.
rule.onNodeWithText("Email")
.performTextInput("a@b.com")Act, Then Assert
The classic flow is act then assert: perform a click, then verify the new state appeared. This mirrors how a person uses your app.
rule.onNodeWithText("+").performClick()
rule.onNodeWithText("Count: 1")
.assertIsDisplayed()Asserting on State Flags
Beyond visibility, you can check toggles. assertIsOn and assertIsOff verify switches and checkboxes hold the value you expect.
rule.onNodeWithText("Notifications")
.assertIsOff()Quick Check
You want to confirm a button is genuinely on screen, not merely present in the tree. Which assertion do you use?
Assert on Count
When you grab a group with onAllNodes, verify how many matched using assertCountEquals. It is perfect for checking list sizes.
rule.onAllNodesWithText("Item")
.assertCountEquals(3)Clear Before You Type
Replacing text? Call performTextClearance first, then performTextInput, so old characters do not stick around and break your check.
rule.onNodeWithText("Name")
.performTextClearance()Readable, Trustworthy Tests
Good assertions describe behavior, not pixels. Aim for tests that read like requirements, so a failure instantly tells you what behavior broke.
Recap: Actions & Assertions
You drive the UI with actions like performClick and performTextInput, then prove the result with assertions like assertIsDisplayed. That is the heart of UI testing. 🎯
常见问题解答
「断言与执行操作」课时是免费的吗?
是的 — 「断言与执行操作」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Jetpack Compose Academy 课程的其余内容,请升级到 CoddyKit PRO。 Jetpack Compose Academy 课程共包含 4 节课。
「断言与执行操作」这节课中我会学到什么?
验证状态并模拟用户输入。 你通过在浏览器中直接运行的动手代码来练习 Jetpack Compose Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Jetpack Compose Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Jetpack Compose Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「断言与执行操作」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Jetpack Compose Academy 课中编写并运行代码吗?
能。每节 Jetpack Compose Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。