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
+84Lines changed: 84 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4689,6 +4689,90 @@ _You can enable the following settings in Xcode by running [this script](resourc
4689
4689
```
4690
4690
</details>
4691
4691
4692
+
* <a id='test-suite-access-control'></a>(<a href='#test-suite-access-control'>link</a>) **Test case functions should be `internal`, and helper methods and properties should be `private`**. This ensures test cases are accessible to the test runner while keeping test implementation details encapsulated. [](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#testSuiteAccessControl)
4693
+
4694
+
<details>
4695
+
4696
+
```swift
4697
+
import Testing
4698
+
4699
+
// WRONG
4700
+
struct SpaceshipTests {
4701
+
let spaceship = Spaceship()
4702
+
4703
+
func launchSpaceship() {
4704
+
spaceship.launch()
4705
+
}
4706
+
4707
+
@Test
4708
+
func spaceshipCanLaunch() {
4709
+
launchSpaceship()
4710
+
#expect(spaceship.hasLaunched)
4711
+
}
4712
+
}
4713
+
4714
+
// RIGHT
4715
+
struct SpaceshipTests {
4716
+
4717
+
// MARK: Internal
4718
+
4719
+
@Test
4720
+
func spaceshipCanLaunch() {
4721
+
launchSpaceship()
4722
+
#expect(spaceship.hasLaunched)
4723
+
}
4724
+
4725
+
// MARK: Private
4726
+
4727
+
private let spaceship = Spaceship()
4728
+
4729
+
private func launchSpaceship() {
4730
+
spaceship.launch()
4731
+
}
4732
+
4733
+
}
4734
+
```
4735
+
4736
+
```swift
4737
+
import XCTest
4738
+
4739
+
// WRONG
4740
+
final class SpaceshipTests: XCTestCase {
4741
+
let spaceship = Spaceship()
4742
+
4743
+
func launchSpaceship() {
4744
+
spaceship.launch()
4745
+
}
4746
+
4747
+
func testSpaceshipCanLaunch() {
4748
+
launchSpaceship()
4749
+
XCTAssertTrue(spaceship.hasLaunched)
4750
+
}
4751
+
}
4752
+
4753
+
// RIGHT
4754
+
final class SpaceshipTests: XCTestCase {
4755
+
4756
+
// MARK: Internal
4757
+
4758
+
func testSpaceshipCanLaunch() {
4759
+
launchSpaceship()
4760
+
XCTAssertTrue(spaceship.hasLaunched)
4761
+
}
4762
+
4763
+
// MARK: Private
4764
+
4765
+
private let spaceship = Spaceship()
4766
+
4767
+
private func launchSpaceship() {
4768
+
spaceship.launch()
4769
+
}
4770
+
4771
+
}
4772
+
```
4773
+
4774
+
</details>
4775
+
4692
4776
* <a id='avoid-force-unwrap-in-tests'></a>(<a href='#avoid-force-unwrap-in-tests'>link</a>) **Avoid force-unwrapping in unit tests**. Force-unwrapping (`!`) will crash your test suite. Use safe alternatives like `try XCTUnwrap` or `try #require`, which will throw an error instead, or standard optional unwrapping (`?`). [](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#noForceUnwrapInTests)
0 commit comments