在MacOS上测试IOS应用程序

本文档介绍如何在以下部分中使用Circleci设置和自定义用于IOS应用程序的测试:

概述

Circleci为MacOS虚拟机中的IOS项目提供支持。提供的每个图像都有一组安装了一组公共工具,例如Ruby和OpenJDK,与Xcode的版本一起。有关提供的图像的更多信息,请参阅software manifest对于每个Xcode映像。

There is documentation foriOS示例项目and入门麦斯科斯

使用MacOS执行程序

Each苹果系统job is run a fresh virtual machine, running a specified version macOS. We build a new image each time a new stable, or beta, version of Xcode is released by Apple and aim to get these deployed as soon as possible. Generally, the contents of a particular build image will remain unchanged, except in very exceptional circumstances we might be forced to re-build a container for a specific reason. Our goal is to keep your build environment stable, and to allow you to opt-in to newer containers by setting thexcodekey in yourconfig.yml文件。

Periodically, we will update the version of macOS each image includes to ensure the build environment is as up to date as possible. When a new major version of macOS is released, we will generally switch to this once the new major version of Xcode reaches thexx.2释放以确保构建环境保持稳定。

我们宣布提供新的MacOS容器,包括Xcode Betas我们讨论网站的Annuecements部分

Beta image support

We endeavour to make beta Xcode versions available on the macOS executor as soon as we can to allow developers to test their apps ahead of the next stable Xcode release.

Unlike our stable images (which are frozen and will not change), once a new beta image is released it will overwrite the previous beta image until a GM (stable) image is released, at which point the image is frozen and no longer updated. If you are requesting an image using an Xcode version that is currently in beta, please expect it to change when Apple releases a new Xcode beta with minimal notice. This can include breaking changes in Xcode/associated tooling which are beyond our control.

要阅读关于我们的客户支持政策,请查看以下内容support center article

Apple Silicon支持

Please Note:苹果公司表示,苹果硅开发者118金宝博娱乐城should continue to use Xcode 12 beta 6, rather than the GM. We have retained this image and you can access it by requesting the12.0.0-beta图像。

可以使用Xcode构建Apple Silicon / Universal二进制文件12.0.0-betaApple提供Intel(x86_64) and Apple Silicon (ARM64.) toolchains in this release. Cross-compiling Apple Silicon binaries on Intel hosts has an additional overhead and as a result compilation times will be longer than native compilation for Intel.

运行或测试Apple Silicon应用程序当前无法作为Circleci构建主机是基于英特尔的Mac。二进制文件需要出口伪影for testing apps locally.

支持的Xcode版本

配置 Xcode Version MacOS版本 支持MacOS UI测试 软件清单 发行说明
12.3.0 Xcode 12.3 (12C33) 10.15.5 是的 Installed software 发行说明
12.2.0 Xcode 12.2 (12B45b) 10.15.5 是的 Installed software 发行说明
12.1.1 Xcode 12.1.1 RC(12a7605b) 10.15.5 是的 Installed software 发行说明
12.1.0 Xcode 12.1 (12A7403) 10.15.5 是的 Installed software 发行说明
12.0.1. Xcode 12.0.1(12A7300) 10.15.5 是的 Installed software 发行说明
11.7.0. Xcode 11.7 (11E801a) 10.15.5 是的 Installed software 发行说明
11.6.0 Xcode 11.6 (11E708) 10.15.5 No Installed software 发行说明
11.5.0. Xcode 11.5 (11E608c) 10.15.4 No Installed software 发行说明
11.4.1 Xcode 11.4.1(11E503A) 10.15.4 No Installed software 发行说明
11.3.1 Xcode 11.3.1(11C505) 10.15.1 No Installed software 发行说明
11.2.1. Xcode 11.2.1(11b500) 10.15.0 No Installed software 发行说明
11.1.0 Xcode 11.1 (11A1027) 10.14.4 No Installed software 发行说明
11.0.0 Xcode 11.0 (11A420a) 10.14.4 No Installed software 发行说明
10.3.0 Xcode 10.3 (10G8) 10.14.4 No Installed software 发行说明
10.2.1 Xcode 10.2.1(10E1001) 10.14.4 No Installed software 发行说明
10.1.0 Xcode 10.1(10B61) 10.13.6. No Installed software 发行说明
10.0.0. Xcode 10.0(10a255) 10.13.6. No Installed software 发行说明
9.4.1 Xcode 9.4.1 (9F2000) 10.13.3. No Installed software

入门

选择您想要从中构建的麦斯科斯项目存储库Add ProjectsCircleci应用程序的页面。你需要确保你有一个计划允许麦斯科斯建立的计划, or if your project is open source, you can申请特别计划免费每月建立信用。

We highly recommend usingFastlane在Circleci中构建和签署您的应用程序。Fastlane在大多数情况下需要最少的配置,并简化构建测试部署过程。

Setting up your Xcode project

在设置Circleci上的项目后,您需要确保您打算使用Fastlane构建的方案标记为Xcode项目中的“共享”。在Xcode创建的大多数新项目中,默认方案将被标记为“共享”。要验证此操作或共享现有方案,请完成以下步骤:

  1. In Xcode, choose Product -> Scheme -> Manage Schemes
  2. 选择要共享的方案的“共享”选项,然后单击“关闭”
  3. 确保myproject.xcodeproj / xcshareddata / xcschemes将检入到Git存储库中的目录并按下更改

简单的项目应以最小的配置运行。您可以找到最小配置的示例iOS Project Tutorial

Using Fastlane

Fastlaneis a set of tools for automating the build and deploy process of mobile apps. We encourage the use of Fastlane on CircleCI as it simplifies the setup and automation of the build, test and deploy process. Additionally, it allows parity between local and CircleCI builds.

添加Gemfile.

It is recommended to add a吉他美to your repository to make sure that the same version of Fastlane is used both locally and on CircleCI and that all dependencies are installed. Below is a sample of a simple吉他美:

# Gemfilesource“https://rubygems.org”宝石'快车道'

创建了一个后吉他美在本地,您需要运行捆绑安装并检查两者吉他美and吉他美。lockinto your project repository.

设置Fastlane以用于Circleci

在Circleci项目中使用Fastlane时,我们建议将以下内容添加到您的开头Fastfile:

#fastlane / fastfile。。。平台:iO.dobefore_alldosetup_circle_ci.结尾。。。结尾

Thesetup_circle_ci.Fastlane行动必须在before_all块以执行以下操作:

  • 创建一个新的临时钥匙串,以便与Fastlane匹配一起使用(有关详细信息,请参阅代码签名部分)。
  • 切换Fastlane匹配readonlymode to make sure CI does not create new code signing certificates or provisioning profiles.
  • 设置日志和测试结果路径以易于收集。

在Circleci上使用Fastlane的示例配置

可以在Circleci上使用的基本Fastlane配置如下:

#fastlane / fastfiledefault_platform.:iO.平台:iO.dobefore_alldosetup_circle_ci.结尾去世"Runs all the tests"lane:testdoscan结尾去世"Ad-hoc build"lane:特设do比赛(类型:“特设”)gym(export_method:"ad-hoc")结尾结尾

此配置可以与以下Circleci配置文件一起使用:

# .circleci/config.ymlversion:2.1jobs:构建和测试:苹果系统:xcode:11.3.0环境:fl_output_dir.:输出FASTLANE_LANE:test脚步:-退房-run:捆绑安装-run:姓名:Fastlane命令:Bundle Exec FastLane $ FastLane_lane-Store_Arifacts.:path:输出-store_test_results.:path:输出/扫描特设:苹果系统:xcode:11.3.0环境:fl_output_dir.:输出FASTLANE_LANE:特设脚步:-退房-run:捆绑安装-run:姓名:Fastlane命令:Bundle Exec FastLane $ FastLane_lane-Store_Arifacts.:path:输出workflows:build-test-adhoc:jobs:-构建和测试-特设:过滤器:分支机构:只要:发展需要:-构建和测试

The environment variablefl_output_dir.是Fastlane日志和签名的工件目录.IPA.file should be stored. Use this to set the path in theStore_Arifacts.步骤自动保存日志并从Fastlane构建文物。

使用Fastlane匹配的代码

We recommend the use of Fastlane Match for signing your iOS applications as it simplifies and automates the process of code signing both locally and in the CircleCI environment.

有关如何开始使用Fastlane匹配的更多信息,请查看我们的iOS代码签名文档

使用Ruby.

我们的MacOS图像包含多个版本的Ruby。所有图像中使用的默认版本是系统Ruby。图像还包括图像构建时最新稳定版本的Ruby。我们使用该确定Ruby的稳定版本ruby-lang.org下载页面。The versions of Ruby that are installed in each image are listed in thesoftware manifests of each container

如果您想在清单中列出列为“可用于Charuby”的Ruby版本的步骤,则可以使用Chruby.这样做。

