Property based Testing for Domain Models

 — 1 minute read


Debasish Ghosh describes the benefits of property based testing compared to traditional xUnit style testing. He advocates to use libraries like ScalaCheck ((Similar libraries are available for many programming languages; cf. http://en.wikipedia.org/wiki/QuickCheck)) where only the property to satisfy by the production code is described and the library generates the test data to check the property with:

I don’t need to construct concrete data by hand (in fact this is what makes xUnit based frameworks a sophisticated manual testing system - it only automates the execution part of your testing).

Example:

[code]
property(“Enrichment of a trade should result in netvalue > 0”) {
forAll((a: Trade) =>
enrichTrade(a).netAmount.get should be > (BigDecimal(0)))
}
[/code]

This method shifts writing test from how to test to what to test.