Kotest परिचय
In this page:
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 करने वाले एक वाक्य जैसा पढ़ते हुए।
उदाहरण: A Basic Kotest StringSpec Test
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
}
})
Login to try C/C++/Java/PHP code in the editor
Using Kotest Matchers
Kotest shouldBe, shouldContain, और shouldThrow जैसे expressive matcher functions प्रदान करता है, जो traditional assertEquals-style calls से ज़्यादा natural language जैसा पढ़े जाते हैं।
उदाहरण: Using Kotest Matchers
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
}
})
Login to try C/C++/Java/PHP code in the editor
Testing for Exceptions
Kotest का shouldThrow<ExceptionType> { } matcher जांचता है कि code का कोई block एक specific exception type फेंकता है, न फेंकने पर test को स्पष्ट रूप से fail करते हुए।
उदाहरण: Testing for Exceptions
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" }
}
}
})
Login to try C/C++/Java/PHP code in the editor
Kotest's Multiple Testing Styles
StringSpec से आगे, Kotest tests को structure करने की अलग पसंद के लिए FunSpec और BehaviorSpec जैसी दूसरी styles प्रदान करता है, सभी को वही underlying Kotest Gradle dependency और test engine setup चाहिए।
उदाहरण: Kotest's Multiple Testing Styles
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
}
})
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि Kotest बिना अपने Gradle dependency और test engine configuration के उपयोग होने वाला एक drop-in replacement है।
- किसी project में बिना किसी साफ़ convention के Kotest की विभिन्न testing styles (StringSpec, FunSpec, BehaviorSpec) को असंगत रूप से मिलाना।
- यह भूल जाना कि Kotest का matcher syntax (
shouldBe,shouldContain) इसकी अपनी assertion library से आता है, JUnit केassertEqualsstyle से अलग।
- Kotest एक Kotlin-specific testing framework है जो कई testing styles और expressive matchers प्रदान करता है।
StringSpecKotest की सबसे सरल styles में से एक है, जहां हर test एक string description है जिसके बाद एक lambda है।shouldBeऔरshouldContainजैसे Kotest matchers assertions लिखने का एक readable, fluent तरीका प्रदान करते हैं।- JUnit की तरह, Kotest को अपनी Gradle/Maven dependencies चाहिए और यह सिर्फ एक plain
.ktfile से नहीं चल सकता।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: