Download Ios Unit Testing Example Techniques - Programming Ebook

Latest

Download Programming Ebook

Friday, October 16, 2020

Download Ios Unit Testing Example Techniques

Ios Unit Testing Example Techniques
Ios Unit Testing Example Techniques




Book Details 
             TitleDownload Ios Unit Testing Example Techniques
      ISBN-10942878347
         Author: BY JON REID
    Language: English
        SubjectSwift / Computers & Technology / Programming / Apple Programming
No. of pages: 597
         Format: EPUB


Preface

One of the principles of agile software development is to “respond to change.”[1] Agility sounds appealing, but these days it is often approached as a project-management tool. Yet we are developers, working in code. The more the code itself resists change, the harder it will be to adopt agility in any meaningful way.

But we can change this—because we are developers, working in code. There are disciplines that fall under the term “technical agile practices.” Among these are unit testing, refactoring, and test-driven development. These are some of the tools we can use to make code pliable so we can safely bend that code into new shapes.

I’ve been learning to apply these disciplines in the workplace since 2001. Lately I’ve been teaching “Test-Driven Development for iOS” workshops.[2] So it seemed a safe bet that if I ever wrote a book, it would be about TDD.

But I realized that folks can learn theoretical TDD, but still be stuck on iOS particulars. Because if you don’t know how to write unit tests for iOS code, how can you ever TDD it? So my book plans pivoted, and here we are. My goal is to give you solid handholds so you can unit test your iOS code.”

Apple’s framework for the user interface, UIKit, lies at the center of iOS code. And the center of UIKit is view controllers. That’s why I focus on unit testing view controllers.


It may seem counterintuitive to test UI-centric code using unit tests. As soon as one sees “UI” or “view,” it’s easy to assume that “UI tests” are the best fit. But UI tests don’t provide the level of control and fast feedback that unit tests give. UI tests are more about automating tests for quality assurance. Unit tests, when they are very fast, serve a different purpose. They become a development tool, helping you bend the code so you can “respond to change.”

So let’s learn how to write unit tests for iOS apps. But keep in mind that the unit tests themselves aren’t the goal. They’re not the end, they’re the means.

How This Book Is Organized

This book explores its ideas using coding examples. Each chapter has a section describing how to make “a place to play.” One can learn things by reading, but learning by doing is more effective. If you take the trouble to set up each project, trying the changes and experiments, your learning will go deeper.

I’ve organized the book into three parts: Foundations, iOS Testing Tips and Techniques, and Using Your New Power. Here’s a quick summary of each part.

PART I: FOUNDATIONS
The first part covers the foundations of using XCTest with iOS apps. The first three chapters cover XCTest. We start with test assertions, move on to managing test suites, and then how to use code coverage to reveal holes.

The next two chapters get into iOS apps. We explore how the app launch sequence may interfere with test isolation. Then we’ll see how to load view controllers from test code. This varies, depending on whether a view controller is storyboard-based, XIB-based, or code-based.

The last chapter of Part I examines dependencies that make testing difficult. Managing dependencies and replacing them is a foundational skill for unit testing. “We’ll see what options are available to us in the Swift programming language.

PART II: IOS TESTING TIPS AND TECHNIQUES

The second part is a grab bag of techniques for testing iOS specifics. You can jump around these chapters more freely. You can follow the cross-references that appear where the chapters build on each other.

We start with the basics of making sure outlets are connected, and how to tap buttons. This alone opens up a world of testing possibilities.

Then we’ll see how to test presented views. This includes alerts and navigation from one view controller to the next.

Things become more challenging as we get into persistent data and networking. These topics lead us into test doubles. We’ll see how to use fakes, spies, and mocks.

For examples of more complicated UI elements, we’ll examine text fields and table views. They’ll take us into the topic of how to test delegate methods. Finally, we’ll see how to test view appearance using snapshot tests.

PART III: USING YOUR NEW POWER

The last part demonstrates what a solid set of unit tests empowers you to do.

Chapters 17, 18, and 19 focus on refactoring. Contrary to common usage, the word “refactoring” doesn’t mean rewriting. Instead, it’s a structured discipline of moving in small, verified steps to change the design of code.
“Disclaimer: No SwiftUI Support
SwiftUI is Apple’s new declarative UI paradigm. They unveiled their first version of it at WWDC 2019. Since there’s a lot of excitement around this paradigm, you may wonder why it’s not covered in this book.

The main difficulty is that as I write this, SwiftUI is still quite new. This makes it a moving target, which Apple will iterate on over the next few years. Not only that, but I simply haven’t used it yet.

And Apple has initially focused on making SwiftUI work and not on making it testable. In the first round, there don’t seem to be any clean ways of testing the behavior or data flow of SwiftUI code. Some folks in the iOS developer community are coming up with their own patterns and helpers as workarounds. Will we come to agree on any standards for unit testing SwiftUI? My guess is that this will happen when Apple decides to put their weight into the problem.

So this book uses the well-established declarative paradigm of UIKit. As you create new projects for the worked examples, be sure to select “Storyboard” as the user interface, not “SwiftUI.”


No comments:

Post a Comment