Skip to content

Commit 4d3df92

Browse files
test: Specify integrations when starting SDK (#3427)
Specify the minimum integrations required for every test case using SentrySDK.start to minimize side effects for tests and reduce flakiness.
1 parent bd2cb64 commit 4d3df92

11 files changed

+76
-9
lines changed

CONTRIBUTING.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Test guidelines:
4747
* We prefer using [Nimble](https://github.com/Quick/Nimble) over XCTest for test assertions. We can't use the latest Nimble version and are stuck
4848
with [v10.0.0](https://github.com/Quick/Nimble/releases/tag/v10.0.0), cause it's the latest one that still supports Xcode 13.2.1, which we use in CI for
4949
running our tests. [v11.0.0](https://github.com/Quick/Nimble/releases/tag/v11.0.0) already requires Swift 5.6 / Xcode 13.3.
50+
* When calling `SentrySDK.start` in a test, specify only the minimum integrations required to minimize side effects for tests and reduce flakiness.
5051

5152

5253

Sentry.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
62A456E32B0370AA003F19A1 /* SentryUIEventTrackerTransactionMode.h in Headers */ = {isa = PBXBuildFile; fileRef = 62A456E22B0370AA003F19A1 /* SentryUIEventTrackerTransactionMode.h */; };
9090
62A456E52B0370E0003F19A1 /* SentryUIEventTrackerTransactionMode.m in Sources */ = {isa = PBXBuildFile; fileRef = 62A456E42B0370E0003F19A1 /* SentryUIEventTrackerTransactionMode.m */; };
9191
62B86CFC29F052BB008F3947 /* SentryTestLogConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 62B86CFB29F052BB008F3947 /* SentryTestLogConfig.m */; };
92+
62C25C862B075F4900C68CBD /* TestOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62C25C852B075F4900C68CBD /* TestOptions.swift */; };
9293
62E081A929ED4260000F69FC /* SentryBreadcrumbDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 62E081A829ED4260000F69FC /* SentryBreadcrumbDelegate.h */; };
9394
62E081AB29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E081AA29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift */; };
9495
62F226B729A37C120038080D /* SentryBooleanSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = 62F226B629A37C120038080D /* SentryBooleanSerialization.m */; };
@@ -975,6 +976,7 @@
975976
62A456E22B0370AA003F19A1 /* SentryUIEventTrackerTransactionMode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryUIEventTrackerTransactionMode.h; path = include/SentryUIEventTrackerTransactionMode.h; sourceTree = "<group>"; };
976977
62A456E42B0370E0003F19A1 /* SentryUIEventTrackerTransactionMode.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryUIEventTrackerTransactionMode.m; sourceTree = "<group>"; };
977978
62B86CFB29F052BB008F3947 /* SentryTestLogConfig.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryTestLogConfig.m; sourceTree = "<group>"; };
979+
62C25C852B075F4900C68CBD /* TestOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestOptions.swift; sourceTree = "<group>"; };
978980
62E081A829ED4260000F69FC /* SentryBreadcrumbDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryBreadcrumbDelegate.h; path = include/SentryBreadcrumbDelegate.h; sourceTree = "<group>"; };
979981
62E081AA29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryBreadcrumbTestDelegate.swift; sourceTree = "<group>"; };
980982
62F226B629A37C120038080D /* SentryBooleanSerialization.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryBooleanSerialization.m; sourceTree = "<group>"; };
@@ -3218,6 +3220,7 @@
32183220
844EDC7829415AB300C86F34 /* TestSentrySystemWrapper.swift */,
32193221
844EDCE72947DCD700C86F34 /* TestSentryNSTimerFactory.swift */,
32203222
84B7FA3B29B2866200AD93B1 /* SentryTestUtils-ObjC-BridgingHeader.h */,
3223+
62C25C852B075F4900C68CBD /* TestOptions.swift */,
32213224
);
32223225
path = SentryTestUtils;
32233226
sourceTree = "<group>";
@@ -4501,6 +4504,7 @@
45014504
files = (
45024505
8431F01629B2851500D8DC56 /* TestSentryNSProcessInfoWrapper.swift in Sources */,
45034506
84B7FA4229B28CDE00AD93B1 /* TestCurrentDateProvider.swift in Sources */,
4507+
62C25C862B075F4900C68CBD /* TestOptions.swift in Sources */,
45044508
84B7FA3F29B28BAD00AD93B1 /* TestTransport.swift in Sources */,
45054509
84A5D75B29D5170700388BFA /* TimeInterval+Sentry.swift in Sources */,
45064510
84AC61D929F7643B009EEF61 /* TestDispatchFactory.swift in Sources */,

SentryTestUtils/TestOptions.swift

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Foundation
2+
import Sentry
3+
4+
public extension Options {
5+
func setIntegrations(_ integrations: [AnyClass]) {
6+
self.integrations = integrations.map {
7+
String(describing: $0)
8+
}
9+
}
10+
11+
func removeAllIntegrations() {
12+
self.integrations = []
13+
}
14+
}

Tests/SentryTests/Integrations/Performance/IO/SentryNSDataTrackerTests.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ class SentryNSDataTrackerTests: XCTestCase {
3434
super.setUp()
3535
fixture = Fixture()
3636
fixture.getSut().enable()
37-
SentrySDK.start { $0.enableFileIOTracing = true }
37+
SentrySDK.start {
38+
$0.removeAllIntegrations()
39+
}
3840
}
3941

4042
override func tearDown() {

Tests/SentryTests/Integrations/Screenshot/SentryScreenshotIntegrationTests.swift

+12-3
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,28 @@ class SentryScreenshotIntegrationTests: XCTestCase {
3636
}
3737

3838
func test_attachScreenshot_disabled() {
39-
SentrySDK.start { $0.attachScreenshot = false }
39+
SentrySDK.start {
40+
$0.attachScreenshot = false
41+
$0.setIntegrations([SentryScreenshotIntegration.self])
42+
}
4043
XCTAssertEqual(SentrySDK.currentHub().getClient()?.attachmentProcessors.count, 0)
4144
XCTAssertFalse(sentrycrash_hasSaveScreenshotCallback())
4245
}
4346

4447
func test_attachScreenshot_enabled() {
45-
SentrySDK.start { $0.attachScreenshot = true }
48+
SentrySDK.start {
49+
$0.attachScreenshot = true
50+
$0.setIntegrations([SentryScreenshotIntegration.self])
51+
}
4652
XCTAssertEqual(SentrySDK.currentHub().getClient()?.attachmentProcessors.count, 1)
4753
XCTAssertTrue(sentrycrash_hasSaveScreenshotCallback())
4854
}
4955

5056
func test_uninstall() {
51-
SentrySDK.start { $0.attachScreenshot = true }
57+
SentrySDK.start {
58+
$0.attachScreenshot = true
59+
$0.setIntegrations([SentryScreenshotIntegration.self])
60+
}
5261
SentrySDK.close()
5362

5463
XCTAssertNil(SentrySDK.currentHub().getClient()?.attachmentProcessors)

Tests/SentryTests/Integrations/SentryCrash/SentryCrashIntegrationTests.swift

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class SentryCrashIntegrationTests: NotificationCenterTestCase {
7777
options.dsn = SentryCrashIntegrationTests.dsnAsString
7878
options.releaseName = releaseName
7979
options.dist = dist
80+
options.setIntegrations([SentryCrashIntegration.self])
8081
}
8182

8283
// To test this properly we need SentryCrash and SentryCrashIntegration installed and registered on the current hub of the SDK.
@@ -89,6 +90,7 @@ class SentryCrashIntegrationTests: NotificationCenterTestCase {
8990
func testContext_IsPassedToSentryCrash() throws {
9091
SentrySDK.start { options in
9192
options.dsn = SentryCrashIntegrationTests.dsnAsString
93+
options.setIntegrations([SentryCrashIntegration.self])
9294
}
9395

9496
let userInfo = try XCTUnwrap(SentryDependencyContainer.sharedInstance().crashReporter.userInfo)

Tests/SentryTests/Integrations/ViewHierarchy/SentryViewHierarchyIntegrationTests.swift

+16-4
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,38 @@ class SentryViewHierarchyIntegrationTests: XCTestCase {
3636
}
3737

3838
func test_attachViewHierarchy() {
39-
SentrySDK.start { $0.attachViewHierarchy = false }
39+
SentrySDK.start {
40+
$0.attachViewHierarchy = false
41+
$0.setIntegrations([SentryViewHierarchyIntegration.self])
42+
}
4043
XCTAssertEqual(SentrySDK.currentHub().getClient()?.attachmentProcessors.count, 0)
4144
XCTAssertFalse(sentrycrash_hasSaveViewHierarchyCallback())
4245
}
4346

4447
func test_attachViewHierarchy_enabled() {
45-
SentrySDK.start { $0.attachViewHierarchy = true }
48+
SentrySDK.start {
49+
$0.attachViewHierarchy = true
50+
$0.setIntegrations([SentryViewHierarchyIntegration.self])
51+
}
4652
XCTAssertEqual(SentrySDK.currentHub().getClient()?.attachmentProcessors.count, 1)
4753
XCTAssertTrue(sentrycrash_hasSaveViewHierarchyCallback())
4854
}
4955

5056
func test_uninstall() {
51-
SentrySDK.start { $0.attachViewHierarchy = true }
57+
SentrySDK.start {
58+
$0.attachViewHierarchy = true
59+
$0.setIntegrations([SentryViewHierarchyIntegration.self])
60+
}
5261
SentrySDK.close()
5362
XCTAssertNil(SentrySDK.currentHub().getClient()?.attachmentProcessors)
5463
XCTAssertFalse(sentrycrash_hasSaveViewHierarchyCallback())
5564
}
5665

5766
func test_integrationAddFileName() {
58-
SentrySDK.start { $0.attachViewHierarchy = true }
67+
SentrySDK.start {
68+
$0.attachViewHierarchy = true
69+
$0.setIntegrations([SentryViewHierarchyIntegration.self])
70+
}
5971
saveViewHierarchy("/test/path")
6072
XCTAssertEqual("/test/path/view-hierarchy.json", fixture.viewHierarchy.saveFilePathUsed)
6173
}

Tests/SentryTests/SentryCrash/SentryCrashInstallationReporterTests.swift

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class SentryCrashInstallationReporterTests: XCTestCase {
5151
private func sdkStarted() {
5252
SentrySDK.start { options in
5353
options.dsn = SentryCrashInstallationReporterTests.dsnAsString
54+
options.setIntegrations([SentryCrashIntegration.self])
5455
}
5556
let options = Options()
5657
options.dsn = SentryCrashInstallationReporterTests.dsnAsString

Tests/SentryTests/SentryCrash/SentryStacktraceBuilderTests.swift

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class SentryStacktraceBuilderTests: XCTestCase {
8484
options.dsn = TestConstants.dsnAsString(username: "SentryStacktraceBuilderTests")
8585
options.swiftAsyncStacktraces = true
8686
options.debug = true
87+
options.setIntegrations([SentryCrashIntegration.self, SentrySwiftAsyncIntegration.self])
8788
}
8889

8990
let waitForAsyncToRun = expectation(description: "Wait async functions")
@@ -113,6 +114,7 @@ class SentryStacktraceBuilderTests: XCTestCase {
113114
options.dsn = TestConstants.dsnAsString(username: "SentryStacktraceBuilderTests")
114115
options.swiftAsyncStacktraces = false
115116
options.debug = true
117+
options.setIntegrations([SentryCrashIntegration.self, SentrySwiftAsyncIntegration.self])
116118
}
117119

118120
let waitForAsyncToRun = expectation(description: "Wait async functions")

Tests/SentryTests/SentrySDKTests.swift

+20-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class SentrySDKTests: XCTestCase {
8080
SentrySDK.start { options in
8181
options.dsn = SentrySDKTests.dsnAsString
8282
options.maxBreadcrumbs = 0
83+
options.setIntegrations([])
8384
}
8485

8586
SentrySDK.addBreadcrumb(Breadcrumb(level: SentryLevel.warning, category: "test"))
@@ -123,6 +124,7 @@ class SentrySDKTests: XCTestCase {
123124
func testStartStopBinaryImageCache() {
124125
SentrySDK.start { options in
125126
options.debug = true
127+
options.removeAllIntegrations()
126128
}
127129

128130
XCTAssertNotNil(SentryDependencyContainer.sharedInstance().binaryImageCache.cache)
@@ -136,6 +138,7 @@ class SentrySDKTests: XCTestCase {
136138
func testStartWithConfigureOptions_NoDsn() throws {
137139
SentrySDK.start { options in
138140
options.debug = true
141+
options.removeAllIntegrations()
139142
}
140143

141144
let options = SentrySDK.currentHub().getClient()?.options
@@ -148,6 +151,7 @@ class SentrySDKTests: XCTestCase {
148151
func testStartWithConfigureOptions_WrongDsn() throws {
149152
SentrySDK.start { options in
150153
options.dsn = "wrong"
154+
options.removeAllIntegrations()
151155
}
152156

153157
let options = SentrySDK.currentHub().getClient()?.options
@@ -164,6 +168,7 @@ class SentrySDKTests: XCTestCase {
164168
wasBeforeSendCalled = true
165169
return event
166170
}
171+
options.removeAllIntegrations()
167172
}
168173

169174
SentrySDK.capture(message: "")
@@ -181,6 +186,7 @@ class SentrySDKTests: XCTestCase {
181186
XCTAssertEqual(123, Dynamic(suggested).maxBreadcrumbs)
182187
return scope
183188
}
189+
options.removeAllIntegrations()
184190
}
185191
XCTAssertEqual("me", SentrySDK.currentHub().scope.userObject?.userId)
186192
XCTAssertIdentical(scope, SentrySDK.currentHub().scope)
@@ -412,7 +418,7 @@ class SentrySDKTests: XCTestCase {
412418

413419
func testInstallIntegrations_NoIntegrations() {
414420
SentrySDK.start { options in
415-
options.integrations = []
421+
options.removeAllIntegrations()
416422
}
417423

418424
assertIntegrationsInstalled(integrations: [])
@@ -491,6 +497,7 @@ class SentrySDKTests: XCTestCase {
491497

492498
SentrySDK.start { options in
493499
options.dsn = SentrySDKTests.dsnAsString
500+
options.removeAllIntegrations()
494501
}
495502

496503
XCTAssertEqual(1, SentrySDK.startInvocations)
@@ -504,6 +511,7 @@ class SentrySDKTests: XCTestCase {
504511

505512
SentrySDK.start { options in
506513
options.dsn = SentrySDKTests.dsnAsString
514+
options.removeAllIntegrations()
507515
}
508516
XCTAssertTrue(SentrySDK.isEnabled)
509517

@@ -515,13 +523,15 @@ class SentrySDKTests: XCTestCase {
515523

516524
SentrySDK.start { options in
517525
options.dsn = SentrySDKTests.dsnAsString
526+
options.removeAllIntegrations()
518527
}
519528
XCTAssertTrue(SentrySDK.isEnabled)
520529
}
521530

522531
func testClose_ResetsDependencyContainer() {
523532
SentrySDK.start { options in
524533
options.dsn = SentrySDKTests.dsnAsString
534+
options.removeAllIntegrations()
525535
}
526536

527537
let first = SentryDependencyContainer.sharedInstance()
@@ -536,9 +546,12 @@ class SentrySDKTests: XCTestCase {
536546
func testClose_ClearsIntegrations() {
537547
SentrySDK.start { options in
538548
options.dsn = SentrySDKTests.dsnAsString
549+
options.swiftAsyncStacktraces = true
550+
options.setIntegrations([SentrySwiftAsyncIntegration.self])
539551
}
540552

541553
let hub = SentrySDK.currentHub()
554+
XCTAssertEqual(1, hub.installedIntegrations().count)
542555
SentrySDK.close()
543556
XCTAssertEqual(0, hub.installedIntegrations().count)
544557
assertIntegrationsInstalled(integrations: [])
@@ -549,6 +562,7 @@ class SentrySDKTests: XCTestCase {
549562
SentrySDK.start { options in
550563
options.dsn = SentrySDKTests.dsnAsString
551564
options.tracesSampleRate = 1
565+
options.removeAllIntegrations()
552566
}
553567

554568
let appStateManager = SentryDependencyContainer.sharedInstance().appStateManager
@@ -557,6 +571,7 @@ class SentrySDKTests: XCTestCase {
557571
SentrySDK.start { options in
558572
options.dsn = SentrySDKTests.dsnAsString
559573
options.tracesSampleRate = 1
574+
options.removeAllIntegrations()
560575
}
561576

562577
XCTAssertEqual(appStateManager.startCount, 2)
@@ -606,6 +621,7 @@ class SentrySDKTests: XCTestCase {
606621
func testClose_SetsClientToNil() {
607622
SentrySDK.start { options in
608623
options.dsn = SentrySDKTests.dsnAsString
624+
options.removeAllIntegrations()
609625
}
610626

611627
SentrySDK.close()
@@ -616,6 +632,7 @@ class SentrySDKTests: XCTestCase {
616632
func testClose_ClosesClient() {
617633
SentrySDK.start { options in
618634
options.dsn = SentrySDKTests.dsnAsString
635+
options.removeAllIntegrations()
619636
}
620637

621638
let client = SentrySDK.currentHub().client()
@@ -627,6 +644,7 @@ class SentrySDKTests: XCTestCase {
627644
func testClose_CallsFlushCorrectlyOnTransport() throws {
628645
SentrySDK.start { options in
629646
options.dsn = SentrySDKTests.dsnAsString
647+
options.removeAllIntegrations()
630648
}
631649

632650
let transport = TestTransport()
@@ -641,6 +659,7 @@ class SentrySDKTests: XCTestCase {
641659
func testFlush_CallsFlushCorrectlyOnTransport() throws {
642660
SentrySDK.start { options in
643661
options.dsn = SentrySDKTests.dsnAsString
662+
options.removeAllIntegrations()
644663
}
645664

646665
let transport = TestTransport()

Tests/SentryTests/SentryTests-Bridging-Header.h

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
#import "SentryScreenshot.h"
166166
#import "SentryScreenshotIntegration.h"
167167
#import "SentrySdkInfo.h"
168+
#import "SentrySwiftAsyncIntegration.h"
168169

169170
#import "SentrySerialization.h"
170171
#import "SentrySession+Private.h"

0 commit comments

Comments
 (0)