0
0
Android Kotlinmobile~15 mins

Why testing ensures app reliability in Android Kotlin - Build It to Prove It

Choose your learning style9 modes available
Build: App Reliability Tester
This screen helps demonstrate how testing ensures app reliability by running a simple test and showing the result.
Target UI
-------------------------
|  App Reliability Tester |
|-----------------------|
| [Run Test]            |
|                       |
| Test Result:          |
|                       |
|                       |
-------------------------
Add a button labeled 'Run Test'.
When the button is tapped, run a simple test function that returns success or failure.
Display the test result below the button as 'Test Passed' or 'Test Failed'.
Use Kotlin and Android best practices.
Ensure the UI updates immediately after the test runs.
Starter Code
Android Kotlin
package com.example.apptester

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val runTestButton: Button = findViewById(R.id.runTestButton)
        val testResultText: TextView = findViewById(R.id.testResultText)

        // TODO: Add click listener to runTestButton
        // TODO: Run test and update testResultText
    }

    // TODO: Add a simple test function that returns Boolean
}
Task 1
Task 2
Task 3
Task 4
Solution
Android Kotlin
package com.example.apptester

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val runTestButton: Button = findViewById(R.id.runTestButton)
        val testResultText: TextView = findViewById(R.id.testResultText)

        runTestButton.setOnClickListener {
            val testPassed = runSimpleTest()
            if (testPassed) {
                testResultText.text = "Test Passed"
            } else {
                testResultText.text = "Test Failed"
            }
        }
    }

    private fun runSimpleTest(): Boolean {
        // Simulate a test that checks if 2 + 2 equals 4
        return (2 + 2 == 4)
    }
}

This app screen has a button labeled 'Run Test'. When the user taps it, the app runs a simple test function that checks if 2 + 2 equals 4. This simulates a basic reliability check. The result is shown immediately below the button as 'Test Passed' or 'Test Failed'. This example shows how testing can confirm that parts of the app work as expected, which helps ensure the app is reliable and trustworthy.

Final Result
Completed Screen
-------------------------
|  App Reliability Tester |
|-----------------------|
| [Run Test]            |
|                       |
| Test Result:          |
| Test Passed           |
|                       |
-------------------------
User taps the 'Run Test' button.
The app runs the simple test function.
The text below updates to 'Test Passed' immediately.
Stretch Goal
Add a second test that sometimes fails to show how testing catches errors.
💡 Hint
Create another function that returns false randomly and update the UI to show which test passed or failed.