|
| 1 | +--- |
| 2 | +title: "iOS Mobile Testing: Ease your life at work with Fastlane" |
| 3 | +excerpt: "This is the story how we use fastlane in CI pipeline for iOS" |
| 4 | +tags: Mobile iOS testing |
| 5 | +authors: |
| 6 | +- Dilshan Fernando |
| 7 | +header: |
| 8 | + teaser: /assets/images/post/ios_fastlane.png |
| 9 | + teaser_alt: Ease your life at work with Fastlane |
| 10 | +category: Software Quality Assurance |
| 11 | +--- |
| 12 | + |
| 13 | + |
| 14 | +# As a software Tester, are you curious in finding the solutions to the below questions? (for iOS) |
| 15 | + - How to reduce test execution time by running in parallel on physical devices and simulators? |
| 16 | + - How to generate test reports with screenshots and run tests via a command-line interface? |
| 17 | + - How development team can do their code analysis at early? |
| 18 | + - How can we execute Unit test before execute UI tests? |
| 19 | + - Finally, How can we include all of the above in Continuous Integration(CI) pipeline? |
| 20 | + |
| 21 | +# Solutions we have introduced in Continuous Integration(CI) pipeline |
| 22 | + |
| 23 | +<figure> |
| 24 | + <img src="/assets/images/post/ios_ci_process.png" > |
| 25 | +</figure> |
| 26 | + |
| 27 | +# Getting started 🎬 |
| 28 | + |
| 29 | +<p align="justify"> |
| 30 | +Fastlane is the easiest way to automate beta deployments and releases for your iOS and Android apps. 🚀 It handles all tedious tasks, like generating screenshots, dealing with code signing, and releasing your application. |
| 31 | +</p> |
| 32 | + |
| 33 | +## Fastlane for iOS 🍎 |
| 34 | + |
| 35 | +fastlane can be installed in multiple ways. The preferred method is with **[Bundler](https://bundler.io/)**. fastlane can also be installed directly through Homebrew (if on macOS). It is possible to use macOS's system Ruby, but it's not recommended, as it can be hard to manage dependencies, and causes conflicts. |
| 36 | + |
| 37 | +Inorder to install, please follow the steps mentioned on the official site of **[fastlane](https://docs.fastlane.tools/getting-started/ios/setup/)** |
| 38 | + |
| 39 | +Now we have Fastlane tools installed. You should have `fastlane` directory in your project with `Appfile` and `Fastfile` created. |
| 40 | + |
| 41 | +# Executing tests via command line ⌨️ |
| 42 | + |
| 43 | +In order to run tests on the Continuous Integration Server, we need to execute them via a command-line interface. You will find multiple approaches for this task, in this tutorial we'll be using the Fastlane Scan action. |
| 44 | + |
| 45 | +Let's set it up: |
| 46 | + - Navigate to created fastlane directory `cd fastlane/` |
| 47 | + - Open `Fastfile` in text editor |
| 48 | + - Set scheme according to your project scheme name |
| 49 | + - Specify test device |
| 50 | + |
| 51 | +```kotlin |
| 52 | +lane :run_ui_tests do |
| 53 | + scan( |
| 54 | + scheme: 'ExampleFastLaneiOSUITests', # Project scheme name |
| 55 | + clean: true, # Clean project folder before test execution |
| 56 | + device: 'iPhone 8 plus' # Simulator for testing |
| 57 | + ) |
| 58 | +end |
| 59 | +``` |
| 60 | +Well done, we have successfully configured our project to execute tests and Fastlane tools will do the rest for us. To run tests: |
| 61 | + |
| 62 | +- Execute `fastlane run_ui_tests` |
| 63 | +- Wait for tests to be executed |
| 64 | + |
| 65 | +<figure> |
| 66 | + <img src="/assets/images/post/test_execution_in_simulator.gif" style="width:800px;height:400px;"> |
| 67 | + <figcaption>Test execution in Simulator.</figcaption> |
| 68 | +</figure> |
| 69 | + |
| 70 | +You will find HTML and Junit reports generated in `fastlane/test_output` directory. |
| 71 | + |
| 72 | +# Improving Test Reports 📖 |
| 73 | + |
| 74 | +Fastlane allows generating only simple test reports which do not include screenshots and device logs. When building the Test Automation framework we need to make sure, that the test report has enough information, so we don’t have to re-run tests manually and can analyze test failures more efficiently |
| 75 | + |
| 76 | +The better approach is to use **[XCTestHTMLReport](https://github.com/XCTestHTMLReport/XCTestHTMLReport)**. |
| 77 | + |
| 78 | +When we have it successfully installed the next step will be to add a helper method, so we can generate a report as a part of the test: |
| 79 | + |
| 80 | +```kotlin |
| 81 | +desc "Generate test reports" |
| 82 | + def generate_uiTestreport |
| 83 | + puts "Generating Test Report ..." |
| 84 | + sh 'xchtmlreport -r test_output/ExampleFastLaneiOSUITests.xcresult -i' |
| 85 | + puts "Test Report successfully generated" |
| 86 | + end |
| 87 | +``` |
| 88 | + |
| 89 | +# Running tests in parallel on multiple devices |
| 90 | + |
| 91 | +When testing Mobile Applications we need to verify that the Application works as expected on all supported devices and OS versions. Fortunately, we can automate this part with minimal effort. Parallel testing on simulators can be configured in Fastfile, we need to specify devices for testing |
| 92 | + |
| 93 | +```kotlin |
| 94 | +TEST_SIMULATORS = ['iPhone 8','iPhone SE (3rd generation)','iPad mini (6th generation)'] |
| 95 | +lane :uiTest do |
| 96 | + scan( |
| 97 | + scheme: 'ExampleFastLaneiOSUITests', # Project scheme name |
| 98 | + clean: true, # clean project folder before test execution |
| 99 | + devices: TEST_SIMULATORS |
| 100 | + ) |
| 101 | +end |
| 102 | +``` |
| 103 | + |
| 104 | +<figure> |
| 105 | + <img src="/assets/images/post/parallel_test_execution_simulators.gif" style="width:800px;height:400px;"> |
| 106 | + <figcaption>Parallel Test Execution on Simulators.</figcaption> |
| 107 | +</figure> |
| 108 | + |
| 109 | +Unfortunately, testing on simulators won’t be always the best approach, cause some issues won’t be appearing. Testing on real devices will provide us with more accurate test results. Also, we need to disable Auto-Lock so that the device will always be ready for test execution: |
| 110 | + |
| 111 | +- Navigate to the Display & Brightness section |
| 112 | +- Set Auto-Lock to Never |
| 113 | + |
| 114 | +To make sure, that system notification won’t affect test execution we need to enable Don’t disturb mode : |
| 115 | + |
| 116 | +- Navigate to the Do Not Disturb section |
| 117 | +- Enable Do Not Disturb mode |
| 118 | +- Set Silence to always |
| 119 | + |
| 120 | +The device is configured for test execution now. To execute tests on real devices we need to specify UDID. You can get it in multiple ways, for example, **[this](https://www.itexico.com/blog/find-unique-device-identifier-udid-on-the-iphone)**. Then we just need to set the physical device as the destination for testing. |
| 121 | + |
| 122 | +```kotlin |
| 123 | +REAL_DEVICES = [ |
| 124 | + 'platform=iOS,id=2a31ef65jc84c16er657af3f29901c20917g37', |
| 125 | + 'platform=iOS,id=78a91ef5bf2036fa49ec3df1af356jh5676390' |
| 126 | + ] |
| 127 | + |
| 128 | +private_lane :real_device_test do |
| 129 | + scan( |
| 130 | + scheme: 'ExampleFastLaneiOSUITests', # Project scheme name |
| 131 | + clean: true, # Clean project folder before test execution |
| 132 | + destination: REAL_DEVICES, # Devices for testing |
| 133 | + result_bundle: "TestResults" # To generate test reports |
| 134 | + ) |
| 135 | + generate_report |
| 136 | +end |
| 137 | +``` |
| 138 | + |
| 139 | +Tests will be executed on configured devices and a test report will be captured for each device. |
| 140 | + |
| 141 | +> 📝 **Notes:** |
| 142 | +>Sometimes you will need to add the following additional parameters |
| 143 | +> |
| 144 | +> - fail_build: false #Otherwise following steps won’t be executed |
| 145 | +> - disable_concurrent_testing: true #to stop parallel execution and enable sequential testing order |
| 146 | +> - testplan: “” #define the test play you wish to execute (eg: smoke, regression,etc…) |
| 147 | +
|
| 148 | +Check out the **[list of all available parameters](https://docs.fastlane.tools/actions/run_tests/#parameters)** |
| 149 | + |
| 150 | +# Next, we'll see how we can add a fastlane for SonarQube analysis |
| 151 | + |
| 152 | +- Create a new text file called `Sonar` |
| 153 | +- Add following code segment |
| 154 | + |
| 155 | +```kotlin |
| 156 | +desc 'run a sonar scan' |
| 157 | +lane :sonar_analysis do |options| |
| 158 | + defaults = { |
| 159 | + project_key: "name.gretzki.awesomeApp", |
| 160 | + project_version: "1.0", |
| 161 | + project_name: "iOS - AwesomeApp", |
| 162 | + sonar_organization: "myOrg", |
| 163 | + sonar_login: "123456abcdef", |
| 164 | + sonar_url: "https://sonarcloud.io" |
| 165 | + }.freeze |
| 166 | + |
| 167 | + sonar(defaults.merge(options)) |
| 168 | +end |
| 169 | +``` |
| 170 | + |
| 171 | +> ⚠️ **Warning:** Please update the Sonar values accordingly. |
| 172 | +
|
| 173 | + |
| 174 | +# Now Let's create the final fatslane workflow as showed |
| 175 | + |
| 176 | +- Open the `Fastfile` |
| 177 | +- Add following new lane |
| 178 | + |
| 179 | +```kotlin |
| 180 | +lane :pr_merge_on_develop do |
| 181 | + sonar_analysis |
| 182 | + build |
| 183 | + run_unit_tests |
| 184 | + run_ui_tests |
| 185 | +end |
| 186 | +``` |
| 187 | +Now it's time to execute ` fastlane pr_merge_on_develop` |
| 188 | + |
| 189 | +After a successful execution, HTML report will be generated with the test results for each device, as well as test logs with screenshots. |
| 190 | + |
| 191 | +<figure> |
| 192 | + <img src="/assets/images/post/ios_test_report_example.gif" style="width:800px;height:400px;"> |
| 193 | + <figcaption><em>Test Report example.</em></figcaption> |
| 194 | +</figure> |
| 195 | + |
| 196 | +<br> |
| 197 | + |
| 198 | +# Conclusion 🔥 |
| 199 | + |
| 200 | +In this article, we have learned how to set up Fastlane for your iOS project and execute Xcode UI Tests in parallel on physical devices and simulators. |
| 201 | + |
| 202 | +It will allow us to set up our test execution on a Continuous Integration server and attach advanced test reports, |
| 203 | +as well as increasing of test coverage and reduce manual compatibility testing effort. I hope now you can set Quality gates in your project CI pipeline. 🤗 |
| 204 | + |
| 205 | +**[Complete Test Framework setup example with Application Source Code is available on GitHub.](https://github.com/dilshan5/ExampleFastLaneiOS)** |
| 206 | + |
| 207 | +If you need to build an advanced fastline pipeline for Continuous Deployment(CD), please refer **[“Beta Deployment”](http://docs.fastlane.tools/getting-started/ios/beta-deployment/)** |
| 208 | + |
| 209 | + |
| 210 | +Happy Coding and Testing..!!! 😊 💻 |
0 commit comments