Pro iOS Testing XCTest Framework for UI and Unit Testing by Avi Tsadok

310 76 0
Pro iOS Testing XCTest Framework for UI and Unit Testing by Avi Tsadok

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Discover what tools there are for unit testing in iOS, and how to work in a test-driven environment. This book reveals how testing is a crucial capability in any iOS developer’s toolset, and a minimum requirement in iOS interviews. A few years ago, tests on mobile platforms were not very popular. It wasn’t a technical constraint, more a cultural one. But these days it’s a crucial skill set, especially when projects become big and hard to maintain. This book shows you how to set up a testing target in XCode unit tests. You''''ll learn how to write unit tests properly and incorporate concepts like spies and mocks and code coverage. You''''ll also learn the philosophy behind the architecture of UI tests, and how to mock network and DB layers in testing. Write unbreakable UI tests performance tests, as well. And learn the difference between integration tests and snapshot testing. This book will show you how to maintain code that''''s not only bug-free but will also remain high quality over time and maintainable while you make changes and refactors during an app''''s life. Testing in all its aspects is the best way of maintaining iOS projects to run fast and reliably long after you''''ve released them. Many iOS developers working today lack an understanding of the advantages of testing, and might be unfamiliar with tools that make the job easier, such as XCTest framework. With Pro iOS Testing you''''ll see how to develop and test apps that work and stay working for a long time. What You''''ll Learn Set up a stable testing system Extend an app''''s lifetime with testing before release Incorporate testing into your everyday development routine Write unbreakable UI tests performance tests Understand the difference between integration tests and snapshot testing Who This Book Is For Professional iOS developers with extensive experience in the basics of building apps.

