JUnit के Basics
In this page:
class TestClassName {
@BeforeEach
fun setUp() { /* prepare */ }
@Test
fun testName() {
assertEquals(expected, actual)
}
}
Writing a Basic JUnit Test
एक JUnit test @Test से annotated एक function है जो यह जांचने के लिए कि असली behavior एक expected value से मेल खाता है एक assertion function, जैसे assertEquals, call करता है।
adding two numbers returns their sum() `, descriptive JUnit test names के लिए एक आम Kotlin convention है।उदाहरण: Writing a Basic JUnit Test
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.assertEquals
class CalculatorTest {
@Test
fun `adding two numbers returns their sum`() {
val result = 2 + 3
assertEquals(5, result)
}
}
Login to try C/C++/Java/PHP code in the editor
Common Assertions
JUnit assertEquals, assertTrue, assertFalse, और assertThrows जैसे assertion functions प्रदान करता है, हर एक उम्मीद पूरी न होने पर test को एक साफ़ message के साथ fail करता है।
उदाहरण: Common Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*
class ValidationTest {
@Test
fun `age validation rejects negative numbers`() {
assertThrows(IllegalArgumentException::class.java) {
require(-1 >= 0) { "Age cannot be negative" }
}
}
}
Login to try C/C++/Java/PHP code in the editor
Organizing Tests with setUp
@BeforeEach से annotated एक method किसी class के हर test से पहले चलता है, shared fixtures setup करने के लिए उपयोगी ताकि हर test एक ज्ञात, साफ़ state से शुरू हो।
उदाहरण: Organizing Tests with setUp
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.assertEquals
class StackTest {
private lateinit var stack: MutableList<Int>
@BeforeEach
fun setUp() {
stack = mutableListOf()
}
@Test
fun `pushing adds an element`() {
stack.add(1)
assertEquals(1, stack.size)
}
}
Login to try C/C++/Java/PHP code in the editor
Running Tests with Gradle
किसी Gradle-based Kotlin project में, JUnit tests ./gradlew test से execute किए जाते हैं, जो test sources को compile करता है और report करता है कि कौन से tests pass या fail हुए।
उदाहरण: Running Tests with Gradle
./gradlew test
⚠️ Run this in your own terminal or Node.js environment.
- यह भूल जाना कि JUnit एक third-party test framework है जिसे एक build tool (Gradle/Maven) dependency चाहिए; यह सिर्फ
kotlincके साथ किसी साधारण.ktfile से नहीं चल सकता। - ऐसे test methods लिखना जो किसी साफ़, descriptive नाम से शुरू न हों, test runner के output से अकेले failures समझना मुश्किल बनाते हुए।
- अलग, केंद्रित tests में बांटने के बजाय एक single test function में कई असंबंधित behaviors test करना।
- JUnit Kotlin सहित JVM languages के लिए एक व्यापक रूप से उपयोग होने वाला testing framework है, और उपयोग करने के लिए एक Gradle/Maven dependency चाहिए।
- एक test function
@Testसे चिह्नित होता है और आमतौर परassertEquals,assertTrue, या similar assertion functions उपयोग करता है। - Test classes और functions आमतौर पर किसी project की
src/test/kotlindirectory के तहत रखे जाते हैं, main source code से अलग। ./gradlew testचलाना किसी Gradle-based Kotlin project में सभी JUnit tests execute करता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: