0
# specs2-scalacheck
1
2
A Scala library that provides seamless integration between the specs2 testing framework and ScalaCheck property-based testing. This integration enables developers to write property-based tests using ScalaCheck's automatic test case generation, shrinking capabilities, and statistical analysis within specs2's behavior-driven development (BDD) specification DSL.
3
4
## Package Information
5
6
- **Package Name**: specs2-scalacheck
7
- **Language**: Scala
8
- **Package Type**: Maven/SBT
9
- **Installation**: `libraryDependencies += "org.specs2" %% "specs2-scalacheck" % "4.21.0"`
10
11
## Core Imports
12
13
```scala
14
import org.specs2.ScalaCheck
15
```
16
17
For individual components:
18
19
```scala
20
import org.specs2.scalacheck._
21
```
22
23
## Basic Usage
24
25
```scala
26
import org.specs2._
27
import org.specs2.ScalaCheck
28
import org.scalacheck._
29
30
class MySpec extends Specification with ScalaCheck { def is = s2"""
31
String concatenation properties
32
concatenation length ${prop((s1: String, s2: String) =>
33
(s1 + s2).length == s1.length + s2.length)}
34
35
empty string identity ${prop((s: String) =>
36
s + "" == s && "" + s == s)}
37
38
Integer arithmetic properties
39
addition commutativity ${prop((a: Int, b: Int) =>
40
a + b == b + a)}
41
42
multiplication by zero ${prop((n: Int) =>
43
n * 0 == 0)}
44
"""
45
}
46
```
47
48
## Architecture
49
50
The specs2-scalacheck integration follows a modular architecture that bridges two testing paradigms:
51
52
- **Property Creation**: The `ScalaCheckPropertyCreation` trait provides `prop` methods that convert functions into testable properties with support for 1-8 parameters
53
- **Property Execution**: The `ScalaCheckPropertyCheck` trait handles property execution, result interpretation, and seed management for reproducible tests
54
- **Parameter Management**: The `Parameters` case class encapsulates all ScalaCheck test parameters plus specs2-specific configuration
55
- **Type Integration**: The `AsResultProp` trait provides seamless conversion between specs2's `Result` types and ScalaCheck's `Prop` types
56
- **DSL Integration**: The `ScalaCheckPropertyDsl` trait enables properties to be used directly in specs2 specifications
57
58
This architecture allows property-based tests to integrate seamlessly with specs2's specification DSL while preserving ScalaCheck's full feature set including custom generators, shrinkers, and statistical data collection.
59
60
## Capabilities
61
62
### Property Creation
63
64
Create property-based tests from functions with automatic test case generation and shrinking. Supports functions with 1-8 parameters, custom generators, shrinkers, and data collectors.
65
66
```scala { .api }
67
def prop[T, R](result: T => R)(implicit
68
arbitrary: Arbitrary[T],
69
shrink: Shrink[T],
70
pretty: T => Pretty,
71
prettyFreqMap: FreqMap[Set[Any]] => Pretty,
72
asResult: AsResult[R],
73
parameters: Parameters
74
): ScalaCheckFunction1[T, R]
75
76
def prop[T1, T2, R](result: (T1, T2) => R)(implicit ...): ScalaCheckFunction2[T1, T2, R]
77
// ... up to 8 parameters
78
```
79
80
[Property Creation](./property-creation.md)
81
82
### Property Configuration
83
84
Configure test execution parameters including number of tests, generator sizes, random seeds, verbosity, and worker threads. Supports both programmatic configuration and command-line parameter overrides.
85
86
```scala { .api }
87
case class Parameters(
88
minTestsOk: Int = 100,
89
minSize: Int = 0,
90
maxDiscardRatio: Float = 5.0f,
91
maxSize: Int = 100,
92
workers: Int = 1,
93
testCallback: Test.TestCallback = Test.Parameters.default.testCallback,
94
loader: Option[ClassLoader] = None,
95
prettyParams: Pretty.Params = Pretty.defaultParams,
96
seed: Option[Seed] = None
97
)
98
99
def set(minTestsOk: Int = ..., minSize: Int = ..., ...): Parameters
100
def display(minTestsOk: Int = ..., minSize: Int = ..., ...): Parameters
101
```
102
103
[Configuration](./configuration.md)
104
105
### Property Execution
106
107
Execute properties with comprehensive result reporting, failure analysis, and statistical data collection. Includes seed capture for reproducible test runs and detailed error reporting.
108
109
```scala { .api }
110
def check(prop: Prop, parameters: Parameters, prettyFreqMap: FreqMap[Set[Any]] => Pretty): Result
111
def checkProperties(properties: Properties, parameters: Parameters, prettyFreqMap: FreqMap[Set[Any]] => Pretty): Result
112
```
113
114
[Execution](./execution.md)
115
116
### Type Integration
117
118
Seamless conversion between specs2 and ScalaCheck types with automatic result interpretation and property wrapping. Supports all specs2 result types and ScalaCheck property types.
119
120
```scala { .api }
121
implicit def asResultToProp[R : AsResult](r: R): Prop
122
implicit def propAsResult(implicit p: Parameters, pfq: FreqMap[Set[Any]] => Pretty): AsResult[Prop]
123
implicit def propertiesAsResult(implicit p: Parameters, pfq: FreqMap[Set[Any]] => Pretty): AsResult[Properties]
124
```
125
126
[Type Integration](./type-integration.md)
127
128
## Types
129
130
```scala { .api }
131
// Main integration trait
132
trait ScalaCheck extends
133
ScalaCheckPropertyCreation with
134
ScalaCheckPropertyCheck with
135
ScalaCheckParameters with
136
AsResultProp with
137
ScalaCheckPropertyDsl with
138
GenInstances
139
140
// Property wrapper types
141
trait ScalaCheckProperty {
142
type SelfType <: ScalaCheckProperty
143
def prop: Prop
144
def parameters: Parameters
145
def prettyFreqMap: FreqMap[Set[Any]] => Pretty
146
def setParameters(ps: Parameters): SelfType
147
def setSeed(seed: Seed): SelfType
148
def setSeed(seed: String): SelfType
149
}
150
151
// Type class instances container
152
case class ScalaCheckArgInstances[T](
153
arbitrary: Arbitrary[T],
154
shrink: Option[Shrink[T]],
155
collectors: List[T => Any],
156
pretty: T => Pretty
157
)
158
159
// Functional programming support for generators
160
trait GenInstances {
161
implicit def genMonad: Monad[Gen]
162
}
163
```