Pro iOS Testing XCTest Framework for UI and Unit Testing — Avi Tsadok Pro iOS Testing XCTest Framework for UI and Unit Testing Avi Tsadok Pro iOS Testing: XCTest Framework for UI and Unit Testing Avi Tsadok Tel Mond, Israel ISBN-13 (pbk): 978-1-4842-6381-5 https://doi.org/10.1007/978-1-4842-6382-2 ISBN-13 (electronic): 978-1-4842-6382-2 Copyright © 2020 by Avi Tsadok This work is subject to copyright All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed Trademarked names, logos, and images may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made The publisher makes no warranty, express or implied, with respect to the material contained herein Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Aaron Black Development Editor: James Markham Coordinating Editor: Jessica Vakili Distributed to the book trade worldwide by Springer Science+Business Media New York, NY Plaza, New York, NY 10014 Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@ springer-sbm.com, or visit www.springeronline.com Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc) SSBM Finance Inc is a Delaware corporation For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com Apress titles may be purchased in bulk for academic, corporate, or promotional use eBook versions and licenses are also available for most titles For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/ 978-1-4842-6381-5 For more detailed information, please visit http://www.apress.com/ source-code Printed on acid-free paper It is a big challenge to write a book while also being a full-­time dad and an iOS developer Therefore, I would like to thank my family – my kids, Harel and Maya, and my loving wife, Tammy – who gave me the time and strength to sit down, investigate, dig, and write Without your unconditional support, this book would not exist Table of Contents About the Author��������������������������������������������������������������������������������xv About the Technical Reviewer����������������������������������������������������������xvii Chapter 1: Introduction for Testing�������������������������������������������������������1 Introduction�����������������������������������������������������������������������������������������������������������1 How to Read This Book�����������������������������������������������������������������������������������������2 What Is Software Testing?������������������������������������������������������������������������������������2 Software Testing in iOS�����������������������������������������������������������������������������������������2 Why Is Testing So Important?��������������������������������������������������������������������������������4 What Can We Test?������������������������������������������������������������������������������������������������6 Summary��������������������������������������������������������������������������������������������������������������8 Chapter 2: Setting Up Our Infrastructure����������������������������������������������9 Introduction�����������������������������������������������������������������������������������������������������������9 Basic Terms���������������������������������������������������������������������������������������������������������10 “My Weather” App�����������������������������������������������������������������������������������������������11 Add Test Targets to an Existing Project����������������������������������������������������������14 Link Everything Together������������������������������������������������������������������������������������15 The Info Tab���������������������������������������������������������������������������������������������������17 The Arguments Tab����������������������������������������������������������������������������������������26 The Options Tab���������������������������������������������������������������������������������������������30 The Diagnostics Tab���������������������������������������������������������������������������������������32 v Table of Contents Exclude Test Classes�������������������������������������������������������������������������������������������35 Disable Tests from the Scheme Editor�����������������������������������������������������������35 Disable Tests from the Test Navigator�����������������������������������������������������������36 Disable Tests by Renaming Them������������������������������������������������������������������37 How Many Test Bundles to Create?��������������������������������������������������������������������38 Test Plans������������������������������������������������������������������������������������������������������������39 Test Plans to Make Your Life Easier���������������������������������������������������������������39 Create Your First Test Plan�����������������������������������������������������������������������������40 Test Plan Configurations��������������������������������������������������������������������������������42 Running Your Test Plans��������������������������������������������������������������������������������46 Summary������������������������������������������������������������������������������������������������������������47 Chapter 3: Writing Tests – The Basics������������������������������������������������49 Introduction���������������������������������������������������������������������������������������������������������49 What Exactly Are Unit Tests?�������������������������������������������������������������������������������50 XCTest and XCTestCase���������������������������������������������������������������������������������������51 XCTestCase����������������������������������������������������������������������������������������������������51 Our First Test Class����������������������������������������������������������������������������������������53 Enable Testability�������������������������������������������������������������������������������������������55 @testable������������������������������������������������������������������������������������������������������56 CocoaPods and Testing Targets���������������������������������������������������������������������57 XCTestCase Life Cycle�����������������������������������������������������������������������������������58 Writing Unit Tests������������������������������������������������������������������������������������������������62 Unit Test Anatomy������������������������������������������������������������������������������������������63 Assertions������������������������������������������������������������������������������������������������������64 Write Asynchronous Operations��������������������������������������������������������������������70 Summary������������������������������������������������������������������������������������������������������������78 vi Table of Contents Chapter 4: Writing Tests – Advanced Techniques�������������������������������79 Introduction���������������������������������������������������������������������������������������������������������79 Test Doubles (Fake, Fake, Fake)��������������������������������������������������������������������������80 Mocks Mocks Everywhere (?)�����������������������������������������������������������������������80 Avoid Test Doubles If Possible�����������������������������������������������������������������������89 Comparing�����������������������������������������������������������������������������������������������������������94 The Problem with Comparing������������������������������������������������������������������������94 Equatable Protocol����������������������������������������������������������������������������������������95 Comparable Protocol�������������������������������������������������������������������������������������96 Compare UIImages����������������������������������������������������������������������������������������97 Compare Arrays���������������������������������������������������������������������������������������������98 Comparison Is Critical in Testing�������������������������������������������������������������������99 Parameterized Unit Tests������������������������������������������������������������������������������������99 Create Abstract Method for Testing�������������������������������������������������������������101 Loading Test Cases from a File��������������������������������������������������������������������103 Invoke Tests Dynamically����������������������������������������������������������������������������106 Summary����������������������������������������������������������������������������������������������������������111 Chapter 5: Integration Tests�������������������������������������������������������������113 Introduction�������������������������������������������������������������������������������������������������������113 The Idea Behind Integration Tests���������������������������������������������������������������������114 What Exactly Are Integration Tests��������������������������������������������������������������114 Integration Tests vs Unit Tests��������������������������������������������������������������������115 Define the Scope�����������������������������������������������������������������������������������������115 vii Table of Contents Writing Integration Tests�����������������������������������������������������������������������������������118 Our First Integration Test�����������������������������������������������������������������������������118 Running in Parallel��������������������������������������������������������������������������������������121 Fault Point in Integration Tests��������������������������������������������������������������������121 A Bigger System to Test�������������������������������������������������������������������������������121 Client-Server Tests��������������������������������������������������������������������������������������129 Summary����������������������������������������������������������������������������������������������������������139 Chapter 6: Write Testable Code���������������������������������������������������������141 Introduction�������������������������������������������������������������������������������������������������������141 What Is a Testable Code?����������������������������������������������������������������������������������142 Clean Code��������������������������������������������������������������������������������������������������������144 KISS (Keep It Simple, Stupid)�����������������������������������������������������������������������145 DRY��������������������������������������������������������������������������������������������������������������146 YAGNI (You Aren’t Gonna Need It)����������������������������������������������������������������147 Code That Is Pleasant to Read Is Also Pleasant to Test�������������������������������147 Pure Functions��������������������������������������������������������������������������������������������������149 Refactor Our Functions to Be Pure��������������������������������������������������������������151 Protocol-Oriented Programming������������������������������������������������������������������153 Dependency Injection����������������������������������������������������������������������������������������153 Ways to Implement Dependency Injection��������������������������������������������������154 SOLID Principles������������������������������������������������������������������������������������������������158 S – Single-Responsibility Principle�������������������������������������������������������������159 O – Open/Closed Principle���������������������������������������������������������������������������159 L – Liskov Substitution Principle�����������������������������������������������������������������159 I – Interface-Segregation Principle�������������������������������������������������������������162 D – Dependency Inversion Principle������������������������������������������������������������162 viii Table of Contents Design Patterns and Architectures�������������������������������������������������������������������163 Singleton�����������������������������������������������������������������������������������������������������163 Facade���������������������������������������������������������������������������������������������������������164 Decorator�����������������������������������������������������������������������������������������������������165 Factory��������������������������������������������������������������������������������������������������������166 MVC�������������������������������������������������������������������������������������������������������������������167 The Model – M���������������������������������������������������������������������������������������������169 The View – V������������������������������������������������������������������������������������������������170 The Controller – C����������������������������������������������������������������������������������������170 The Problem with MVC��������������������������������������������������������������������������������171 MVP/MVVM��������������������������������������������������������������������������������������������������������171 VIPER�����������������������������������������������������������������������������������������������������������������174 Comparison Between Different Design Patterns�����������������������������������������������176 Summary����������������������������������������������������������������������������������������������������������176 Chapter 7: User Interface Tests��������������������������������������������������������177 Introduction�������������������������������������������������������������������������������������������������������177 Adding UI Tests��������������������������������������������������������������������������������������������������178 How Do UI Tests Work?�������������������������������������������������������������������������������������179 Accessibility in UIKit – accessibilityLabel����������������������������������������������������180 Element Tree������������������������������������������������������������������������������������������������181 Write Our First UI Test���������������������������������������������������������������������������������������182 XCUIApplication�������������������������������������������������������������������������������������������183 Elements������������������������������������������������������������������������������������������������������183 Wrap It All Together�������������������������������������������������������������������������������������194 Record Your Actions�������������������������������������������������������������������������������������195 ix Table of Contents Dealing with Problems��������������������������������������������������������������������������������������197 Keeping Your Tests Consistent���������������������������������������������������������������������198 Handling System Alerts�������������������������������������������������������������������������������199 Page Object Model��������������������������������������������������������������������������������������������200 The Problem������������������������������������������������������������������������������������������������200 What Is a Page Object Model?���������������������������������������������������������������������201 Test Reports������������������������������������������������������������������������������������������������������205 Activities������������������������������������������������������������������������������������������������������206 Attachments������������������������������������������������������������������������������������������������210 More Great UI Test Features������������������������������������������������������������������������������216 Testing Your Siri Integration�������������������������������������������������������������������������217 Multiple App Testing������������������������������������������������������������������������������������217 Dragging Using XCUICoordinate������������������������������������������������������������������218 Summary����������������������������������������������������������������������������������������������������������219 Chapter 8: Cover Another Aspect of Your App – Performance Testing�������������������������������������������������������������������������221 Introduction�������������������������������������������������������������������������������������������������������221 The Basic Idea of Performance Test������������������������������������������������������������������222 The Basic Measuring Function��������������������������������������������������������������������������223 Define the Baseline�������������������������������������������������������������������������������������224 What the “Baseline” Means for Our Test?���������������������������������������������������226 measure(metrics:) Function������������������������������������������������������������������������������227 Analyzing the Metrics����������������������������������������������������������������������������������229 More Configuration with XCTMeasureOptions��������������������������������������������������231 iterationCount����������������������������������������������������������������������������������������������232 invocationOptions����������������������������������������������������������������������������������������232 Measuring App Launch��������������������������������������������������������������������������������234 x Chapter 11 Using Command-Line Tools Command-Line Tools Command-Line tools are a great way to script your testing and incorporate it with CI/CD environments They let you build, archive, and test your projects from the terminal command line and customize your run with different parameters and arguments Command-Line Tools are not just for CI/CD – they can also help you automate your tasks during development For example, instead of repeating the same actions of testing different test plans, committing, and then archiving, you can implement all of that in one script file Meet xcodebuild “xcodebuild” is the primary tool of the command-line tool package, and it’s used to build, archive, and analyze test and any action you can with the scheme The real power of xcodebuild is the flexibility to run various actions and configurations and, as a result of that, it is the primary tool for testing Install and Set Up xcodebuild Although you can download Command-Line Tools separately, they come with every new Xcode But if you still want to download them, you have two options: –– Download from the Developer website –– Install from the command line using xcode-select “xcode-select” is a command line that comes bundled with macOS. If you want to use it to install Command-Line Tools, open the Terminal and type $ xcode-select –install 287 Chapter 11 Using Command-Line Tools And press Enter Many developers have multiple versions of Xcode installed on their machine, and one of the first steps using xcodebuild is to make sure it works with the correct Xcode version To find out what is the “active” Xcode version, we can use xcode-select again for that: $ xcode-select -p /Applications/Xcode.app/Contents/Developer To change the active Xcode version, locate the developer path and use the -switch command: $ xcode-select -switch /Applications/Xcode12.app/Contents/ Developer To uninstall Command-Line Tools, just delete /Library/Developer/ CommandLineTools with this command: $ sudo rm -r /Library/Developer/CommandLineTools Run Tests with xcodebuild Running tests with xcodebuild is quite simple and requires very few parameters for the basic run First, you need to make sure your current directory in the Terminal is the project directory A basic xcodebuild command looks something like this: $ xcodebuild \ -workspace MyWeatherApp.xcworkspace \ -scheme MyWeatherApp \ -destination 'platform=iOS Simulator,name=iPhone 11' \ test 288 Chapter 11 Using Command-Line Tools Let’s go over the command parameters: workspace – If you are using workspaces instead of projects (CocoaPods is a good example), pass your workspace name here scheme – Your selected scheme name test – Run the “test” action of the scheme destination – Specify the platform that is used for the test A destination can be either a simulator or a physical device Let’s go deeper into this The Destination Argument The syntax of the destination argument is based on key–value pairs The first key is the platform, which describes whether it’s a device or a simulator and what platform it is This is the list of platforms you can use: –– OS X, your Mac –– iOS, a connected iOS device –– iOS Simulator –– watchOS –– watchOS Simulator –– tvOS –– tvOS Simulator The second key–value pair is related to the type of the device If it’s a physical device, you can use either “name” to target the actual device name or “id” to target the device UUID 289 Chapter 11 Using Command-Line Tools If it’s a simulator, the “name” key describes the name of the simulator (“iPhone 11”), and another key–value pair is “os” to specify the OS version (“11.0”) Let’s see some examples: To run your test on iPhone 11 Simulator, running iOS 12.0: -destination "platform=iOS Simulator, name=iPhone 11, OS=12.0" To run your test on a physical device: -destination "platform=iOS, name=Avi's iPhone" To list all of your available destinations, type in your terminal: $ instruments -s devices Run Test Plans from Command Line You can use xcodebuild to run test plans right from the command line To see the list of available test plans for a scheme, use showTestPlans argument: $ xcodebuild -scheme 'My Weather App' -showTestPlans Test plans associated with the scheme "My Weather App":         Localization Test Plan         Memory Running tests with a specific scheme will run the default test plan To run a particular test plan, use testPlan argument: $ xcodebuild \ -workspace MyWeatherApp.xcworkspace \ -scheme MyWeatherApp \ -destination 'platform=iOS Simulator,name=iPhone 11' \ -testPlan 'Memory' test 290 Chapter 11 Using Command-Line Tools More xcodebuild Important Arguments “xcodebuild” has more tricks up in its sleeves To list all the schemes, build configurations, and targets, use list argument: $ xcodebuild -list Information about project "My Weather App":     Targets:         My Weather App         My Weather AppTests         My Weather AppUITests     Build Configurations:         Debug         Release     If no build configuration is specified and -scheme is not passed then "Release" is used     Schemes:         My Weather App If you already build your app for testing and want to rerun tests without building it again, you can use run-without-building to save time: $ xcodebuild \ -workspace MyWeatherApp.xcworkspace \ -scheme MyWeatherApp \ -destination 'platform=iOS Simulator,name=iPhone 11' \ run-without-building Test 291 Chapter 11 Using Command-Line Tools On the other hand, if all you want is to build but not test, you can use build-for-testing argument: $ xcodebuild \ -workspace MyWeatherApp.xcworkspace \ -scheme MyWeatherApp \ -destination 'platform=iOS Simulator,name=iPhone 11' \ build-for-testing Test If you want to make sure Xcode cleans the project before running your tests, you can add clean to the command: $ xcodebuild \ clean \ -workspace MyWeatherApp.xcworkspace \ -scheme MyWeatherApp \ -destination 'platform=iOS Simulator,name=iPhone 11' \ test To see all the available SDKs you can use, try the showsdks argument: $ xcodebuild -showsdks iOS SDKs:         iOS 13.2                      -sdk iphoneos13.2 iOS Simulator SDKs:         Simulator - iOS 13.2          -sdk iphonesimulator13.2 macOS SDKs:         DriverKit 19.0                -sdk driverkit.macosx19.0         macOS 10.15                   -sdk macosx10.15 tvOS SDKs:         tvOS 13.2                     -sdk appletvos13.2 292 Chapter 11 Using Command-Line Tools tvOS Simulator SDKs:         Simulator - tvOS 13.2         -sdk appletvsimulator13.2 watchOS SDKs:         watchOS 6.1                   -sdk watchos6.1 watchOS Simulator SDKs:         Simulator - watchOS 6.1        -sdk watchsimulator6.1 Summary We’ve learned that it’s not enough to write great tests; it’s also essential to make sure to run them continuously As an iOS developer, we need to focus on writing great software and solve complex problems Let the automation server take care of running the tests for us 293 Index A Arguments tab environment variables, 29, 30 launch arguments Arguments Passed on Launch, 27 code, 28, 29 CommandLine, 28, 29 list of arguments, 27, 28 new argument, 27 ProcessInfo, 28, 29 user registration, 27 scheme editor, 26 Arrange-Act-Assert (AAA), 120 Attachment, types, 215, 216 B Baseline definition, 224 deviation, 227 info.plist, 237 packet content, 237 settings window, 225 STDDEV, 226 Xcode, 236, 238 Behavior-Driven Development (BDD), 270 declarative scenarios, 276 developer, 274 GHERKO language, 278 GIVEN section, 279, 280 multiple tests, 277 product owner, 273 QA, 273 real-world data, 277 registration screen, 275 TDD process, 274 technical issue, 273 THEN section, 278 use cases, 273 user scenarios, 275, 276 Black Box testing, 131, 132, 134 Bottom-Up Approach (BUA), 123, 128 Bugs, 263 C CalendarService method, 127 Classic pyramid, 264, 265 Clean Code, 144 DRY, 146 KISS, 145, 146 pleasant to read, 147 pleasant to write, 148 YAGNI, 147 © Avi Tsadok 2020 A Tsadok, Pro iOS Testing, https://doi.org/10.1007/978-1-4842-6382-2 295 Index Client-Server integration tests, 116, 139 Client-Server testing, 129, 130 black box testing, 132–134 black box vs white box, 131 gold responses, 138 ideal state, 135 network recorder, 135–137 white box testing, 134, 135 Code coverage detecting dead code, 282 identify areas, 282 target, 280, 281 UI tests, 280 Command-line tools CI/CD environments, 287 installation, 287 -switch command, 288 xcodebuild, 287 Comparing arrays, 98 Comparable protocol, 96, 97 Equatable protocol, 95 problem, 94, 95 testing, 99 UIImages, 97 Continuous Deployment (CD), 2, 286 Continuous Integration (CI), 19, 283, 286 Coupling, 89–93 296 D, E Data sync, 130, 134 Decorator, 165, 166 Dependency injection initializer-based, 155 logic unit, 154 parameter-based, 157, 158 property-based, 156, 157 Dependency Inversion Principle, 162 Design pattern comparison, 176 decorator, 165, 166 facade, 164, 165 factory, 166, 167 singleton, 163, 164 Development task, 262 Diagnostics tab, 32, 33 Address Sanitizer, 33 Guard Malloc, 35 Main Thread Checker, 34 Malloc Guard Edges, 34 Malloc Scribble, 34 Thread Sanitizer, 34 Zombie Objects, 35 doLogin() method, 83, 84 dump() swift function, 245 F Facade, 142, 164, 165 Factory, 166, 167 Index #file keyword, 242 findCuteCats(), 71 fulfill() method, 73 G, H Given-When-Then (GWT), 63 generateLayout() method, 100, 101 Groups, 13 I, J Ice cream cone model, 265–267 collaboration, 267 explicit agreement, 268 unit tests, 268 ImageProcessor, 223 Incremental integration test BUA, 123 edges, 129 top-down, 128 Info tab application data download container, 23, 24 format, 24 new group, 25 package, 24, 25 XCAppData, 23 build configuration, 17 list of tests, 17 location, 21, 22 options, 17, 18 parallel testing, 18, 19 Randomize Execution Order, 19, 20 Initializer-based Dependency Injection, 155 init() method, 81 Integration tests, 6, iOS, 115 layer, 114 scope, 116 types, 117 UI layer, 115 vs unit test, 115 writing AAA, 120 complex feature, 122 fault point, 121 incremental, 122 layers, 119 running in parallel, 121 To-Do App, 118 Interface-Segregation Principle, 162 iOSSnapshotTestCase CocoaPods installation, 250 environment variables, 250, 251 FBSnapshotTestCase, 251, 252 setup() method, 253, 254 isInverted property, 74 isPrimeFunction() function, K Keep It Simple, Stupid (KISS), 145 297 Index L O Liskov Substitution Principle (LSP), 159–161 loadViewIfNeeded() method, 120 Object-oriented programming (OOP), 149, 153 Open/Closed Principle, 159 Options tab, 30, 31 application language, 31 application region, 31 code coverage, 32 UI testing, 32 M Measuring function CPU cycles, 231 CPU time, 230 monotonic time, 229, 230 XCTClockMetric, 227 XCTCPUMetric, 227 XCTMemoryMetric, 227 XCTOSSignpostMetric, 228 XCTStorageMetric, 228, 231 Model layer, 169, 170 Model-View-Controller (MVC), 168, 169 The controller-C, 170, 171 The model-M, 169 MVVM, 172, 173 problem, 171 The view-V, 170 VIPER, 174, 175 “My Weather” App, 11, 12, 27 N Narrow integration tests, 116, 117, 130 Network Layer, 114, 115, 169, 170 298 P, Q, R Page object model A/B testing, 200 doSignIn() method, 203, 204 init() method, 203 methods/properties, 201, 202 Parameter-Based injection, 157, 158 Parameterized unit tests abstract method, 101, 102 function signature, 99, 100 invoke tests create/add new test cases, Fly, 108, 110, 111 defaultTestSuite(), 107, 108 XCTestRun environment, 106, 107 loading test cases, file, 103, 105 Performance tests a-sync function, 235 average, 222 Index baseline (see Baseline) measuring function, 223 software, 221 wait() function, 236 XCTestExpectation tool, 235 Property-based injection, 156, 157 Protocol-oriented programming, 153 Pure functions, 149 placeType, 152 protocols, 153 refactor the code, 151 updateTitle(:), 151 vs standard function, 150 S Schemes, 10 Screenshots creation, 210 location, 214 scheme configuration, 212, 213 test report, 211, 212 XCResult, 214, 215 setup() method, 58, 63, 121, 253 setUpWithError() function, 54, 58–60 Single-Responsibility Principle, 159 Singletons, 152, 164 Snapshot testing assertion function, 244, 245, 247, 259 diagram, 240 documentation, 247 fileNameOptions, 257 function signature, 241 manual test, 248 parameters, 259 reference, 241 snapshotVerifyViewOrLayer function, 258 state persistency, 239 suffixes, 258 swift keywords, 242–244 unit test, 248 Software testing better code quality, 5, check ourselves, definition, 1, documentation, iOS, 2–4 refactoring, regressions, SOLID principles, 158–162 T teardown() method, 60, 61, 126, 127 tearDownWithError() method, 54, 60 Testable code, 142 Test bundles, 38, 39 Test classes, disable renaming, 37, 38 scheme editor, 35, 36 test navigator, 36, 37 299 Index Test coverage parameter value coverage, 283 product, 283 QA testers, 282 risk, 283 Test doubles mocks complete vs partial, 88 dummy, 81 fake, 82 mock, 86–88 Spy, 85, 86 Stub, 82–84 Test-Driven Development (TDD), 263, 270 life cycle, 271 refactor part, 272 software development technique, 271 testImageProcessing() method, 72 Testing diamond, 268, 269 Test plans configuration, 42–46 convert, 40–42 definition, 39 options, 41 running, 46, 47 scheme editor, 40 Test report activities, 206, 208, 209 Xcode, 205 Typealias, 146, 147 300 U UI snapshot testing FBSnapshotTestCase, 249 iOSSnapshotTestCase, 249 text changes, 248 verification failure, 254–256 UI Testing framework features, 216 multiple app, 217, 218 Siri integration, 217 XCUICoordinate, 218, 219 Unit tests, 6, 8, 14, 50, 263 Unit tests, writing anatomy, 63, 64 assertions, 64–70 asynchronous operations code, 70 expect array expectation, ordered, 75 expect/wait/fulfill/assert, 71 one expectation, fulfill multiple times, 74 one expectation, not fulfilled, 74 XCTestExpectation pattern, 72, 73 XCTestExpectation subclass, 75–77 User interface(UI) tests accessibilityLabel, 180 addUIInterruptionMonitor() function, 199 Index consistent, 198 element tree, 181 flows, 177 fruits app, 182 identity inspector, 180 issues, 197 launchArguments property, 198 options window, 179 Xcode, 179 XCUIApplication, 183 V verify() method, 88 viewDidAppear method, 143 W waitForExpectations() method, 73 White box testing, 131, 135, 138 X Xcode record button, 196 text field, 196 Xcodebuild build-for-testing arguments, 292 destination arguments, 289 list arguments, 291 running tests, 288 showsdks arguments, 292, 293 testplan arguments, 290 Xcode project arguments tab (see Arguments tab) basic terms, 10 definition, 10 diagnostics tab (see Diagnostics tab) Info tab (see Info tab) “My Weather” app, 12 new, 12 options tab (see Options tab) schemes, 15–17 template box, 11 test bundles, 13, 38, 39 test classes (see Test classes, disable) test plans (see Test plans) test targets adding, 14 options, 15 unit/UI tests, 12 XCParse, 215 XCTAttachment screenshots, 210 test report, 210 XCTest, 51 301 Index XCTestCase Coc0apods, 57 enable testability, 55 first test class, 53, 54 lifecycle instance, 62 setup(), 58 setUpWithError() Throw, 58, 59 tearDown(), 61 teardown block, 59, 60 tearDownWithError(), 60 test methods, 59 LoginHandler, 51 subclass, 52, 53 @testable, 56 XCTMeasureOptions invocationOptions, 232, 233 302 iterationCount, 232 measure app launch, 234 performance test metrics, 231 XCUIElement actions, 190–192 assertions, 194 buttons property, 184 exists, 192, 193 identifier/index, 188, 189 modifications, 187 querying element, 184, 186 traits, 185 XCTest, 187 Y, Z You Aren’t Gonna Need It (YAGNI), 147 .. .Pro iOS Testing XCTest Framework for? ?UI and? ?Unit Testing Avi? ?Tsadok Pro iOS Testing: XCTest Framework for UI and Unit Testing Avi? ?Tsadok Tel Mond, Israel ISBN-13... relevant for us as iOS developers Unit Tests – Unit tests are the bread and butter of testing Unit Tests are responsible for testing small code pieces of your app, such as methods and functions The unit. .. the different main types of testing which we can use © Avi Tsadok 2020 A Tsadok, Pro iOS Testing, https://doi.org/10.1007/978-1-4842-6382-2_1 Chapter Introduction for Testing How to Read This

Ngày đăng: 17/05/2021, 07:47

Từ khóa liên quan

Mục lục

  • Table of Contents

  • About the Author

  • About the Technical Reviewer

  • Chapter 1: Introduction for Testing

    • Introduction

    • How to Read This Book

    • What Is Software Testing?

    • Software Testing in iOS

    • Why Is Testing So Important?

    • What Can We Test?

    • Summary

    • Chapter 2: Setting Up Our Infrastructure

      • Introduction

      • Basic Terms

      • “My Weather” App

        • Add Test Targets to an Existing Project

        • Link Everything Together

          • The Info Tab

            • Parallel Testing

            • Randomize Execution Order

            • Location and Application Data

            • Location

            • Application Data

            • The Arguments Tab

              • Launch Arguments

              • Environment Variables

Tài liệu cùng người dùng

Tài liệu liên quan