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
+31-11Lines changed: 31 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,18 +5,40 @@
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, as well as Dates, `OptionSet` and `CaseIterable` enums
14
+
* Compose generators to create more complex inputs
15
+
* Automatically [shrink](#shrinking) large inputs to easily digestable inputs, including for custom types without extra effort
16
+
* Switch to specific failing cases with just [a single line](#using-fixed-seeds)
11
17
12
18
## Requirements
13
19
14
-
* Swift 6.1 (or Xcode 16.3)
20
+
* Swift 6.2 (or Xcode 26)
21
+
* Limited support for Swift 6.1 and Xcode 16.3
15
22
* Any platform supported by Swift Testing
16
23
* macOS 10.15+
17
24
* iOS/tvOS 13.0+, watchOS 6.0+, visionOS 1.0+
18
25
* Linux, Windows, etc.
19
26
27
+
## Installation
28
+
29
+
### When writing a Swift package:
30
+
31
+
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`
41
+
20
42
## Examples
21
43
22
44
Simple example:
@@ -77,9 +99,6 @@ func failsSometimes() async {
77
99
78
100
# Shrinking
79
101
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
102
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
103
85
104
For example, the following test fails when the given numbers sum to a value above a certain threshold:
@@ -95,16 +114,17 @@ For example, the following test fails when the given numbers sum to a value abov
95
114
96
115
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
116
98
-
Enable the shrinker by adding the `shrinking` trait:
117
+
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
118
100
-
```swift
101
-
@Test(.shrinking) funccheckSumInRange() async
102
119
```
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.
120
+
Failure occured with input [46, 97, 68, 23, 16].
121
+
(shrunk down from [63, 61, 33, 53, 97, 68, 23, 16] after 7 iterations)
122
+
```
105
123
106
124
When using the built-in generators and the `zip` function, shrinkers will also be composed.
107
125
126
+
The `.shrinking` TestTrait or SuiteTrait can be used to override shrinking behavior.
0 commit comments