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
Copy file name to clipboardExpand all lines: README.md
+44-7Lines changed: 44 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4343,21 +4343,21 @@ _You can enable the following settings in Xcode by running [this script](resourc
4343
4343
## Testing
4344
4344
4345
4345
*<a id='prefer-unwrapping-apis'></a>(<a href='#prefer-unwrapping-apis'>link</a>) **Prefer Test APIs for unwrapping optionals over `iflet`.** XCTest and Swift Testing have APIs for unwrapping an optional and failing the test, which are much simpler than unwrapping the optionals yourself.
4346
+
4346
4347
<details>
4347
4348
4348
4349
```swift
4349
4350
import XCTest
4350
4351
4351
4352
finalclass SomeTestCase: XCTestCase {
4352
-
4353
4353
functest_something() throws {
4354
-
// RIGHT:
4355
-
let value =tryXCTUnwrap(optionalValue)
4356
-
4357
4354
// WRONG:
4358
4355
guardlet value = optionalValue else {
4359
4356
XCTFail()
4360
4357
}
4358
+
4359
+
// RIGHT:
4360
+
let value =tryXCTUnwrap(optionalValue)
4361
4361
}
4362
4362
}
4363
4363
```
@@ -4368,17 +4368,54 @@ _You can enable the following settings in Xcode by running [this script](resourc
4368
4368
structSomeTests {
4369
4369
@Test
4370
4370
funcsomething() throws {
4371
-
// RIGHT:
4372
-
let value =try#require(optionalValue)
4373
-
4374
4371
// WRONG:
4375
4372
guardlet value = optionalValue {
4376
4373
return
4377
4374
}
4375
+
4376
+
// RIGHT:
4377
+
let value =try#require(optionalValue)
4378
+
}
4379
+
}
4380
+
```
4381
+
4382
+
*<a id='prefer-throwing-tests'></a>(<a href='#prefer-throwing-tests'>link</a>) **Prefer throwing tests to `try!`** `try!` will crash your test suite like a force-unwrapped optional. XCTest and Swift Testing support throwing test methods, so use that instead. [](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#throwingTests)
0 commit comments