0
votes

Recently, I'm quite excited when reading about the idea of property based testing.

But I have 1 question that I still cannot find the answer anywhere:

How can property based testing ensures that it will test the corner cases every time?

To be more specific, let say I'm using ScalaCheck to test my division function:

def divide(a: Int, b: Int): Int

As the test cases will be generated randomly, how can I be sure that ScalaCheck will check the case where b = 0 every time?

2

2 Answers

2
votes

Special cases are often properties of their own. If you try to write a single case that covers b = any integer, then all your properties will end up in one big and complicated test. But you can split the parameter space into multiple chunks e.g.:

  • b = positive integer
  • b = negative integer
  • b = 0

And check them separately.

2
votes

Some PBT tools will always inject corner cases before generating random ones.

For example, jqwik (for Java) would try 0, 1, -1, Integer.MIN…VALUE and Integer.MAX…VALUE before any random values.