如何从XCUITest提取测试层级信息,以便在xcodebuild前自定义Scheme?
Hey there! Let's break down how to tackle your two XCUITest-related goals step by step—this should help you set up custom Schemes smoothly before running xcodebuild.
1. Get XCUITest Class Names & Test Method Names
You’ve got two reliable approaches here, depending on whether you want to work with compiled test bundles or raw source code:
Option 1: Use xcodebuild to List Tests Directly
The easiest way is to leverage xcodebuild’s built-in ability to list all tests without running them. Run this command in your project directory:
xcodebuild test-without-building \ -workspace YourApp.xcworkspace \ -scheme YourExistingTestScheme \ -destination 'platform=iOS Simulator,name=iPhone 15' \ -list-tests
This will output a structured list of all test suites (classes) and individual test methods, like this:
Test Suite 'YourTestTarget.xctest' started at 2024-05-20 12:00:00.001
Test Suite 'LoginTests' started at 2024-05-20 12:00:00.002
Test Case '-[LoginTests testSuccessfulLogin]' started.
Test Case '-[LoginTests testFailedLogin]' started.
...
You can pipe this output through grep or a simple script to extract class names (e.g., LoginTests) and method names (e.g., testSuccessfulLogin).
Option 2: Static Source Code Parsing
If you want to scan raw Swift/Objective-C files without compiling, use syntax analysis tools:
- Swift: Use
SwiftSyntaxto write a small utility that scans.swiftfiles, identifies classes inheriting fromXCTestCase, and extracts methods starting withtest(required naming convention for XCUITest methods). - Objective-C: Use
clangwith AST dumping (clang -Xclang -ast-dump -fsyntax-only YourTestFile.m) or a script to regex-match@interfacedeclarations inheriting fromXCTestCaseand methods starting with- (void)test.
2. Organize Test Hierarchy & Create Custom Schemes
Once you’ve got your test class/method names plus their folder structure, follow these steps to build custom Schemes:
Step 1: Map Test Hierarchy
First, tie each test class/method to its source folder. For example:
Tests/Authentication/LoginTests.swift→ Folder:Authentication, Class:LoginTests, Methods:testSuccessfulLogin,testFailedLoginTests/Checkout/PaymentTests.swift→ Folder:Checkout, Class:PaymentTests, Methods:testCreditCardPayment,testApplePayPayment
You can automate this with a shell script using find to scan your test directory, then pair file paths with the test names you extracted earlier.
Step 2: Create Custom Schemes
Schemes are just plist files under the hood—here’s how to build them:
Option A: Export & Modify a Template Scheme
- Open Xcode, create a temporary Scheme that targets your test bundle, then go to Product > Scheme > Manage Schemes.
- Select the Scheme, click Export, and save it as a
.xcschemefile. - Open the plist in a text editor, find the
TestActionsection, and modify theTestsarray to include only the test classes/methods you want (grouped by folder if needed). For example:<key>Tests</key> <array> <dict> <key>TestableReference</key> <dict> <key>TestableTargetName</key> <string>YourTestTarget</string> <key>Tests</key> <array> <dict> <key>TestIdentifier</key> <string>Class:LoginTests</string> </dict> <dict> <key>TestIdentifier</key> <string>Method:PaymentTests/testCreditCardPayment</string> </dict> </array> </dict> </dict> </array> - Import the modified
.xcschemefile back into Xcode via Manage Schemes > Import.
Option B: Automate Scheme Generation with Scripts
If you have lots of test groups, write a Python/Shell script that:
- Reads your mapped test hierarchy (folder → class → methods).
- Generates a valid
.xcschemeplist file programmatically. - Places the file in your project’s
xcshareddata/xcschemesdirectory (for shared Schemes) orxcuserdata/{YourUsername}.xcuserdatad/xcschemes(for local Schemes).
Step 3: Use the Custom Scheme with xcodebuild
Once your Scheme is set up, run tests with:
xcodebuild test \ -workspace YourApp.xcworkspace \ -scheme YourCustomScheme \ -destination 'platform=iOS Simulator,name=iPhone 15'
内容的提问来源于stack exchange,提问作者Confused

