What is Property-Based Testing?

You can use the term Generative Testing for Property-Based testing PBT. You don’t provide explicit model inputs with desired outputs, likewise with unit tests. As a substitute, you can characterize properties regarding code and utilize a generative-testing engine (for example, QuickCheck) to create randomized inputs to guarantee the described properties are accurate.

Property Based Testing

Why do we need property-based testing?

You can use fewer lines of code in property-based tests as unit tests usually. Still, you can cover almost all boundary cases with this limited code because these tests always use different sets of inputs during testing, which is impossible in unit tests.

Because these tests use a lot of inputs at runtime, it gives a better understanding of the function under test. As we know, 5 * 3 = 15, but we are not confident that we correctly implemented the function for multiplication with this single example.

It would be best if you had a better understanding of your code functionality to achieve your goal and fulfill all requirements to find a problem’s solution.

PBT is a replacement for unit tests?

While it is enticing to say high-quality property-based tests could entirely replace unit tests, still unit tests have their position in the software development life cycle. Testing that uses examples is suitable for the initial stages of Test Driven Development. It helps to evaluate your development efforts on each step.

You can verify the working of the square-root function in your code with example-based testing. e.g. sqrt(4)= 2

Example-based tests only verify the final results of the functions under test with specific inputs, and they can’t find some internal error in functionality. On the other side, PBT always generates new inputs data to test function that helps software developers to find out issues. However, example-based tests help to enhance property-based test suites.

Some common properties

You can find a correlation between mathematical functions and software functions that would be helpful to apply mathematical properties to some desired functionality. You can find a few simple examples below:

  • Associative Property x + (y + z) = (x + y) + z

    string1.append( string2.append(string3)) = string1.append(string2).append(string3)

  • Commutative Property x + y = y + x

    max(val1, val2) = max(val2, val1)

  • Identity Property

    val1 + 0 = val1

    val1 * 1 = val1

    data1.append([]) = data1

  • Zero Property

    value * 0 = 0

    intersect(nonEmptySet, emptySet) = emptySet

ScalaCheck

You can find a simple example regarding PBT from the ScalaTest website as given below:

// ScalaCheck is a PBT tool
import org.scalatest.junit.JUnitSuite
import org.scalatest.prop.Checkers
import org.scalacheck.Arbitrary._
import org.scalacheck.Prop._

class MySuite extends JUnitSuite with Checkers {
  @Test
  def testCombinedLists() {
    check((list1: List[Int], list2: List[Int]) => list1.size + list2.size == (list1 ::: list2).size)
  }
}

ScalaCheck is a property-based testing tool that uses Checker and ScalaTest. It works as other tools of PBT, uses existing libraries (e.g., assumptions, assertions, and theories) to test code functionality.

In the above example, You can confirm that the length of the two combined lists is equal to the sum of the length of the two input lists.

Quickcheck

One more example regarding JUnit Quickcheck for Java is given below:

import com.pholser.junit.quickcheck.Property;
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;

    @RunWith(JUnitQuickcheck.class)
    public class StringProps {
        @Property public void concatenatedStringsLength(String str1, String str2) {
            assertEquals(str1.length() + str2.length(), (str1 + str2).length());
        }
    }
}    

The above example verifies that two concatenated strings length equals the sum of the length of two input strings.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved