Kotest Property Extension

TOC

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-faker dependency to the test classpath
  • ② add the testImplementation dependency for the kotlin-faker-kotest-property extension
dependencies {
    testImplementation("io.github.serpro69:kotlin-faker:$fakerVersion") // ①
    testImplementation("io.github.serpro69:kotlin-faker-kotest-property:$fakerExtVersion") // ②
}

back-to-toc


Arb Extensions

To get an Arb instance of any fake generator, use Arb.of extension function:

Arb.of(Faker().address::city)


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:

dependencies {
    testImplementation("io.github.serpro69:kotlin-faker-books:$fakerVersion")
    testImplementation("io.github.serpro69:kotlin-faker-edu:$fakerVersion")
}




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()
            }
        }
    }
})

back-to-toc


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()
    }
}

back-to-toc