0Pricing
Kotlin Multiplatform Academy · บทเรียน

การจัดวาง Test: commonTest และส่วนที่เกี่ยวข้อง

ชุดซอร์สสำหรับ Test แบบ shared และเฉพาะแพลตฟอร์ม

การจัดวาง Test: commonTest และส่วนที่เกี่ยวข้อง เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Kotlin Multiplatform Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Tests Get Source Sets Too

Just like your main code, tests live in their own source sets, mirroring commonMain, androidMain and iosMain.

Meet commonTest

commonTest holds tests that run on every platform. Write a test once and verify your shared logic everywhere. ✅

The kotlin.test Library

Common tests use the multiplatform kotlin.test library, which gives you assertions that work on all targets.

commonTest.dependencies {
    implementation(kotlin("test"))
}

A First Shared Test

A common test reads like any unit test, using a portable assertEquals to check shared behavior.

@Test
fun greetsName() {
    assertEquals("Hi, Sam", greet("Sam"))
}

androidUnitTest

Need a JVM-only test? Put it in androidUnitTest, where you can use JUnit and Android-specific helpers.

iosTest

iOS-specific tests live in iosTest. They run on the simulator using Kotlin/Native's test runner.

Common Tests Run On All

A test in commonTest executes on both the Android and iOS test tasks, catching platform differences early.

Run the Android Suite

From the terminal, a single Gradle task runs the JVM-side tests, including the common ones.

./gradlew :shared:testDebugUnitTest

Run the iOS Suite

Another task runs the same shared tests on the iOS simulator target, confirming parity.

./gradlew :shared:iosSimulatorArm64Test

Why Test in Common

Testing shared logic in commonTest means one suite guards both apps, so a fix is proven on every platform at once.

Keep Tests Where Code Is

Mirror your main sets: common logic gets common tests, platform code gets platform tests. The structure stays predictable.

Quick Check

Pick the right test home.

Recap

Tests mirror the source sets: commonTest for shared, androidUnitTest and iosTest for platform-specific checks.

คำถามที่พบบ่อย

บทเรียน “การจัดวาง Test: commonTest และส่วนที่เกี่ยวข้อง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดวาง Test: commonTest และส่วนที่เกี่ยวข้อง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดวาง Test: commonTest และส่วนที่เกี่ยวข้อง”

ชุดซอร์สสำหรับ Test แบบ shared และเฉพาะแพลตฟอร์ม คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดวาง Test: commonTest และส่วนที่เกี่ยวข้อง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม

ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. โมดูล shared เทียบกับโมดูลแอป
  2. commonMain, androidMain และ iosMain
  3. การอ่าน build.gradle.kts ของ shared
  4. การจัดวาง Test: commonTest และส่วนที่เกี่ยวข้อง
← กลับไปที่ Kotlin Multiplatform Academy