Assess your understanding of Android unit tests and instrumentation tests, including concepts, differences, and best practices. This quiz covers key aspects of Android application testing, helping you reinforce your skills in reliable and efficient code validation.
Which of the following best describes the main goal of writing unit tests in an Android project?
Explanation: Unit tests are designed to ensure that specific methods or classes work as expected, without dependencies on other parts of the code or system resources. They do not focus on app performance or screen rendering. While automation is a key benefit, unit tests do not automate UI navigation; that task is handled by other forms of testing, such as instrumentation or UI tests.
In Android, instrumentation tests are primarily used to test which kind of code?
Explanation: Instrumentation tests run on a device or emulator, allowing them to interact with system components and hardware, such as sensors or UI. Simple mathematics or plain Java logic can be tested with unit tests, and static code analysis is unrelated to instrumentation or unit tests. Instrumentation tests go beyond business logic to also check device interactions.
Where should instrumentation test classes be placed in an Android project directory?
Explanation: The src/androidTest/java directory is specifically designated for instrumentation tests, allowing them to run on a device or emulator. src/unitTest/java is not a standard Android directory, and src/main/java is for production code, not for tests. The src/testFixtures/java folder is used for test utilities and not for the tests themselves.
Which environment do Android unit tests usually run in by default?
Explanation: Unit tests are typically executed with a standard Java Virtual Machine on the local computer, independent of hardware or emulators. They don’t run in the app store, nor are they executed in live applications delivered to users. Connected hardware devices are needed for instrumentation tests, not unit tests.
Suppose you want to check that a button press changes the text on a screen. Which type of test should you write?
Explanation: Instrumentation tests can simulate user interactions such as button presses and verify changes in the user interface by running on an actual device or emulator. Static analysis is used for code structure, not runtime behavior. Unit tests do not interact with UI as they run in isolation, and 'UI rendering test' is not a widely used Android testing classification.
Which annotation should be placed above a test function to indicate it is a unit test in an Android project?
Explanation: The @Test annotation marks test methods for execution in both standard unit and instrumentation test frameworks. @Instrumentation and @RunUnit are not standard annotations. @UITest is not recognized by default Android testing tools and does not mark a method as a test.
Why are mocks or stubs commonly used in Android unit tests?
Explanation: Mocks and stubs simulate external dependencies, allowing unit tests to run quickly and in isolation. They are not used for integration testing with real system services, nor do they affect code obfuscation or UI transition performance. This ensures tests remain fast and focused on logic, not implementation details.
Generally, which type of test executes faster in Android development?
Explanation: Unit tests usually run much faster because they execute on the developer’s local machine and do not depend on devices, emulators, or UI rendering. Instrumentation tests are slower as they operate in a more complex environment. Manual and end-to-end tests typically take even longer due to human involvement and full integration paths.
Which benefit do Android tests most directly provide when new features are added to an app?
Explanation: Automated tests can identify regression bugs, alerting developers when new features cause old functionality to break. Tests do not generate documentation or guarantee no errors in production, nor do they impact network speeds. Regularly running tests helps maintain code stability as the project evolves.
What is typically used in both unit and instrumentation tests to check if the code outcome matches expectations?
Explanation: Assertions are methods used within tests to compare actual results with expected values, verifying correct behavior. Specifications are design documents, not test checks. Listeners observe test events but do not verify outcomes. Annotations describe or mark code but don’t compare values during test execution.