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 Asynchronous testing Using Scala-js Using Inside Using OptionValues Using EitherValues Using PartialFunctionValues Using PrivateMethodTester Using WrapWith Philosophy and design Migrating to 3.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 through ScalaTest + ScalaCheck library, by including: libraryDependencies += "org.scalatestplus" %% "scalacheck-1-18" % "3.2.19.0" % "test" in your SBT file, or if you use Maven: <dependency> <groupId>org.scalatestplus</groupId> <artifactId>scalacheck-1-18_3</artifactId> <version>3.2.19.0</version> <scope>test</scope> </dependency>
To use this style of testing, mix in trait
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,
When you get a failed property check, ScalaTest will report the failure with initial seed so that you can reproduce the failure. You'll need to pass the initial seed value through testOptions += Tests.Argument(TestFrameworks.ScalaTest, "-S", "123456789")or if you are using testOnly you can do: sbt> testOnly MyTest -- -S 123456879 For more information check out the user guide pages for: Next, learn about Asynchronous testing. |
ScalaTest is brought to you by Bill Venners and Artima.
ScalaTest is free, open-source software
released under the Apache
2.0 license.
If your company loves ScalaTest, please consider sponsoring the project.
Copyright © 2009-2024 Artima, Inc. All Rights Reserved.