interface Price {
public boolean contains(Money money);
public Money min();
public Money max();
// ...
}
class SingleAmount implements Price { /* ... */ }
class ContinuousRangeOfAmounts implements Price { /* ... */ }
class SetOfAmounts implements Price { /* ... */ }
// given
Price single = new SingleAmount(new Money("2.00/EUR"));
Price range = new ContinuousRangeOfAmounts(
new Money("1.50/EUR"),
new Money("2.10/EUR")
);
SetOfAmounts set = new SetOfAmounts();
// when
set.add(single);
set.add(range);
// then
assertTrue(set.contains("1.50/EUR"));
assertTrue(set.contains("2.00/EUR"));
assertFalse(set.contains("2.11/EUR"));
// given
List<Price> prices = randomSequence(
SingleAmount.class,
ContinuousRangeOfAmounts.class
);
//when
SetOfAmounts set = new SetOfAmounts();
for (Price p: prices) {
set.add(p);
}
// then
for (Price p: prices) {
assertTrue(
set.contains(p),
"Price " + p " is not contained in the final set"
);
}
$this
->forAll(
Generator\set(Generator\elements(['country', 'price', ...]))
)
->then(function($fields) {
$response = $this->groupBy($fields);
$landed = 0;
foreach ($response['elements'] as $row) {
$landed += $row['landed'];
}
$this->assertEquals(
$totalExpectedLanded,
$landed
);
});
$this
->forAll(
Generator\choose(2000, 2020),
Generator\choose(0, 364),
Generator\choose(0, 364)
)
->then(function($year, $dayOfYear, $anotherDayOfYear) {
$day = UTCDateTime::fromZeroBasedDayOfYear(
$year, $dayOfYear);
$anotherDay = UTCDateTime::fromZeroBasedDayOfYear(
$year, $anotherDayOfYear);
$this->assertEquals(
abs($dayOfYear - $anotherDayOfYear) * 86400,
abs($day->differenceInSeconds($anotherDay)),
"Days of the year $year, ..."
);
});
$this
->forAll(
Generator\image(
Generator\choose(0, 1024),
Generator\choose(0, 768)
),
)
->then(function($inputImage) {
$this->assertImagesAreEqual(
$image->resize(0.5)->resize(0.5),
$image->resize(0.25)
);
});
C | theft |
C++ | CppQuickCheck |
Clojure | test.check |
Coq | QuickChick |
F# | FsCheck |
Haskell | Quickcheck |
JavaScript | jsverify |
PHP | Eris |
Python | Hypothesis |
Ruby | Rantly |
Rust | Quickcheck |
Scala | ScalaCheck |
Swift | Swiftcheck |
- A summary blog post
- Choosing properties for property-based testing
- A survey of Quickchecks
Images on how to find properties by scottw