如何使用xcodebuild -only-testing执行Testing框架测试?
如何用xcodebuild -only-testing指定执行Testing框架的测试用例?
项目中的两类测试用例
1. 基于XCTest的测试
import XCTest @testable import MyApp final class MyAppXCTests: XCTestCase { func testXCToggleOne() throws { XCTAssertTrue(true) } }
2. 基于Testing框架的测试
import Testing @testable import MyApp struct MyAppTests { @Test func one() async throws { #expect(true) } }
对应的测试链接分别是:
- XCTest测试:
test://com.apple.xcode/MyApp/MyAppTests/MyAppXCTests/testOne - Testing框架测试:
test://com.apple.xcode/MyApp/MyAppTests/MyAppTests/one()
执行测试遇到的问题
成功执行XCTest测试的命令
使用xcodebuild可以正常执行XCTest的测试用例:
xcodebuild \ -project ./MyApp.xcodeproj \ -scheme MyApp \ -destination "platform=iOS Simulator,name=iPhone 16,OS=26.0" \ -enableCodeCoverage YES \ -only-testing:MyAppTests/MyAppXCTests/testOne \ test
执行Testing框架测试的失败尝试
- 省略括号,无测试用例执行:
xcodebuild \ -project ./MyApp.xcodeproj \ -scheme MyApp \ -destination "platform=iOS Simulator,name=iPhone 16,OS=26.0" \ -enableCodeCoverage YES \ -only-testing:MyAppTests/MyAppTests/one \ test
- 直接加括号,触发bash语法错误:
xcodebuild \ -project ./MyApp.xcodeproj \ -scheme MyApp \ -destination "platform=iOS Simulator,name=iPhone 16,OS=26.0" \ -enableCodeCoverage YES \ -only-testing:MyAppTests/MyAppTests/one() \ test
- 添加
test前缀,找不到测试用例:
xcodebuild \ -project ./MyApp.xcodeproj \ -scheme MyApp \ -destination "platform=iOS Simulator,name=iPhone 16,OS=26.0" \ -enableCodeCoverage YES \ -only-testing:MyAppTests/MyAppTests/testOne \ test
正确的执行命令
要解决这个问题,需对包含括号的测试名称进行转义或用引号包裹,避免bash解析括号时出错。以下两种方式均有效:
方式一:转义括号
xcodebuild \ -project ./MyApp.xcodeproj \ -scheme MyApp \ -destination "platform=iOS Simulator,name=iPhone 16,OS=26.0" \ -enableCodeCoverage YES \ -only-testing:MyAppTests/MyAppTests/one\(\) \ test
方式二:用引号包裹整个-only-testing参数
xcodebuild \ -project ./MyApp.xcodeproj \ -scheme MyApp \ -destination "platform=iOS Simulator,name=iPhone 16,OS=26.0" \ -enableCodeCoverage YES \ "-only-testing:MyAppTests/MyAppTests/one()" \ test
另外,也可以指定测试目标批量执行Testing框架的测试,比如运行整个MyAppTests测试结构下的所有用例:
xcodebuild \ -project ./MyApp.xcodeproj \ -scheme MyApp \ -destination "platform=iOS Simulator,name=iPhone 16,OS=26.0" \ -enableCodeCoverage YES \ -only-testing:MyAppTests/MyAppTests \ test
内容的提问来源于stack exchange,提问作者user2958279
相关产品推荐
相关产品推荐

