A small library for generating UNIT tests for the Kotlin language. Save time with the code generator by focusing more on truly important matters. The library covers common scenarios and generates test cases based on your code. All you need to do is provide the expected value.
- Default case with return value
- Default case for 'Unit' value
- Nullable return types case
- Primitives test case
- Boolean true/false cases
Add dependency:
implementation("io.github.divinenickname.kotlin.utgen:utgen-core:1.3.1")
After that you can start generating test using two simple methods:
UnitTestGenerator()
.generateAndSave("path/to/kotlin/Class.kt")
OR
UnitTestGenerator()
.generate("path/to/kotlin/Class.kt")
.writeTo(file or path)
Your source class is:
package com.example.demo1
class MyTestClass {
fun nonVoid(): ObjClass {
return ObjClass()
}
}
Library generates code:
package com.example.demo1
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
internal class MyTestClassTest {
@Test
public fun nonVoidTest() {
val obj = MyTestClass()
val expected = ObjClass()
val actual = obj.nonVoid()
Assertions.assertEquals(expected, actual)
}
}
See more at: EXAMPLES.md
- ANTLR Kotlin grammar
- Kotlinpoet for codegeneration
- junit-jupiter-api
hi!