Skip to content

Website and documentation for jqwik.net

Notifications You must be signed in to change notification settings

jqwik-team/jqwik.net

Repository files navigation

jqwik is pronounced like "jay quick" [ˈdʒeɪkwɪk].

The main purpose of jqwik is to bring Property-Based Testing (PBT) to the JVM. The library's focus is mainly on Java and Kotlin; Groovy works as well.

Property-Based Testing tries to combine the intuitiveness of Microtests with the effectiveness of randomized, generated test data. Originally driven by the common hype about functional programming, PBT has meanwhile been recognized as an important ingredient of any up-to-date testing approach.

Properties

A property is supposed to describe a generic invariant or post condition of your code, given some precondition. The testing library - jqwik - will then try to generate many value sets that fulfill the precondition hoping that one of the generated sets can falsify a wrong assumption.

The following property deals with a partial implementation for the (in)famous Fizz Buzz Kata:

  • Precondition: Consider numbers between 1 and 100 that are divisible by 3
  • Postcondition: The string returned by fizzBuzz() starts with Fizz
import java.util.*;
import java.util.stream.*;

import net.jqwik.api.*;

class FizzBuzzTests {
	@Property
	boolean every_third_element_starts_with_Fizz(@ForAll("divisibleBy3") int i) {
		return fizzBuzz().get(i - 1).startsWith("Fizz");
	}

	@Provide
	Arbitrary<Integer> divisibleBy3() {
		return Arbitraries.integers().between(1, 100).filter(i -> i % 3 == 0);
	}

	private List<String> fizzBuzz() {
		return IntStream.range(1, 100).mapToObj((int i) -> {
			boolean divBy3 = i % 3 == 0;
			boolean divBy5 = i % 5 == 0;

			return divBy3 && divBy5 ? "FizzBuzz"
				: divBy3 ? "Fizz"
				: divBy5 ? "Buzz"
				: String.valueOf(i);
		}).collect(Collectors.toList());
	}
}

By using a few annotations jqwik tries to make it as simple as possible for programmers to write and run Properties.

Where to go from here

About

Website and documentation for jqwik.net

Resources

Stars

Watchers

Forks