You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"This trait enables the shrinker by default. Regardless of this trait being set, you can use the shrinking() TestTrait to enable/disable the shrinker on a case-by-case basis."
Copy file name to clipboardExpand all lines: README.md
+29-10Lines changed: 29 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,9 +5,15 @@
5
5
6
6
PropertyBased is a Swift 6 library that enables Property-Based Testing in `swift-testing`, similar to QuickCheck for Haskell or FsCheck for F# and C#.
7
7
8
-
Property-Based Testing can be used as an alternative for (or in addition to) testing with hardcoded values. Run tests with composable random inputs, then easily switch to specific failing cases just by adding a single line.
8
+
Property-Based Testing can be used as an alternative for (or in addition to) testing with hardcoded values.
9
9
10
-
This project aims to support all platforms which can run Swift Testing, including platforms without [Foundation](https://developer.apple.com/documentation/foundation) support.
10
+
## Features
11
+
12
+
* Works through Swift Testing, including on platforms without [Foundation](https://developer.apple.com/documentation/foundation) support
13
+
* Contains a set of generators for all commonly-used types in the Swift standard library
14
+
* Compose generators to create more complex inputs
15
+
* Automatically [shrink](#shrinking) large inputs to easily digestable inputs
16
+
* Switch to specific failing cases with just [a single line](#using-fixed-seeds)
11
17
12
18
## Requirements
13
19
@@ -17,6 +23,21 @@ This project aims to support all platforms which can run Swift Testing, includin
17
23
* iOS/tvOS 13.0+, watchOS 6.0+, visionOS 1.0+
18
24
* Linux, Windows, etc.
19
25
26
+
## Installation
27
+
28
+
### When writing a Swift package:
29
+
30
+
Add the following line to the dependencies array in your `Package.swift` file:
3. Paste the repository URL in the search field: `https://github.com/x-sheep/swift-property-based.git`
40
+
20
41
## Examples
21
42
22
43
Simple example:
@@ -77,9 +98,6 @@ func failsSometimes() async {
77
98
78
99
# Shrinking
79
100
80
-
> [!NOTE]
81
-
> This feature is experimental, and disabled by default. The shrinking output will be very verbose, due to a limitation in Swift Testing.
82
-
83
101
When a failing case has been found, it's possible that the input is large and contrived, such as arrays with many elements. When _shrinking_ is enabled, PropertyBased will repeat a failing test until it finds the smallest possible input that still causes a failure.
84
102
85
103
For example, the following test fails when the given numbers sum to a value above a certain threshold:
@@ -95,16 +113,17 @@ For example, the following test fails when the given numbers sum to a value abov
95
113
96
114
The generator could come up with an array like `[63, 61, 33, 53, 97, 68, 23, 16]`, which sums to `414`. Ideally, we want to have an input that sums to exactly `250`.
97
115
98
-
Enable the shrinker by adding the `shrinking` trait:
116
+
After shrinking, the new failing case is `[46, 97, 68, 23, 16]`, which sums to exactly `250`. The first few elements have been removed, while the middle element has been reduced to be closer to the edge. PropertyBased will output both the shrunk input and the original input:
99
117
100
-
```swift
101
-
@Test(.shrinking) funccheckSumInRange() async
102
118
```
103
-
104
-
After shrinking, the new failing case is `[46, 97, 68, 23, 16]`, which sums to exactly `250`. The first few elements have been removed, while the middle element has been reduced to be closer to the edge.
119
+
Failure occured with input [46, 97, 68, 23, 16].
120
+
(shrunk down from [63, 61, 33, 53, 97, 68, 23, 16] after 7 iterations)
121
+
```
105
122
106
123
When using the built-in generators and the `zip` function, shrinkers will also be composed.
107
124
125
+
The `.shrinking` TestTrait or SuiteTrait can be used to override shrinking behavior.
0 commit comments