Skip to content

Commit e46f99c

Browse files
committed
add throwing tests rule
1 parent 535c69b commit e46f99c

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4340,6 +4340,50 @@ _You can enable the following settings in Xcode by running [this script](resourc
43404340

43414341
**[ back to top](#table-of-contents)**
43424342

4343+
## Testing
4344+
4345+
* <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. [![SwiftFormat: wrap](https://img.shields.io/badge/SwiftFormat-throwingTests-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#throwingTests)
4346+
4347+
<details>
4348+
4349+
```swift
4350+
import XCTest
4351+
4352+
final class SomeTestCase: XCTestCase {
4353+
4354+
// RIGHT:
4355+
func test_something() throws {
4356+
try Something().doSomething()
4357+
}
4358+
4359+
// WRONG:
4360+
func test_something() {
4361+
try! Something().doSomething()
4362+
}
4363+
}
4364+
```
4365+
4366+
```swift
4367+
import Testing
4368+
4369+
struct SomeTests {
4370+
// WRONG:
4371+
@Test
4372+
func something() {
4373+
try! Something().doSomething()
4374+
}
4375+
4376+
// RIGHT:
4377+
@Test
4378+
func something() throws {
4379+
try Something().doSomething()
4380+
}
4381+
}
4382+
```
4383+
</details>
4384+
4385+
**[ back to top](#table-of-contents)**
4386+
43434387
## Contributors
43444388

43454389
- [View Contributors](https://github.com/airbnb/swift/graphs/contributors)

0 commit comments

Comments
 (0)