Kotest Property Extension¶
About¶
kotlin-faker-kotest-property artifact extends Arb generators and provides an easy way to use kotlin-faker functionality with kotest property testing.
Usage¶
Installation¶
kotlin-faker-kotest-property extension needs to be added in your build.gradle.kts alongside core kotlin-faker dependency:
- ① add the core
kotlin-fakerdependency to the test classpath - ② add the
testImplementationdependency for thekotlin-faker-kotest-propertyextension
Arb Extensions¶
To get an Arb instance of any fake generator, use Arb.of extension function:
Info
For any additional fakers that you want to generate Arbs for, e.g. BooksFaker or EduFaker, make sure to add the corresponding dependency to testImplementation:
This can then be used with standard Kotest property testing functionality, just like the built-in Arbs, e.g. with property test functions like forAll:
class KotestPropertyArbsTest : DescribeSpec({
describe("Custom kotlin-faker Arbs") {
it("should generate quotes from the bible") {
val b = BooksFaker()
forAll(Arb.of(b.bible::quote)) { q: String ->
q.isNotBlank()
}
}
it("should generate addresses") {
val f = Faker()
forAll(Arb.of(f.address::city)) { c: String ->
c.isNotBlank()
}
forAll(Arb.of(f.address::city), Arb.of(f.address::streetName)) { city: String, street: String ->
city.isNotBlank()
street.isNotBlank()
}
}
}
})
Random Class Instance ARB¶
It is possible to generate Random Class Instances in a similar way:
it("should generate person with address") {
val f = Faker()
f.randomClass.configure {
namedParameterGenerator("name") { f.name.name() }
namedParameterGenerator("age") { f.random.nextInt(20, 30) }
namedParameterGenerator("city") { f.address.city() }
namedParameterGenerator("streetName") { f.address.streetName() }
namedParameterGenerator("streetAddress") { f.address.streetAddress() }
}
forAll(Arb.of(f.randomClass::randomClassInstance), Arb.of(f.randomClass::randomClassInstance)) { p: Person, a: Address ->
p.name.isNotBlank()
p.age in 20..30
a.city.isNotBlank()
a.streetName.isNotBlank()
a.streetAddress.isNotBlank()
}
}