You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 (and src/androidTest/kotlin for 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.gradle file—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 kotlin directory under test → Mark Directory as → Test Sources Root. Repeat for androidTest/kotlin as 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 dependencies block 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, and it blocks.
    • 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.
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:19:34