Skip to content

Commit

Permalink
update for swift testing
Browse files Browse the repository at this point in the history
  • Loading branch information
qyang-nj committed Oct 8, 2024
1 parent 9df0d2b commit 902556c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions articles/iOSTesting.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ UI tests let us test our app like the end user. It can mimic the user behaviors,

## xcodebuild test-without-building
A more common scenario is to use `xcodebuild test-without-build` to run `.xctest`. [Here](../testing/xcodebuild/run_test.py) is a script to show how this works.

## Swift Testing
3 changes: 3 additions & 0 deletions xctest/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# XCTest

> [!WARNING]
> This note is deprecated. See [Behind the scenes: iOS Testing](./articles/iOSTesting.md)
The word "xctest" is extensively used in the realm of iOS testing, the test bundle (`.xctest`), the test framework (`XCTest.framework`), and the test runner (`xctest`). This article along with a sample will dive into some details about iOS testing.

## Build and Run
Expand Down
9 changes: 4 additions & 5 deletions xctest/build_and_test.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/bin/zsh
# This script was tested on Xcode 14.0
# This script was tested on Xcode 16.0
set -e

# Change the target to arm64 to run tests natively on M1 machine
TARGET="x86_64-apple-ios14.0-simulator"
TARGET="arm64-apple-ios16.0-simulator"

SDKROOT=$(xcrun --show-sdk-path --sdk iphonesimulator)
PLATFORM_DIR="$(xcode-select -p)/Platforms/iPhoneSimulator.platform"
Expand All @@ -26,12 +25,12 @@ xcrun ld -bundle -o build/Test.xctest/Test build/Test.o \
-L "$PLATFORM_DIR/Developer/usr/lib" \
-F "$PLATFORM_DIR/Developer/Library/Frameworks" \
-F "$SDKROOT/System/Library/Frameworks" \
-lSystem
-lXCTestSwiftSupport

# To set environment variables in the simulator, use SIMCTL_CHILD_ prefix
# export SIMCTL_CHILD_DYLD_PRINT_ENV=1
# export SIMCTL_CHILD_DYLD_PRINT_LIBRARIES=1

xcrun simctl spawn --arch=$ARCH --standalone "iPhone 8" \
xcrun simctl spawn --arch=$ARCH --standalone "iPhone 16" \
"$PLATFORM_DIR/Developer/Library/Xcode/Agents/xctest" \
$(realpath build/Test.xctest)
26 changes: 23 additions & 3 deletions xctest/test.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@

import XCTest
import Testing

class Tests: XCTestCase {
func testExample() throws {
XCTAssert(true)
class XCTestDemo: XCTestCase {

func testMathOperations() {
let a = 5
let b = 3

XCTAssertEqual(a + b, 8)
XCTAssertEqual(a * b, 15)
XCTAssertEqual(a - b, 2)
}
}

struct SwiftTestingDemo {

@Test func verifyMathOperations() {
let a = 5
let b = 3

#expect(a + b == 8)
#expect(a * b == 15)
#expect(a - b == 2)
}

}

0 comments on commit 902556c

Please sign in to comment.