Kotlin Android测试问题求助:目录未生成、报错及框架选型
Hey there! Let's break down and solve your Kotlin testing problems step by step:
1. Fixing the Missing
src/test/kotlin Directory The src/test/kotlin directory doesn’t auto-generate by default in older Android Studio setups, but we can get this sorted easily:
- Manually create the directory: Head to your project root, then build out the path
src/test/kotlin(andsrc/androidTest/kotlinfor instrumented tests) if they don’t exist yet. - Verify Kotlin plugin setup: Make sure you’ve applied the Kotlin Android plugin at the top of your
build.gradlefile—this is critical for Android Studio to recognize Kotlin test directories:apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' - Sync and mark as test sources: Click the "Sync Now" button in Android Studio. After syncing, right-click the
kotlindirectory undertest→ Mark Directory as → Test Sources Root. Repeat forandroidTest/kotlinas Android Test Sources Root.
2. Resolving the "No Class Found Error" in androidTest
This error almost always ties to missing instrumented test dependencies or misconfigured test runners. Here’s what to check:
- Add required androidTest dependencies: Your current
dependenciesblock only has unit test (testCompile) dependencies, but instrumented tests need their own set. Add these lines:androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' androidTestImplementation 'com.android.support:multidex-instrumentation:1.0.2' - Update the test instrumentation runner: Since you have
multiDexEnabled true, switch your runner to support multi-dex in tests:testInstrumentationRunner "android.support.test.runner.MultiDexTestRunner" - Clean and rebuild: Go to Build → Clean Project, then Build → Rebuild Project. Cached build files often cause class-not-found issues.
- Consider updating dependencies: Your Firebase and support library versions are quite old (11.8.0, 27.0.2). Updating to more recent compatible versions can avoid hidden compatibility conflicts.
3. Kotlintest vs Spek: Which is Better for You?
Both are solid Kotlin testing frameworks, but they play to different strengths:
- Spek:
- Built exclusively for BDD (Behavior-Driven Development), with a focus on readable, natural-language test structures using
describe,context, anditblocks. - Tight integration with the JUnit 5 platform, making it easy to run alongside other JUnit tests.
- Ideal if you want a lightweight, opinionated framework for writing clean, scenario-focused tests.
- Built exclusively for BDD (Behavior-Driven Development), with a focus on readable, natural-language test structures using
- Kotlintest (now renamed to Kotest):
- Supports multiple testing styles (BDD, Fun Spec, Should Spec, etc.), so you can pick the style that fits your team’s workflow.
- Comes with a rich set of idiomatic Kotlin assertions (like
shouldBe,shouldContain,shouldThrow) that feel more natural than traditional JUnit assertions. - Offers advanced features like data-driven testing, test tagging, and parallel test execution.
- Has a larger feature set overall, making it a great choice if you need flexibility and powerful testing tools.
Short answer: If you prefer a pure BDD approach with minimal setup, go with Spek. If you want versatility and robust assertion capabilities, Kotest (Kotlintest) is the way to go.
内容的提问来源于stack exchange,提问作者Anuj Mody
相关产品推荐
相关产品推荐

