-
Core Data: Improvement of Singleton pattern by creating
testing
static variable.struct PersistentStore { static var shared: PersistentStore static var testing: PersistentStore }
-
UserDefaults: Subclass to create mock
class MockUserDefaults: UserDefaults { }
-
Networking Layer: Protocol + Mock class
protocol DataServiceProtocol { } class MockService: DataServiceProtocol { }
-
Other features
- Test plans
- UnitTests.xctestplan: Unit tests only
- FullTests.xctestplan: Unit tests + UI test
- Code coverage
- Test plans
- Build generate Build products & .xctestrun manifest file
xcodebuild build-for-testing -scheme XCTestMashup -destination 'platform=iOS Simulator,OS=16.4,name=iPhone 14'
- Test using the above generated files to create Result Bundle. (Need to enter path)
xcodebuild test-without-building -xctestrun "ENTER_PATH_HERE_FOR_XCTESTRUN Xcode/DerivedData/XCTest_Mashup.xctestrun" -destination 'platform=iOS Simulator,OS=16.4,name=iPhone 14'
- Display JSON format of Result Bundle containing Build log, Test report, Code coverage report, Test attachments
xcrun xcresulttool get --path ResultBundle.xcresult --format json
The above steps could be used as the main steps for CI
xcodebuild test -project XCTestMashup.xcodeproj -scheme XCTestMashup -destination 'platform=iOS Simulator,OS=16.4,name=iPhone 14'
xcodebuild test -scheme XCTestMashup -destination 'platform=iOS Simulator,OS=16.4,name=iPhone 14' -destination 'platform=iOS Simulator,OS=16.4,name=iPad (10th generation)'
xcodebuild test -scheme XCTestMashup -resultBundlePath $PWD/testresult/ResultBundle.xcresult -destination 'platform=iOS Simulator,OS=16.4,name=iPhone 14'
xcrun xccov view --report ResultBundle.xcresult