This page of ScalaTest.org!is brought to you by: |
ScalaTest User Guide Getting started Selecting testing styles Defining base classes Writing your first test Using assertions Tagging your tests Running your tests Sharing fixtures Sharing tests Using matchers Testing with mock objects Property-based testing Using Selenium Other goodies Philosophy and design Migrating to 2.0 |
Property-based testingScalaTest supports property-based testing, where a property is a high-level specification of behavior that should hold for a range of data points. For example, a property might state that the size of a list returned from a method should always be greater than or equal to the size of the list passed to that method. This property should hold no matter what list is passed. The difference between a traditional test and a property is that tests traditionally verify behavior based on specific data points checked by the test. A test might pass three or four specific lists of different sizes to a method under test that takes a list, for example, and check the results are as expected. A property, by contrast, would describe at a high level the preconditions of the method under test and specify some aspect of the result that should hold no matter what valid list is passed.
In ScalaTest, properties are specified as functions and the data points used to check properties can be supplied by either tables
or generators. Generator-driven property checks are performed via integration with ScalaCheck.
To use this style of testing, mix in trait PropertyChecks, or import the
members of its companion object. As an example property-based testing using both table- and generator-driven properties,
imagine you want to test this class Fraction(n: Int, d: Int) {
If you mix in forAll { (n: Int, d: Int) =>
The
You might place the previous property check in its own test whose name describes the property at a high level, such as
val invalidCombos = Table( ("n", "d"), (Integer.MIN_VALUE, Integer.MIN_VALUE), (1, Integer.MIN_VALUE), (Integer.MIN_VALUE, 1), (Integer.MIN_VALUE, 0), (1, 0) )
In this example,
After the declaration of the table, For more information check out the user guide pages for: Next, learn about using ScalaTest's Selenium DSL. |
ScalaTest is brought to you by Bill Venners, with
contributions from several other folks. It is sponsored by
Artima, Inc.
ScalaTest is free, open-source software
released under the Apache
2.0 license.
Copyright © 2009-2013 Artima, Inc. All Rights Reserved.