kotlin.test في commonTest
اكتب اختبارات وحدات مستقلة عن المنصة مرة واحدة.
kotlin.test في commonTest درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «kotlin.test في commonTest»؟
اكتب اختبارات وحدات مستقلة عن المنصة مرة واحدة. تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «kotlin.test في commonTest»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- kotlin.test في commonTest
- اختبار الدوال المعلّقة وFlows
- محاكاة Ktor Client والمستودعات
- تشغيل الاختبارات على أهداف Android وiOS