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
+89Lines changed: 89 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4689,6 +4689,95 @@ _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>) **In test suites, test cases should be `internal`, and helper methods and properties should be `private`**. [](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#testSuiteAccessControl)
4693
+
4694
+
<details>
4695
+
4696
+
#### Why?
4697
+
Test suites and test cases don't need to be `public` to be picked up by XCTest / Swift Testing, so should be `internal`.
4698
+
4699
+
Helpers and stored properties should be `private` since they are not accessed outside of the test suite.
4700
+
4701
+
```swift
4702
+
import Testing
4703
+
4704
+
// WRONG
4705
+
struct SpaceshipTests {
4706
+
let spaceship = Spaceship()
4707
+
4708
+
func launchSpaceship() {
4709
+
spaceship.launch()
4710
+
}
4711
+
4712
+
@Test
4713
+
func spaceshipCanLaunch() {
4714
+
launchSpaceship()
4715
+
#expect(spaceship.hasLaunched)
4716
+
}
4717
+
}
4718
+
4719
+
// RIGHT
4720
+
struct SpaceshipTests {
4721
+
4722
+
// MARK: Internal
4723
+
4724
+
@Test
4725
+
func spaceshipCanLaunch() {
4726
+
launchSpaceship()
4727
+
#expect(spaceship.hasLaunched)
4728
+
}
4729
+
4730
+
// MARK: Private
4731
+
4732
+
private let spaceship = Spaceship()
4733
+
4734
+
private func launchSpaceship() {
4735
+
spaceship.launch()
4736
+
}
4737
+
4738
+
}
4739
+
```
4740
+
4741
+
```swift
4742
+
import XCTest
4743
+
4744
+
// WRONG
4745
+
final class SpaceshipTests: XCTestCase {
4746
+
let spaceship = Spaceship()
4747
+
4748
+
func launchSpaceship() {
4749
+
spaceship.launch()
4750
+
}
4751
+
4752
+
func testSpaceshipCanLaunch() {
4753
+
launchSpaceship()
4754
+
XCTAssertTrue(spaceship.hasLaunched)
4755
+
}
4756
+
}
4757
+
4758
+
// RIGHT
4759
+
final class SpaceshipTests: XCTestCase {
4760
+
4761
+
// MARK: Internal
4762
+
4763
+
func testSpaceshipCanLaunch() {
4764
+
launchSpaceship()
4765
+
XCTAssertTrue(spaceship.hasLaunched)
4766
+
}
4767
+
4768
+
// MARK: Private
4769
+
4770
+
private let spaceship = Spaceship()
4771
+
4772
+
private func launchSpaceship() {
4773
+
spaceship.launch()
4774
+
}
4775
+
4776
+
}
4777
+
```
4778
+
4779
+
</details>
4780
+
4692
4781
* <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