Skip to content

Commit

Permalink
test: Specify integrations when starting SDK (#3427)
Browse files Browse the repository at this point in the history
Specify the minimum integrations required for every test case using
SentrySDK.start to minimize side effects for tests and reduce flakiness.
  • Loading branch information
philipphofmann authored Nov 17, 2023
1 parent bd2cb64 commit 4d3df92
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 9 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Test guidelines:
* 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
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
running our tests. [v11.0.0](https://github.com/Quick/Nimble/releases/tag/v11.0.0) already requires Swift 5.6 / Xcode 13.3.
* When calling `SentrySDK.start` in a test, specify only the minimum integrations required to minimize side effects for tests and reduce flakiness.



Expand Down
4 changes: 4 additions & 0 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
62A456E32B0370AA003F19A1 /* SentryUIEventTrackerTransactionMode.h in Headers */ = {isa = PBXBuildFile; fileRef = 62A456E22B0370AA003F19A1 /* SentryUIEventTrackerTransactionMode.h */; };
62A456E52B0370E0003F19A1 /* SentryUIEventTrackerTransactionMode.m in Sources */ = {isa = PBXBuildFile; fileRef = 62A456E42B0370E0003F19A1 /* SentryUIEventTrackerTransactionMode.m */; };
62B86CFC29F052BB008F3947 /* SentryTestLogConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 62B86CFB29F052BB008F3947 /* SentryTestLogConfig.m */; };
62C25C862B075F4900C68CBD /* TestOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62C25C852B075F4900C68CBD /* TestOptions.swift */; };
62E081A929ED4260000F69FC /* SentryBreadcrumbDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 62E081A829ED4260000F69FC /* SentryBreadcrumbDelegate.h */; };
62E081AB29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E081AA29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift */; };
62F226B729A37C120038080D /* SentryBooleanSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = 62F226B629A37C120038080D /* SentryBooleanSerialization.m */; };
Expand Down Expand Up @@ -975,6 +976,7 @@
62A456E22B0370AA003F19A1 /* SentryUIEventTrackerTransactionMode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryUIEventTrackerTransactionMode.h; path = include/SentryUIEventTrackerTransactionMode.h; sourceTree = "<group>"; };
62A456E42B0370E0003F19A1 /* SentryUIEventTrackerTransactionMode.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryUIEventTrackerTransactionMode.m; sourceTree = "<group>"; };
62B86CFB29F052BB008F3947 /* SentryTestLogConfig.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryTestLogConfig.m; sourceTree = "<group>"; };
62C25C852B075F4900C68CBD /* TestOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestOptions.swift; sourceTree = "<group>"; };
62E081A829ED4260000F69FC /* SentryBreadcrumbDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryBreadcrumbDelegate.h; path = include/SentryBreadcrumbDelegate.h; sourceTree = "<group>"; };
62E081AA29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryBreadcrumbTestDelegate.swift; sourceTree = "<group>"; };
62F226B629A37C120038080D /* SentryBooleanSerialization.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryBooleanSerialization.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3218,6 +3220,7 @@
844EDC7829415AB300C86F34 /* TestSentrySystemWrapper.swift */,
844EDCE72947DCD700C86F34 /* TestSentryNSTimerFactory.swift */,
84B7FA3B29B2866200AD93B1 /* SentryTestUtils-ObjC-BridgingHeader.h */,
62C25C852B075F4900C68CBD /* TestOptions.swift */,
);
path = SentryTestUtils;
sourceTree = "<group>";
Expand Down Expand Up @@ -4501,6 +4504,7 @@
files = (
8431F01629B2851500D8DC56 /* TestSentryNSProcessInfoWrapper.swift in Sources */,
84B7FA4229B28CDE00AD93B1 /* TestCurrentDateProvider.swift in Sources */,
62C25C862B075F4900C68CBD /* TestOptions.swift in Sources */,
84B7FA3F29B28BAD00AD93B1 /* TestTransport.swift in Sources */,
84A5D75B29D5170700388BFA /* TimeInterval+Sentry.swift in Sources */,
84AC61D929F7643B009EEF61 /* TestDispatchFactory.swift in Sources */,
Expand Down
14 changes: 14 additions & 0 deletions SentryTestUtils/TestOptions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation
import Sentry

public extension Options {
func setIntegrations(_ integrations: [AnyClass]) {
self.integrations = integrations.map {
String(describing: $0)
}
}

func removeAllIntegrations() {
self.integrations = []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class SentryNSDataTrackerTests: XCTestCase {
super.setUp()
fixture = Fixture()
fixture.getSut().enable()
SentrySDK.start { $0.enableFileIOTracing = true }
SentrySDK.start {
$0.removeAllIntegrations()
}
}

override func tearDown() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,28 @@ class SentryScreenshotIntegrationTests: XCTestCase {
}

func test_attachScreenshot_disabled() {
SentrySDK.start { $0.attachScreenshot = false }
SentrySDK.start {
$0.attachScreenshot = false
$0.setIntegrations([SentryScreenshotIntegration.self])
}
XCTAssertEqual(SentrySDK.currentHub().getClient()?.attachmentProcessors.count, 0)
XCTAssertFalse(sentrycrash_hasSaveScreenshotCallback())
}

func test_attachScreenshot_enabled() {
SentrySDK.start { $0.attachScreenshot = true }
SentrySDK.start {
$0.attachScreenshot = true
$0.setIntegrations([SentryScreenshotIntegration.self])
}
XCTAssertEqual(SentrySDK.currentHub().getClient()?.attachmentProcessors.count, 1)
XCTAssertTrue(sentrycrash_hasSaveScreenshotCallback())
}

func test_uninstall() {
SentrySDK.start { $0.attachScreenshot = true }
SentrySDK.start {
$0.attachScreenshot = true
$0.setIntegrations([SentryScreenshotIntegration.self])
}
SentrySDK.close()

XCTAssertNil(SentrySDK.currentHub().getClient()?.attachmentProcessors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class SentryCrashIntegrationTests: NotificationCenterTestCase {
options.dsn = SentryCrashIntegrationTests.dsnAsString
options.releaseName = releaseName
options.dist = dist
options.setIntegrations([SentryCrashIntegration.self])
}

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

let userInfo = try XCTUnwrap(SentryDependencyContainer.sharedInstance().crashReporter.userInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,38 @@ class SentryViewHierarchyIntegrationTests: XCTestCase {
}

func test_attachViewHierarchy() {
SentrySDK.start { $0.attachViewHierarchy = false }
SentrySDK.start {
$0.attachViewHierarchy = false
$0.setIntegrations([SentryViewHierarchyIntegration.self])
}
XCTAssertEqual(SentrySDK.currentHub().getClient()?.attachmentProcessors.count, 0)
XCTAssertFalse(sentrycrash_hasSaveViewHierarchyCallback())
}

func test_attachViewHierarchy_enabled() {
SentrySDK.start { $0.attachViewHierarchy = true }
SentrySDK.start {
$0.attachViewHierarchy = true
$0.setIntegrations([SentryViewHierarchyIntegration.self])
}
XCTAssertEqual(SentrySDK.currentHub().getClient()?.attachmentProcessors.count, 1)
XCTAssertTrue(sentrycrash_hasSaveViewHierarchyCallback())
}

func test_uninstall() {
SentrySDK.start { $0.attachViewHierarchy = true }
SentrySDK.start {
$0.attachViewHierarchy = true
$0.setIntegrations([SentryViewHierarchyIntegration.self])
}
SentrySDK.close()
XCTAssertNil(SentrySDK.currentHub().getClient()?.attachmentProcessors)
XCTAssertFalse(sentrycrash_hasSaveViewHierarchyCallback())
}

func test_integrationAddFileName() {
SentrySDK.start { $0.attachViewHierarchy = true }
SentrySDK.start {
$0.attachViewHierarchy = true
$0.setIntegrations([SentryViewHierarchyIntegration.self])
}
saveViewHierarchy("/test/path")
XCTAssertEqual("/test/path/view-hierarchy.json", fixture.viewHierarchy.saveFilePathUsed)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class SentryCrashInstallationReporterTests: XCTestCase {
private func sdkStarted() {
SentrySDK.start { options in
options.dsn = SentryCrashInstallationReporterTests.dsnAsString
options.setIntegrations([SentryCrashIntegration.self])
}
let options = Options()
options.dsn = SentryCrashInstallationReporterTests.dsnAsString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class SentryStacktraceBuilderTests: XCTestCase {
options.dsn = TestConstants.dsnAsString(username: "SentryStacktraceBuilderTests")
options.swiftAsyncStacktraces = true
options.debug = true
options.setIntegrations([SentryCrashIntegration.self, SentrySwiftAsyncIntegration.self])
}

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

let waitForAsyncToRun = expectation(description: "Wait async functions")
Expand Down
21 changes: 20 additions & 1 deletion Tests/SentryTests/SentrySDKTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class SentrySDKTests: XCTestCase {
SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
options.maxBreadcrumbs = 0
options.setIntegrations([])
}

SentrySDK.addBreadcrumb(Breadcrumb(level: SentryLevel.warning, category: "test"))
Expand Down Expand Up @@ -123,6 +124,7 @@ class SentrySDKTests: XCTestCase {
func testStartStopBinaryImageCache() {
SentrySDK.start { options in
options.debug = true
options.removeAllIntegrations()
}

XCTAssertNotNil(SentryDependencyContainer.sharedInstance().binaryImageCache.cache)
Expand All @@ -136,6 +138,7 @@ class SentrySDKTests: XCTestCase {
func testStartWithConfigureOptions_NoDsn() throws {
SentrySDK.start { options in
options.debug = true
options.removeAllIntegrations()
}

let options = SentrySDK.currentHub().getClient()?.options
Expand All @@ -148,6 +151,7 @@ class SentrySDKTests: XCTestCase {
func testStartWithConfigureOptions_WrongDsn() throws {
SentrySDK.start { options in
options.dsn = "wrong"
options.removeAllIntegrations()
}

let options = SentrySDK.currentHub().getClient()?.options
Expand All @@ -164,6 +168,7 @@ class SentrySDKTests: XCTestCase {
wasBeforeSendCalled = true
return event
}
options.removeAllIntegrations()
}

SentrySDK.capture(message: "")
Expand All @@ -181,6 +186,7 @@ class SentrySDKTests: XCTestCase {
XCTAssertEqual(123, Dynamic(suggested).maxBreadcrumbs)
return scope
}
options.removeAllIntegrations()
}
XCTAssertEqual("me", SentrySDK.currentHub().scope.userObject?.userId)
XCTAssertIdentical(scope, SentrySDK.currentHub().scope)
Expand Down Expand Up @@ -412,7 +418,7 @@ class SentrySDKTests: XCTestCase {

func testInstallIntegrations_NoIntegrations() {
SentrySDK.start { options in
options.integrations = []
options.removeAllIntegrations()
}

assertIntegrationsInstalled(integrations: [])
Expand Down Expand Up @@ -491,6 +497,7 @@ class SentrySDKTests: XCTestCase {

SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
options.removeAllIntegrations()
}

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

SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
options.removeAllIntegrations()
}
XCTAssertTrue(SentrySDK.isEnabled)

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

SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
options.removeAllIntegrations()
}
XCTAssertTrue(SentrySDK.isEnabled)
}

func testClose_ResetsDependencyContainer() {
SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
options.removeAllIntegrations()
}

let first = SentryDependencyContainer.sharedInstance()
Expand All @@ -536,9 +546,12 @@ class SentrySDKTests: XCTestCase {
func testClose_ClearsIntegrations() {
SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
options.swiftAsyncStacktraces = true
options.setIntegrations([SentrySwiftAsyncIntegration.self])
}

let hub = SentrySDK.currentHub()
XCTAssertEqual(1, hub.installedIntegrations().count)
SentrySDK.close()
XCTAssertEqual(0, hub.installedIntegrations().count)
assertIntegrationsInstalled(integrations: [])
Expand All @@ -549,6 +562,7 @@ class SentrySDKTests: XCTestCase {
SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
options.tracesSampleRate = 1
options.removeAllIntegrations()
}

let appStateManager = SentryDependencyContainer.sharedInstance().appStateManager
Expand All @@ -557,6 +571,7 @@ class SentrySDKTests: XCTestCase {
SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
options.tracesSampleRate = 1
options.removeAllIntegrations()
}

XCTAssertEqual(appStateManager.startCount, 2)
Expand Down Expand Up @@ -606,6 +621,7 @@ class SentrySDKTests: XCTestCase {
func testClose_SetsClientToNil() {
SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
options.removeAllIntegrations()
}

SentrySDK.close()
Expand All @@ -616,6 +632,7 @@ class SentrySDKTests: XCTestCase {
func testClose_ClosesClient() {
SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
options.removeAllIntegrations()
}

let client = SentrySDK.currentHub().client()
Expand All @@ -627,6 +644,7 @@ class SentrySDKTests: XCTestCase {
func testClose_CallsFlushCorrectlyOnTransport() throws {
SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
options.removeAllIntegrations()
}

let transport = TestTransport()
Expand All @@ -641,6 +659,7 @@ class SentrySDKTests: XCTestCase {
func testFlush_CallsFlushCorrectlyOnTransport() throws {
SentrySDK.start { options in
options.dsn = SentrySDKTests.dsnAsString
options.removeAllIntegrations()
}

let transport = TestTransport()
Expand Down
1 change: 1 addition & 0 deletions Tests/SentryTests/SentryTests-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
#import "SentryScreenshot.h"
#import "SentryScreenshotIntegration.h"
#import "SentrySdkInfo.h"
#import "SentrySwiftAsyncIntegration.h"

#import "SentrySerialization.h"
#import "SentrySession+Private.h"
Expand Down

0 comments on commit 4d3df92

Please sign in to comment.