Test Structure
The project follows the standard Android testing structure:Unit Tests
Location:app/src/test/java/com/teamtech/techsales/
- Run on the local JVM (fast)
- No Android framework dependencies required
- Used for testing business logic, utilities, and data models
Instrumented Tests
Location:app/src/androidTest/java/com/teamtech/techsales/
- Run on Android devices or emulators
- Have access to Android framework APIs
- Used for UI testing, integration testing, and context-dependent code
Running Tests with Gradle
Run All Unit Tests
test/ directory.
Run Debug Unit Tests
Run Release Unit Tests
Run Instrumented Tests
Instrumented tests require a connected device or running emulator.
Run All Tests
Execute both unit and instrumented tests:Running Tests from Android Studio
Navigate to Test File
Open a test file in the editor:
ExampleUnitTest.javafor unit testsExampleInstrumentedTest.javafor instrumented tests
Run Test
Right-click on:
- The test class name (runs all tests in the class)
- A specific test method (runs that test only)
- The test package (runs all tests in the package)
Example Unit Test
Fromapp/src/test/java/com/teamtech/techsales/ExampleUnitTest.java:
- Uses JUnit 4 framework
- Runs on local JVM (fast execution)
- No Android dependencies
- Uses standard JUnit assertions
Example Instrumented Test
Fromapp/src/androidTest/java/com/teamtech/techsales/ExampleInstrumentedTest.java:
- Uses AndroidJUnit4 runner
- Requires Android device/emulator
- Has access to Android Context and framework APIs
- Verifies the app’s package name is correct
Test Dependencies
Test dependencies are configured inapp/build.gradle.kts:
Dependency Scopes
- testImplementation - Dependencies for unit tests only (JVM)
- androidTestImplementation - Dependencies for instrumented tests only (Android)
- implementation - Dependencies available to both app and tests
Key Testing Libraries
- JUnit - Standard Java testing framework for unit tests
- AndroidX Test JUnit - JUnit extensions for Android instrumented tests
- Espresso - UI testing framework for Android
Test Configuration
The test instrumentation runner is configured inapp/build.gradle.kts:
- Running instrumented tests on devices
- Access to Android framework APIs
- Integration with Android Test Orchestrator
Test Reports
After running tests, Gradle generates HTML reports:Unit Test Reports
- Test results summary
- Pass/fail status for each test
- Execution time
- Failure details and stack traces
Instrumented Test Reports
Generate Reports
Running Specific Tests
Run Specific Test Class
Run Specific Test Method
Run Tests Matching Pattern
Continuous Testing
Run tests continuously during development:Code Coverage
Enable code coverage to measure test effectiveness:JaCoCo coverage reporting requires additional configuration in
build.gradle.kts. This is not currently configured in the project.Best Practices
Unit Tests
- Test business logic and data transformations
- Keep tests fast and isolated
- Mock Android framework dependencies
- Aim for high code coverage
Instrumented Tests
- Test UI interactions and user flows
- Verify integration with Android framework
- Test database operations and file I/O
- Use test data that’s independent from production
General Guidelines
- Write tests before fixing bugs (test-driven debugging)
- Keep tests simple and focused on one behavior
- Use descriptive test method names
- Run tests before committing code
Troubleshooting
Unit Tests Fail to Run
Instrumented Tests Can’t Find Device
Test Dependencies Not Resolved
Tests Pass Locally But Fail in CI
- Check Android SDK versions match
- Verify emulator configuration
- Review test timeouts and flakiness
Next Steps
Building
Build the app for release
Project Structure
Understand where to add new tests