Test-Driven iOS Development with Swift
eBook - ePub

Test-Driven iOS Development with Swift

  1. 280 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Test-Driven iOS Development with Swift

Book details
Book preview
Table of contents
Citations

About This Book

Build robust applications using TDD with Swift 5.5 and become a TDD expert by writing tests for view controller, views, network code, and even SwiftUI viewKey Features• Build a complete iOS app using test-driven development• Explore testing view controllers, table views, navigation, and network code• Learn how to write tests for Combine and SwiftUI codeBook DescriptionTest-driven development (TDD) is a proven way to find software bugs earlier on in software development. Writing tests before you code improves the structure and maintainability of your apps, and so using TDD in combination with Swift 5.5's improved syntax leaves you with no excuse for writing bad code.Developers working with iOS will be able to put their knowledge to work with this practical guide to TDD in iOS. This book will help you grasp the fundamentals and show you how to run TDD with Xcode. You'll learn how to test network code, navigate between different parts of the app, run asynchronous tests, and much more. Using practical, real-world examples, you'll begin with an overview of the TDD workflow and get to grips with unit testing concepts and code cycles. You'll then develop an entire iOS app using TDD while exploring different strategies for writing tests for models, view controllers, and networking code. Additionally, you'll explore how to test the user interface and business logic of iOS apps and even write tests for the network layer of the sample app.By the end of this TDD book, you'll be able to implement TDD methodologies comfortably in your day-to-day development for building scalable and robust applications.What you will learn• Implement TDD in Swift application development• Detect bugs before you run code using the TDD approach• Use TDD to build models, view controllers, and views• Test network code with asynchronous tests and stubs• Write code that's a joy to read and maintain• Design functional tests to suit your software requirements• Discover scenarios where TDD should be applied and avoidedWho this book is forThis book is for iOS developers looking to apply TDD to build maintainable and scalable applications. Intermediate-level developers with Swift application development experience will be able to make the most out of this book. Prior experience of applying TDD to Swift applications is not required.

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Test-Driven iOS Development with Swift by Dr. Dominik Hauser in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming. We have over one million books available in our catalogue for you to explore.

Information

Year
2022
ISBN
9781803246864
Edition
4

Section 1 –The Basics of Test-Driven iOS Development

Without a good understanding of the basics, learning is hard and frustrating. In this section, we will learn what unit tests are, how they are connected to test-driven development, and how they look and work in Xcode.
In this section, we will cover the following chapters:
  • Chapter 1, Your First Unit Tests
  • Chapter 2, Understanding Test-Driven Development
  • Chapter 3, Test-Driven Development in Xcode

Chapter 1: Your First Unit Tests

When the iPhone platform was first introduced, applications were small and focused only on one feature. It was easy to make money with an app that only did one thing (for example, a flashlight app that only showed a white screen). The code of these early apps only had a few hundred lines and could easily be tested by tapping the screen for a few minutes.
Since then, the App Store and the available apps have changed a lot. There are still small apps with a clear focus in the App Store, but it's much harder to make money from them. A common app has many features but still needs to be easy to use. There are companies with several developers working on one app full-time. These apps sometimes have a feature set that is normally found in desktop applications. It is very difficult and time-consuming to test all the features in such apps manually for every update.
One reason for this is that manual testing needs to be done through a user interface (UI), and it takes time to load the app to be tested. In addition to this, human beings are very slow compared to the capabilities of computers for tasks such as testing and verifying computer programs. Most of the time, a computer (or a smartphone) waits for the user's next input. If we could let a computer insert values, testing could be drastically accelerated. In fact, a computer can run several hundred tests within a few seconds. This is exactly what unit tests are all about.
A unit test is a piece of code that executes some other code and checks whether the result is what the developer expected. The word "unit" means that the test executes a small unit of code. Usually, that is one function of a class or some similar type of structure. How big the unit actually is depends on the feature to be tested and on the person who is writing the test.
Writing unit tests seems hard at first because for most developers, it's a new concept. This chapter is aimed at helping you get started with writing your first simple unit tests.
These are the main topics we will cover in the chapter:
  • Building your first automatic unit test
  • Assert functions in the XCTest framework
  • Understanding the difference from other kinds of tests

Technical requirements

All the code in this chapter is uploaded (in complete form) here:
https://github.com/PacktPublishing/Test-Driven-iOS-Development-with-Swift-Fourth-Edition/tree/main/chapter01

Building your first automatic unit test

If you have done some iOS development (or application development in general) already, the following example might seem familiar to you.
You are planning to build an app. You start collecting features, drawing some sketches, or your project manager hands the requirements to you. At some point, you start coding. You set up the project and start implementing the required features of the app.
Let's say the app has an input form, and the values the user puts in have to be validated before the data can be sent to the server. The validation checks, for example, whether the email address and the phone number have a valid format. After implementing the form, you want to check whether everything works. But before you can test it manually, you need to write code that presents the form on the screen. Then, you build and run your app in the iOS simulator. The form is somewhere deep in the view hierarchy, so you navigate to the view and put the values into the form. It doesn't work—something is wrong with the phone number validation code. You go back to the code and try to fix the problem. Sometimes, this also means starting the debugger and stepping through the code to find the bug.
Eventually, the validation works for the test data you put in. Normally, you would need to test for all possible values to make sure that the validation not only works for your name and your data, but also for all valid data. But there is this long list of requirements on your desk, and you are already running late. The navigation to the form takes three taps in the simulator and putting in all the different values just takes too long. You are a coder, after all.
If only a robot could perform this testing for you.

What are unit tests?

Automatic unit tests act like this robot for you. They execute cod...

Table of contents

  1. Fourth Edition
  2. Test-Driven iOS Development with Swift
  3. Preface
  4. Section 1 –The Basics of Test-Driven iOS Development
  5. Chapter 1: Your First Unit Tests
  6. Chapter 2: Understanding Test-Driven Development
  7. Chapter 3: Test-Driven Development in Xcode
  8. Section 2 –The Data Model
  9. Chapter 4: The App We Are Going to Build
  10. Chapter 5: Building a Structure for ToDo Items
  11. Chapter 6: Testing, Loading, and Saving Data
  12. Section 3 –Views and View Controllers
  13. Chapter 7: Building a Table View Controller for the To-Do Items
  14. Chapter 8: Building a Simple Detail View
  15. Chapter 9: Test-Driven Input View in SwiftUI
  16. Section 4 –Networking and Navigation
  17. Chapter 10: Testing Networking Code
  18. Chapter 11: Easy Navigation with Coordinators
  19. Other Books You May Enjoy