← Back to Kotlin Course | Chapter 13: Error Handling & Testing | Lesson 5 of 6

Kotest परिचय

Kotest एक और testing helper है, specifically Kotlin को ध्यान में रखकर बनाया गया, checks लिखने का एक ज़्यादा expressive, story-जैसा तरीका प्रदान करते हुए।
Syntax
kotlin
class SpecName : StringSpec({
    "test description" {
        actual shouldBe expected
    }
    "throws on bad input" {
        shouldThrow<ExceptionType> { riskyCall() }
    }
})

A Basic Kotest StringSpec Test

Kotest की StringSpec style आपको एक test को एक साधारण string description और उसके बाद एक lambda body के रूप में लिखने देती है, लगभग expected behavior describe करने वाले एक वाक्य जैसा पढ़ते हुए।

Warning: यह उदाहरण एक third-party testing/mocking library उपयोग करता है जो साधारण Kotlin standard library का हिस्सा नहीं है और इसे एक project dependency के रूप में जोड़ना चाहिए (जैसे Gradle के ज़रिए) -- यह बिना किसी dependency वाले plain kotlinc sandbox में नहीं चल सकता।

उदाहरण: A Basic Kotest StringSpec Test

markup
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe

class CalculatorSpec : StringSpec({
    "adding two numbers returns their sum" {
        (2 + 3) shouldBe 5
    }
})

Using Kotest Matchers

Kotest shouldBe, shouldContain, और shouldThrow जैसे expressive matcher functions प्रदान करता है, जो traditional assertEquals-style calls से ज़्यादा natural language जैसा पढ़े जाते हैं।

Warning: यह उदाहरण एक third-party testing/mocking library उपयोग करता है जो साधारण Kotlin standard library का हिस्सा नहीं है और इसे एक project dependency के रूप में जोड़ना चाहिए (जैसे Gradle के ज़रिए) -- यह बिना किसी dependency वाले plain kotlinc sandbox में नहीं चल सकता।

उदाहरण: Using Kotest Matchers

markup
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.collections.shouldContain
import io.kotest.matchers.shouldBe

class ListSpec : StringSpec({
    "a list of fruits should contain banana" {
        val fruits = listOf("apple", "banana", "cherry")
        fruits shouldContain "banana"
        fruits.size shouldBe 3
    }
})

Testing for Exceptions

Kotest का shouldThrow<ExceptionType> { } matcher जांचता है कि code का कोई block एक specific exception type फेंकता है, न फेंकने पर test को स्पष्ट रूप से fail करते हुए।

Warning: यह उदाहरण एक third-party testing/mocking library उपयोग करता है जो साधारण Kotlin standard library का हिस्सा नहीं है और इसे एक project dependency के रूप में जोड़ना चाहिए (जैसे Gradle के ज़रिए) -- यह बिना किसी dependency वाले plain kotlinc sandbox में नहीं चल सकता।

उदाहरण: Testing for Exceptions

markup
import io.kotest.core.spec.style.StringSpec
import io.kotest.assertions.throwables.shouldThrow

class ValidationSpec : StringSpec({
    "negative age should throw an exception" {
        shouldThrow<IllegalArgumentException> {
            require(-1 >= 0) { "Age cannot be negative" }
        }
    }
})

Kotest's Multiple Testing Styles

StringSpec से आगे, Kotest tests को structure करने की अलग पसंद के लिए FunSpec और BehaviorSpec जैसी दूसरी styles प्रदान करता है, सभी को वही underlying Kotest Gradle dependency और test engine setup चाहिए।

Warning: यह उदाहरण एक third-party testing/mocking library उपयोग करता है जो साधारण Kotlin standard library का हिस्सा नहीं है और इसे एक project dependency के रूप में जोड़ना चाहिए (जैसे Gradle के ज़रिए) -- यह बिना किसी dependency वाले plain kotlinc sandbox में नहीं चल सकता।

उदाहरण: Kotest's Multiple Testing Styles

markup
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class MathSpec : FunSpec({
    test("multiplying two numbers returns their product") {
        (4 * 5) shouldBe 20
    }
})
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि Kotest बिना अपने Gradle dependency और test engine configuration के उपयोग होने वाला एक drop-in replacement है।
  2. किसी project में बिना किसी साफ़ convention के Kotest की विभिन्न testing styles (StringSpec, FunSpec, BehaviorSpec) को असंगत रूप से मिलाना।
  3. यह भूल जाना कि Kotest का matcher syntax (shouldBe, shouldContain) इसकी अपनी assertion library से आता है, JUnit के assertEquals style से अलग।
चैप्टर सारांश
  • Kotest एक Kotlin-specific testing framework है जो कई testing styles और expressive matchers प्रदान करता है।
  • StringSpec Kotest की सबसे सरल styles में से एक है, जहां हर test एक string description है जिसके बाद एक lambda है।
  • shouldBe और shouldContain जैसे Kotest matchers assertions लिखने का एक readable, fluent तरीका प्रदान करते हैं।
  • JUnit की तरह, Kotest को अपनी Gradle/Maven dependencies चाहिए और यह सिर्फ एक plain .kt file से नहीं चल सकता।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.