You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从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 SwiftSyntax to write a small utility that scans .swift files, identifies classes inheriting from XCTestCase, and extracts methods starting with test (required naming convention for XCUITest methods).
  • Objective-C: Use clang with AST dumping (clang -Xclang -ast-dump -fsyntax-only YourTestFile.m) or a script to regex-match @interface declarations inheriting from XCTestCase and 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, testFailedLogin
  • Tests/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

  1. Open Xcode, create a temporary Scheme that targets your test bundle, then go to Product > Scheme > Manage Schemes.
  2. Select the Scheme, click Export, and save it as a .xcscheme file.
  3. Open the plist in a text editor, find the TestAction section, and modify the Tests array 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>
    
  4. Import the modified .xcscheme file 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:

  1. Reads your mapped test hierarchy (folder → class → methods).
  2. Generates a valid .xcscheme plist file programmatically.
  3. Places the file in your project’s xcshareddata/xcschemes directory (for shared Schemes) or xcuserdata/{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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:06:35