Note:由于系统目录上强制执行的限制权限,不建议使用系统Ruby安装Gems。作为一般规则,我们建议使用由Chruby提供的所有替代石宝进行所有工作。

使用Xcode 11.7及更高版本的图像

由于Macos System Ruby(2.6.3)与各种宝石(特别是那些需要本机扩展),Xcode 11.7和以后的图像默认到Ruby 2.7 Via的越来越不兼容Chruby.

Defaulting to Ruby 2.7 allows for greater compatibility and reliability with gems moving forward. Common gems, such as Fastlane, run without any issues in Ruby 2.7.

切换到另一个Ruby版本,请参阅our chruby documentation。To revert back to the system Ruby, add the following to the beginning of your job:

#...run:姓名:设置Ruby版本命令:Echo'Thuby System'>>〜/ .bash_profile

Images using Xcode 11.2 and later

TheChruby.程序安装在图像上,可用于选择Ruby的版本。默认情况下,不会启用自动切换功能。要选择Ruby的版本要使用,请添加Chruby.function to~/.bash_profile:

#...run:姓名:设置Ruby版本命令:echo 'chruby ruby-2.6' >> ~/.bash_profile

代替2.6随着Ruby的版本所必需的 - 您无需指定完整的Ruby版本,2.6。5例如,只是主要版本。这将确保在切换到可能具有稍微较新的Ruby版本的更新图像时,您的配置不会破坏。

Images using Xcode 11.1 and earlier

使用MacOS 10.14及更早版本(Xcode 11.1及更早版本)的图像都有两者Chruby.andthe auto-switcher默认启用。

要指定要使用的Ruby版本,有两个选项。你可以创建一个名为的文件。ruby-versionand commit it to your repository, as documented byChruby.

If you do not want to commit a。ruby-version文件到源控件,然后您可以从作业步骤创建文件:

#...run:姓名:设置Ruby版本命令:echo“Ruby-2.4”>〜/ .Ruby版本

代替2.4随着Ruby的版本所必需的 - 您无需指定完整的Ruby版本,2.4.9例如,只是主要版本。这将确保在切换到可能具有稍微较新的Ruby版本的更新图像时,您的配置不会破坏。

安装额外的Ruby版本

要使用未预先安装的Ruby版本运行作业,必须安装所需的Ruby版本。我们使用ruby-installtool to install the required version. After the install is complete, you can select it using the appropriate technique above.

使用Cocoapods和其他Ruby Gems的自定义版本

To make sure the version of CocoaPods that you use locally is also used in your CircleCI builds, we suggest creating a Gemfile in your iOS project and adding the CocoaPods version to it:

source'https://rubygems.org'宝石'cocoapods','= 1.3.0'

Then you can install these using bundler:

脚步:-restore_cache.:key:1-gems-{{ checksum "Gemfile.lock" }}-run:捆绑检查||捆绑安装--Path供应商/捆绑--Clean-save_cache.:key:1-gems-{{ checksum "Gemfile.lock" }}path:-供应商/捆绑

然后,您可以通过前缀命令确保您使用这些。bundle exec:

#...脚步:-run:bundle exec pod install

Using Homebrew

Homebrewis pre-installed on CircleCI, so you can simply use酿造型安装要添加您需要的几乎任何依赖性来完成构建。例如:

#...脚步:-run:姓名:Install cowsay命令:酿造型安装cowsay-run:姓名:Cowsay Hi.命令:Cowsay嗨!

It is also possible to use thesudo命令if necessary to perform customizations outside of Homebrew.

配置uring deployment

在测试和签名应用程序后,您已准备好将部署配置为您的选择服务,例如App Store Connect或TestFlight。有关如何部署到各种服务的更多信息,包括示例Fastlane配置,请查看部署IOS应用指南

Reducing job time and best practises

预启动模拟器

在构建应用程序之前预先启动IOS模拟器,以确保在时间上启动模拟器。这样做通常会减少构建中观察到的模拟器超时的数量。

To pre-start the simulator, add the following to yourconfig.ymlfile, assuming that you are running your tests on an iPhone 11 Pro simulator with iOS 13.2:

#...脚步:-run:姓名:pre-start simulator命令:XCrun Instruments -W“iPhone 11 Pro(13.3)[”||true

