kotlin.test ใน commonTest
เขียนการทดสอบหน่วยที่ไม่ขึ้นกับแพลตฟอร์มเพียงครั้งเดียว
kotlin.test ใน commonTest เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Kotlin Multiplatform Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Test Once, Trust Everywhere
Shared code deserves shared tests. With kotlin.test you write a single suite that proves your logic on every target. 🧪
Add the Test Dependency
First, pull kotlin.test into your common test source set so the assertions are available on all platforms.
commonTest.dependencies {
implementation(kotlin("test"))
}Where Common Tests Live
Your portable tests belong in commonTest, the source set that mirrors commonMain and compiles for every target.
The @Test Annotation
Mark any function with @Test and the multiplatform runner treats it as a test case to execute.
@Test
fun greetsByName() {
// assertions go here
}assertEquals
The everyday assertion is assertEquals: it compares an expected value to the actual result of your shared code.
assertEquals("Hi, Sam", greet("Sam"))assertTrue and assertFalse
For boolean checks reach for assertTrue or assertFalse, which fail when the condition is not what you expect.
assertTrue(price > 0)
assertFalse(items.isEmpty())Checking for Null
Use assertNull and assertNotNull to express that a value should, or should not, be absent.
assertNotNull(user.email)Asserting Exceptions
To prove that bad input fails, wrap the call in assertFailsWith and name the exception you expect.
assertFailsWith<IllegalArgumentException> {
parse("not-a-number")
}Setup with @BeforeTest
Need fresh state per test? A function marked @BeforeTest runs before each case to prepare it.
@BeforeTest
fun setup() {
calc = TaxCalculator()
}One Assertion, One Idea
Keep each test focused: a clear name plus a single core assertion makes failures instantly readable.
It Runs on Every Target
Because the test sits in commonTest, the same case runs under the Android and iOS test tasks, guarding both apps at once. ✅
Quick Check
Pick the right common assertion.
Recap
Add kotlin.test, place cases in commonTest, mark them @Test, and assert once to verify shared logic everywhere.
คำถามที่พบบ่อย
บทเรียน “kotlin.test ใน commonTest” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “kotlin.test ใน commonTest” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “kotlin.test ใน commonTest”
เขียนการทดสอบหน่วยที่ไม่ขึ้นกับแพลตฟอร์มเพียงครั้งเดียว คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “kotlin.test ใน commonTest” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม
ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- kotlin.test ใน commonTest
- ทดสอบฟังก์ชัน suspend และโฟลว์
- จำลองไคลเอนต์และคลังของ Ktor
- เรียกใช้การทดสอบบนเป้าหมาย Android และ iOS