Challenge - 5 Problems
Property Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this property-based test snippet?
Consider this Kotlin code using the property-based testing library Kotest. What will be printed when the test runs?
Kotlin
import io.kotest.property.checkAll import io.kotest.property.arbitrary.int fun main() { checkAll<Int> { a -> println(a * 2 >= a) } }
Attempts:
2 left
💡 Hint
Consider negative integers: for a = -1, is -2 >= -1?
✗ Incorrect
The property 'a * 2 >= a' is true for non-negative integers but false for negative ones (e.g., -1 * 2 = -2 which is not >= -1). Therefore, 'false' will be printed for some generated integers.
🧠 Conceptual
intermediate1:30remaining
What is the main advantage of property-based testing?
Choose the best description of the main advantage of property-based testing compared to example-based testing.
Attempts:
2 left
💡 Hint
Think about how property-based testing generates inputs.
✗ Incorrect
Property-based testing automatically generates many inputs to test properties, helping find edge cases that example-based tests might miss.
🔧 Debug
advanced2:30remaining
Why does this property-based test fail with a false negative?
This Kotlin property-based test is supposed to check if reversing a list twice returns the original list. Why might it fail unexpectedly?
Kotlin
import io.kotest.property.checkAll fun main() { checkAll<List<Int>> { list -> assert(list.reversed().reversed() == list) } }
Attempts:
2 left
💡 Hint
Consider how property-based testing generates inputs for generic types.
✗ Incorrect
Kotest requires specifying an arbitrary generator for generic types like List. Without it, the test cannot generate inputs and fails.
📝 Syntax
advanced2:00remaining
Which option correctly defines a property test for reversing a string twice?
Select the Kotlin code snippet that correctly tests if reversing a string twice returns the original string using Kotest property testing.
Attempts:
2 left
💡 Hint
Consider the best assertion function to use in Kotlin tests.
✗ Incorrect
Option A uses assertEquals which is the proper way to compare expected and actual values in tests. Options A and B use assert which is less descriptive, and D tests a different property.
🚀 Application
expert1:30remaining
How many test cases will be generated by this property test?
Given this Kotlin property-based test code, how many test cases will Kotest generate by default?
Kotlin
import io.kotest.property.checkAll fun main() { checkAll<Int> { a -> println(a) } }
Attempts:
2 left
💡 Hint
Check Kotest default number of generated cases for checkAll.
✗ Incorrect
Kotest's checkAll function generates 100 test cases by default unless configured otherwise.