Note:the[特征是唯一标识iPhone 7模拟器的必要条件,因为手机+手表模拟器也存在于构建图像中:

  • 苹果手机11 Pro (13.3) []对于iPhone模拟器。
  • iPhone 11 Pro(13.3)+ Apple Watch系列5 - 40mm(6.1.1)[]for the phone + watch pair.

收集IOS模拟器崩溃报告

Often if yourscanstep fails, for example due to a test runner timeout, it is likely that your app has crashed during the test run. In such cases, collecting crash report is useful for diagnosing the exact cause of the crash. Crash reports can be uploaded as artifacts, as follows:

脚步:#...-Store_Arifacts.:path:〜/ library / logs / diagnosticreports

优化Fastlane.

默认情况下,Fastlane Scan会生成测试输出报告HTML.andjunit.formats. If your tests are taking a long time and you do not need these reports, consider disabling them by altering theoutput_type.参数如下所述Fastlane Docs.

优化Cocoapods.

In addition to the basic setup steps, it is best practice to use Cocoapods 1.8 or newer which allows the use of the CDN, rather than having to clone the entire Specs repo. This will allow you to install pods faster, reducing build times. If you are using Cocoapods 1.7 or older, consider upgrading to 1.8 or newer as this change allows for much faster job execution of thePOD安装step.

要启用此功能,请确保Podfile中的第一行如下:

source 'https://cdn.cocoapods.org/'

If upgrading from Cocoapods 1.7 or older, additionally ensure the following line is removed from your Podfile, along with removing the “Fetch CocoaPods Specs” step in your CircleCI Configuration:

源'https://github.com/cocoapods/specs.git'

要将CocoApod更新为最新稳定版本,只需使用以下命令更新Ruby Gem:

sudo gem安装cocoapods

我们还建议您检查一下Pods directory into source control。This will ensure that you have a deterministic, reproducible build.

Note:The previous S3 mirror we provided for the Cocoapods Spec repo is no longer being maintained or updated since the release of Cocoapods 1.8. It will remain available to prevent existing jobs breaking, we highly recommend switching to the CDN method described above.

Optimizing Homebrew

默认情况下,Homebrew将在任何操作开始时检查更新。随着自制的发布周期,这意味着任何呼叫的步骤酿造can take some extra time to complete.

If build speed, or bugs introduced by new Homebrew updates are a concern, this automatic update feature can be disabled. On average, this can save up to 2-5 minutes per job.

To disable this feature, define thehomebrew_no_auto_update.您工作中的环境变量:

version:2.1jobs:构建和测试:苹果系统:xcode:11.3.0环境:homebrew_no_auto_update.:1脚步:-退房-run:酿造型安装wget

支持ed build and test tools

使用Circleci上的MacOS执行程序,可以根据需要自定义构建以满足几乎任何IOS构建和测试策略。

Common test tools

已知以下常见的测试工具在Circleci上运行良好:

React Native projects

React Native projects can be built on CircleCI using苹果系统andDocker.executor types. For an example of configuring a React Native project, please seeour demo React Native application

Creating aconfig.ymlFile

自定义构建的最灵活的方法是修改项目的Circleci配置.circleci / config.yml.。This allows you to run arbitrary bash commands as well as utilise built-in features such as workspaces and caching. See the配置Circleci.文档以了解结构的详细描述config.yml文件。

使用多个执行程序类型(MacOS + Docker)

可以使用多个executor types在相同的工作流程中。在以下示例中,每个推送iOS项目将在麦斯卡斯(和其他IOS工具)内置(SwiftLintand危险)将在Docker中运行。

version:2.1jobs:构建和测试:苹果系统:xcode:11.3.0环境:fl_output_dir.:输出脚步:-退房-run:姓名:Install CocoaPods命令:Pod安装 --run:姓名:构建和运行测试命令:Fastlane扫描环境:SCAN_DEVICE:苹果手机8scan_scheme.:媒体-store_test_results.:path:输出/扫描-Store_Arifacts.:path:输出swiftlint.:Docker.:-image:bytesguy/swiftlint:latest验证:用户名:mydockerhub-user.password:$ dockerhub_password.# context / project UI env-var reference脚步:-退房-run:swiftlint.lint --reporter junit | tee result.xml-Store_Arifacts.:path:结果.xml.-store_test_results.:path:结果.xml.danger:Docker.:-image:bytesguy/danger:latest验证:用户名:mydockerhub-user.password:$ dockerhub_password.# context / project UI env-var reference脚步:-退房-run:dangerworkflows:构建 - 测试棉绒:jobs:-swiftlint.-danger-构建和测试

Troubleshooting

If you are facing build failures while executing your jobs, check out our支持中心知识库for answers to common issues.

也可以看看