Best Kotest code snippet using io.kotest.property.PropertyContext.classifications
proptest.kt
Source:proptest.kt
...5import io.kotest.property.Gen6import io.kotest.property.PropTestConfig7import io.kotest.property.PropertyContext8import io.kotest.property.PropertyTesting9import io.kotest.property.classifications.outputClassifications10import io.kotest.property.seed.createRandom11suspend fun <A> proptest(12 genA: Gen<A>,13 config: PropTestConfig,14 property: suspend PropertyContext.(A) -> Unit15): PropertyContext {16 config.checkFailOnSeed()17 val constraints = config.constraints18 ?: config.iterations?.let { Constraints.iterations(it) }19 ?: Constraints.iterations(PropertyTesting.defaultIterationCount)20 val context = PropertyContext()21 val random = createRandom(config)22 when (genA) {23 is Arb -> {...
context.kt
Source:context.kt
...5 */6class PropertyContext {7 private var successes = 08 private var failures = 09 private val classifications = mutableMapOf<String, Int>()10 private val autoclassifications = mutableMapOf<String, MutableMap<String, Int>>()11 fun markSuccess() {12 successes++13 }14 fun markFailure() {15 failures++16 }17 fun successes() = successes18 fun failures() = failures19 fun attempts(): Int = successes + failures20 fun classifications(): Map<String, Int> = classifications.toMap()21 fun autoclassifications(): Map<String, Map<String, Int>> = autoclassifications.toMap()22 /**23 * Increase the count of [label].24 */25 fun classify(label: String) {26 val current = classifications.getOrElse(label) { 0 }27 classifications[label] = current + 128 }29 fun classify(input: Int, label: String) {30 val current = autoclassifications.getOrPut(input.toString()) { mutableMapOf() }31 val count = current[label] ?: 032 current[label] = count + 133 autoclassifications[input.toString()]34 }35 /**36 * Increase the count of [label] if [condition] is true.37 */38 fun classify(condition: Boolean, label: String) {39 if (condition) classify(label)40 }41 /**42 * Increase the count of [trueLabel] if [condition] is true, otherwise increases43 * the count of [falseLabel].44 */45 fun classify(condition: Boolean, trueLabel: String, falseLabel: String) {46 if (condition) {47 val current = classifications.getOrElse(trueLabel) { 0 }48 classifications[trueLabel] = current + 149 } else {50 val current = classifications.getOrElse(falseLabel) { 0 }51 classifications[falseLabel] = current + 152 }53 }54}...
coverage.kt
Source:coverage.kt
...15 *16 */17suspend fun checkCoverage(label: String, percentage: Double, f: suspend () -> PropertyContext): PropertyContext {18 val context = f()19 val labelled = context.classifications()[label] ?: 020 val attempts = context.attempts()21 val actual = (labelled.toDouble() / attempts.toDouble()) * 100.022 if (actual < percentage)23 fail("Property test required coverage of $percentage% for [$label] but was [${actual.toInt()}%]")24 return context25}26/**27 * Asserts more than one label coverage at once.28 *29 * Example, checks that we had at least 25% "even" and 25% "odd" iterations.30 *31 * checkCoverage("even" to 25.0, "odd" to 25.0) {32 * forAll(Arb.int()) { a ->33 * classify(a % 2 == 0, "even", "odd")34 * a + a == 2 * a35 * }36 * }37 */38suspend fun checkCoverage(vararg pairs: Pair<String, Double>, f: suspend () -> PropertyContext): PropertyContext {39 val context = f()40 val coverage = pairs.toMap()41 val attempts = context.attempts()42 context.classifications().forEach { (label, count) ->43 val actual = (count.toDouble() / attempts.toDouble()) * 100.044 val required = coverage[label] ?: 0.045 if (actual < required)46 fail("Property test required coverage of $required% for [$label] but was [${actual.toInt()}%]")47 }48 return context49}...
output.kt
Source:output.kt
1package io.kotest.property.classifications2import io.kotest.property.PropTestConfig3import io.kotest.property.PropertyContext4import io.kotest.property.PropertyResult5import io.kotest.property.PropertyTesting6fun PropertyContext.outputClassifications(inputs: Int, config: PropTestConfig, seed: Long) {7 val result =8 PropertyResult(List(inputs) { it.toString() }, seed, attempts(), successes(), failures(), autoclassifications())9 if (config.outputClassifications) config.labelsReporter.output(result)10}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!