When working on Android development using Kotlin, writing unit tests is essential to ensure code reliability and prevent bugs before your app hits production. However, many developers encounter a frustrating issue: unit tests not running in Android Studio, especially when using Kotlin.
If you’re facing this problem, don’t worry. This guide walks you through the common causes and solutions for unit tests that fail to run or appear greyed out in Android Studio. By the end, you’ll be equipped with working examples and project configurations to resolve the issue completely.
Common Symptoms
- The test class is not detected by Android Studio.
- The “Run” or “Debug” icons don’t appear next to the test functions.
- Running the test manually results in
No tests found
. - Android Studio shows “Test events were not received”.
Typical Causes
Here are the main reasons your Kotlin unit tests might not be running:
- Missing or misconfigured test dependencies
- Incorrect directory structure for test classes
- Missing @Test annotation or using JUnit4 vs JUnit5 improperly
- Gradle configuration issues
- Using AndroidJUnit4 runner for unit tests instead of instrumentation tests
Example Problem: Test Not Running
Let’s consider a simple function and test case:
1. Main Kotlin Function
// File: app/src/main/java/com/example/utils/Calculator.kt package com.example.utils class Calculator { fun add(a: Int, b: Int): Int = a + b }
2. Test Case (Not Running)
// File: app/src/test/java/com/example/utils/CalculatorTest.kt package com.example.utils import org.junit.Test import org.junit.Assert.assertEquals class CalculatorTest { @Test fun testAdd() { val calculator = Calculator() val result = calculator.add(2, 3) assertEquals(5, result) } }
Issue: When you try to run this test, Android Studio might not detect it or say “No tests found”.
Solution Steps
1. Check Directory Structure
Your unit test should be in:
app/src/test/java/com/example/utils/CalculatorTest.kt
❗ Don’t put unit tests under
androidTest
unless you’re writing instrumentation tests.
2. Apply the Correct Dependencies
Ensure your build.gradle
includes these dependencies for JUnit4 support:
// app/build.gradle dependencies { testImplementation 'junit:junit:4.13.2' }
If using JUnit5, you’ll need:
testImplementation("org.junit.jupiter:junit-jupiter:5.9.3") test { useJUnitPlatform() }
3. Use @Test Annotation Correctly
import org.junit.Test // Make sure it's this one!
Avoid importing incorrect annotations from other libraries or frameworks.
4. Set the Test Runner Correctly (Optional)
If you’re running instrumentation tests, use AndroidJUnit4
. For unit tests, you don’t need a test runner. If used improperly, the test will not run.
Wrong:
@RunWith(AndroidJUnit4::class)
Correct (for unit test):
// No @RunWith needed for standard unit tests
5. Rebuild and Invalidate Caches
If everything seems correct but tests still don’t run:
- Go to File > Invalidate Caches / Restart
- Then Clean Project and Rebuild
Working Example That Runs Successfully
// File: app/src/test/java/com/example/utils/CalculatorTest.kt package com.example.utils import org.junit.Test import org.junit.Assert.assertEquals class CalculatorTest { @Test fun testAddition() { val calculator = Calculator() assertEquals(5, calculator.add(2, 3)) } }
Run it: Right-click on the class or method > Run ‘CalculatorTest’
Bonus: Troubleshooting Table
Problem | Cause | Solution |
---|---|---|
Test class not found | Wrong folder or missing annotation | Use @Test and put file under src/test/java |
No tests found | JUnit5 used without platform | Add useJUnitPlatform() in build.gradle |
Test runs as instrumentation test | Using @RunWith(AndroidJUnit4::class) | Remove annotation for unit tests |
Build fails on test | Missing dependencies | Add correct testImplementation |
Final Tips
- Always use
src/test
for unit tests andsrc/androidTest
for UI/instrumentation tests. - Ensure that the Kotlin compiler and Android Gradle plugin versions are up-to-date.
- Consider switching to JUnit5 if you want more flexible parameterized tests or nested classes.
Summary
Unit tests are a vital part of Android app development. When tests don’t run in Kotlin projects inside Android Studio, it’s usually due to project structure issues or missing configurations. This article covered common causes, fixes, and working examples to help you run tests properly.
By resolving these issues, you ensure your codebase remains clean, maintainable, and free from critical bugs making you a more efficient and professional Android developer.