Running Automated Tests via CI Pipelines: GitHub Actions

rozeri dilar
iOS App Mastery
Published in
3 min readAug 7, 2023

When it comes to testing, end-to-end tests may not provide immediate feedback compared to unit tests. However, by incorporating a solution that runs both unit and end-to-end tests through an automated continuous integration (CI) pipeline, we can streamline the development process. This approach ensures that the team does not have to wait for all tests to be completed, promotes developer happiness, and strengthens the overall integrity of our codebase.

Choosing a CI Server: To implement a CI pipeline tailored to your team’s needs, it’s crucial to select the right CI server. While there are various options available, let’s explore an example using GitHub Actions.

Setting Up GitHub Actions:

  1. Go to the “Actions” tab on your GitHub repository.
  2. Select the Swift file for your project.
  3. Add a new file named .github/workflows/swift.yml to your main branch.

Configuring the CI Pipeline:

Here is an example: https://github.com/rozeridilar/Example-App-Data-Races-iOS/blob/main/.github/workflows/swift.yml
https://github.com/rozeridilar/Example-App-Data-Races-iOS/actions

Once you have created the swift.yml file, you need to write a script that calls the xcodebuild command-line tool to build the code in your new scheme. You can first test the script on your local machine, and then add it to the swift.yml file. Below is an example of how the swift.yml file should look:

name: CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build-and-test:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: Select Xcode
run: sudo xcode-select -s /Applications/Xcode.app
- name: Build and test
run: xcodebuild clean build test -project EssentialFeed/EssentialFeed.xcodeproj -scheme "CI" -destination 'platform=iOS Simulator,name=iPhone 14' CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO

In the example above, we define a job named “build-and-test” that runs whenever there is a push or pull request to the main branch. This job executes the following steps:

  1. Checks out the repository using actions/checkout@v3.
  2. Selects Xcode using sudo xcode-select -s /Applications/Xcode.app.
  3. Builds and tests the project using the xcodebuild command. The script cleans the project first, specifies the project file and scheme (in this case, "CI"), and omits code signing since this is a CI pipeline. The destination specifies the iOS simulator device for testing.

Benefits of CI Pipeline: By incorporating a CI pipeline, we gain confidence that our end-to-end tests won’t break any modules. It is essential to establish the infrastructure for the CI pipeline from day one. Every pull request triggers a build with the full unit and end-to-end tests. If everything passes successfully, we allow the changes to be merged into the main branch. It’s crucial to merge to the main branch frequently, avoiding long-lived branches.

Continuous Deployment:

Taking it a step further, merges to the main branch can also trigger automated releases through continuous deployment. Depending on your setup, the master branch can be deployed to platforms such as Firebase, TestFlight, or even the official app store. This seamless integration allows for the rapid and reliable distribution of updates to users.

Implementing automated tests via CI pipelines, such as GitHub Actions, brings numerous advantages to the development process. By automating the testing process, we can ensure code integrity, improve team efficiency, and enable continuous deployment

References

https://rozeridilar.com/2023/07/30/running-automated-tests-via-ci-pipelines-github-actions/

https://www.essentialdeveloper.com/articles/s02e21-migrating-to-swift-5-2-and-ci-with-github-actions-professional-ios-engineering-series

https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/08-automation.html

https://developer.apple.com/videos/play/wwdc2018/403/

--

--