【问题标题】:Passing arguments to iOS tests with xcodebuild使用 xcodebuild 将参数传递给 iOS 测试
【发布时间】:2016-11-21 14:41:01
【问题描述】:

我想通过命令行 (xcodebuild) 将命令行参数传递给我的 iOS 测试。我正在 XCode 上寻找与此设置等效的设置:

简单地将参数传递给 xcodebuild 是行不通的,例如:

xcodebuild -verbose test -workspace theworkspace.xcworkspace -scheme 'thescheme' -destination 'platform=iOS Simulator,name=iPhone 7' --argument=value

此问题与xcodebuild pass arguments to application on iOS 类似,但该问题的解决方案并不令人满意。

【问题讨论】:

标签: xcodebuild


【解决方案1】:

这是关于传递环境变量而不是命令行参数,但看起来它有可能在 Xcode 13 的 xcodebuild 中得到支持。来自release notes

xcodebuild 现在支持将某些环境变量传递给测试运行程序进程。在调用 xcodebuild 的环境中,为任何变量添加前缀 TEST_RUNNER_ 以将该变量(去除前缀)传递给 XCTest 测试运行程序进程。例如,运行 env TEST_RUNNER_Foo=Bar xcodebuild test ... 会导致在测试运行器的环境中设置环境变量 Foo=Bar。 (74104870)

【讨论】:

    【解决方案2】:

    为了补充@ManWithBear 的答案,我最终在脚本中执行了此操作:

    #Remove previous command line arguments
    /usr/libexec/PlistBuddy -c "Delete DetoxTestRunner:CommandLineArguments" "$TESTRUN" || true
    #Add an empty array
    /usr/libexec/PlistBuddy -c "Add DetoxTestRunner:CommandLineArguments array" "$TESTRUN"
    
    #Add script arguments as launch arguments of the test runner app
    for i in $*; do
      /usr/libexec/PlistBuddy -c "Add DetoxTestRunner:CommandLineArguments: string '$i'" "$TESTRUN"
    done
    

    在上面的代码中,我将传递给脚本的所有参数作为启动参数添加到测试器应用程序。 DetoxTestRunner 是测试方案/目标的名称。

    【讨论】:

      【解决方案3】:

      我没有找到“简单”的解决方案。因此,我将测试分为 3 个步骤:
      1.运行xcodebuild build-for-testing。它将在派生数据中生成xctestrun 文件,其中包含启动参数列表
      2. 在此处添加您想要的启动参数
      3.运行xcodebuild test-without-building -xctestrun <%path_to_file_here%>

      我为它写了脚本。它还需要一些改进,所以我会在不久的将来分享它的最终形式。

      编辑:从来没有时间更新脚本。所以这里是丑陋的版本,适合我们的需要。 https://gist.github.com/ManWithBear/57cbabc8dcd0193d156c376d2d23ff02

      【讨论】:

      • 非常有用的脚本!代替 Swift Plistbuddy 可能会更好,但它确实有效!
      • 是的,我尝试了 plistbuddy,但没有正确使用它,所以编写 swift 脚本要快得多。 ¯_(ツ)_/¯
      【解决方案4】:

      您也可以尝试在构建设置中创建preprocessor macros。 然后传递一个像这样的值(假设它被命名为 DARK_MODE):

      xcodebuild -project \
         -scheme  \
         -testPlan  \
         -destination \
         -derivedDataPath \
         DARK_MODE=NO \
         test
      

      下面是如何在测试中使用它:

      override func setUp() {
          super.setUp()
          if ProcessInfo.processInfo.environment["DARK_MODE"] == "YES" {
              SystemSettings.changeUIStyle(to: .dark)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-05-25
        • 2015-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-08
        相关资源
        最近更新 更多