Skip to content

Commit 628cbdc

Browse files
authored
Add rule to avoid redundant Swift Testing expectation messages (#371)
1 parent 2a503a6 commit 628cbdc

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5092,6 +5092,44 @@ _You can enable the following settings in Xcode by running [this script](https:/
50925092

50935093
</details>
50945094

5095+
- <a id='avoid-redundant-expectation-comments'></a>(<a href='#avoid-redundant-expectation-comments'>link</a>) **In Swift Testing, avoid expectation message strings that restate the expectation without adding additional context.** Unlike `XCTAssert`, the Swift Testing `#expect` macro generates detailed failure messages that include the expectation condition.
5096+
5097+
<details>
5098+
5099+
```swift
5100+
// WRONG: Restates what #expect already reports in failure output
5101+
@Test
5102+
func `engage warp drive`() {
5103+
spaceship.engageWarpDrive()
5104+
#expect(spaceship.isWarpDriveActive, "Warp drive should be active")
5105+
#expect(spaceship.speed > lightSpeed, "Speed should be greater than light speed")
5106+
}
5107+
5108+
// RIGHT: Omits the message string, or adds valuable context
5109+
@Test
5110+
func `engage warp drive`() {
5111+
spaceship.engageWarpDrive()
5112+
#expect(spaceship.isWarpDriveActive)
5113+
#expect(spaceship.speed > lightSpeed, "Spaceship must reach light speed before the warp bubble can form")
5114+
}
5115+
```
5116+
5117+
Code comments also work well to add additional context:
5118+
5119+
```swift
5120+
// ALSO RIGHT
5121+
@Test
5122+
func `engage warp drive`() {
5123+
spaceship.engageWarpDrive()
5124+
#expect(spaceship.isWarpDriveActive)
5125+
5126+
// Spaceship must reach light speed before the warp bubble can form
5127+
#expect(spaceship.speed > lightSpeed)
5128+
}
5129+
```
5130+
5131+
</details>
5132+
50955133
- <a id='avoid-guard-in-tests'></a>(<a href='#avoid-guard-in-tests'>link</a>) **Avoid `guard` statements in unit tests**. XCTest and Swift Testing have APIs for unwrapping an optional and failing the test, which are much simpler than unwrapping the optionals yourself. Use assertions instead of guarding on boolean conditions.
50965134

50975135
<details>

0 commit comments

Comments
 (0)