Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Test-Driven iOS Development with Swift
Test-Driven iOS Development with Swift

Test-Driven iOS Development with Swift: Create fully-featured and highly functional iOS apps by writing tests first

Arrow left icon
Profile Icon Dr. Dominik Hauser
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2 (10 Ratings)
Paperback Feb 2016 218 pages 1st Edition
eBook
$24.99 $35.99
Paperback
$43.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Dr. Dominik Hauser
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2 (10 Ratings)
Paperback Feb 2016 218 pages 1st Edition
eBook
$24.99 $35.99
Paperback
$43.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$24.99 $35.99
Paperback
$43.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Test-Driven iOS Development with Swift

Chapter 2. Planning and Structuring Your Test-Driven iOS App

In the previous chapter, we learned how to write unit tests, and we saw an easy example of TDD. When starting TDD, writing unit tests is easy for most people. The hard part is to transfer the knowledge from writing the test to driving the development. What can be assumed? What should be done before one writes the first test? What should be tested to end up with a complete app?

As a developer, you are used to thinking in terms of code. When you see a feature on the requirement list for an app, your brain already starts to layout the code for this feature. And for recurring problems in iOS development (such as building table views), you most probably have already developed your own best practices.

In TDD, you should not think about the code while working on the test. The tests have to describe what the unit under test should do and not how it should do it. It should be possible to change the implementation without breaking...

Task list view

When starting the app, the user sees a list of to-do items. The items in the list consist of a title, an optional location, and the due date. New items can be added to the list by an add (+) button, which is shown in the navigation bar of the view. The task list view will look like this:

Task list view

User stories:

  • As a user, I want to see the list of to-do items when I open the app
  • As a user, I want to add to-do items to the list

In a to-do list app, the user will obviously need to be able to check items when they are finished. The checked items are shown below the unchecked items, and it is possible to uncheck them again. The app uses the delete button in the UI of UITableView to check and uncheck items. Checked items will be put at the end of the list in a section with the Finished header. The user can also delete all the items from the list by tapping the trash button. The UI for the to-do item list will look like this:

Task list view

User stories:

  • As a user, I want to check a to-do item to mark it...

Task detail view

The tasks detail view shows all the information that's stored for a to-do item. The information consists of a title, due date, location (name and address), and a description. If an address is given, a map with an address is shown. The detail view also allows checking the item as finished. The detail view looks like this:

Task detail view

User stories:

  • As a user, given that I have tapped a to-do item in the list, I want to see its details
  • As a user, I want to check a to-do item from its details view

Task input view

When the user selects the add (+) button in the list view, the task input view is shown. The user can add information for the task. Only the title is required. The Save button can only be selected when a title is given. It is not possible to add a task that is already in the list. The Cancel button dismisses the view. The task input view will look like this:

Task input view

User stories:

  • As a user, given that I have tapped the add (+) button in the item list, I want to see a form to put in the details (title, optional date, optional location name, optional address, and optional description) of a to-do item
  • As a user, I want to add a to-do item to the list of to-do items by tapping on the Save button

We will not implement the editing and deletion of tasks. But when you have worked through this book completely, it will be easy for you to add this feature yourself by writing the tests first.

Keep in mind that we will not test the look and design of the app. Unit tests cannot figure out if an app...

Structure of the app

The following diagram shows the structure of the app:

Structure of the app

The Table View Controller, the delegate and the data source

In iOS apps, data is often presented using a table view. Table views are highly optimized for performance; they are easy to use and to implement. We will use a table view for the list of to-do items.

A table view is usually represented by UITableViewController, which is also the data source and delegate for the table view. This often leads to a massive table View Controller because it is doing too much: presenting the view, navigating to other view controllers, and managing the presentation of the data in the table view.

It is a good practice to split up the responsibility into several classes. Therefore, we will use a helper class to act as the data source and delegate for the table view. The communication between the Table View Controller and the helper class will be defined using a protocol. Protocols define what the interface of a class looks like. This...

Getting started with Xcode

Now, let's start our journey by creating a project that we will implement using TDD.

Open Xcode and create a new iOS project using the Single View Application template. In the options window, add ToDo as the product name, select Swift as language, choose iPhone in the Devices option, and check the box next to Include Unit Tests. Let the Use Core Data and Include UI Tests boxes stay unchecked.

Xcode creates a small iOS project with two targets: one for the implementation code and the other for the unit tests. The template contains code that presents a single view on screen. We could have chosen to start with the master-detail application template because the app will show a master and a detail view. However, we have chosen the Single View Application template because it comes with hardly any code, and in TDD, we want to have all the implementation code demanded by failing tests.

To take a look at how the application target and test target fit together, select...

Task list view


When starting the app, the user sees a list of to-do items. The items in the list consist of a title, an optional location, and the due date. New items can be added to the list by an add (+) button, which is shown in the navigation bar of the view. The task list view will look like this:

User stories:

  • As a user, I want to see the list of to-do items when I open the app

  • As a user, I want to add to-do items to the list

In a to-do list app, the user will obviously need to be able to check items when they are finished. The checked items are shown below the unchecked items, and it is possible to uncheck them again. The app uses the delete button in the UI of UITableView to check and uncheck items. Checked items will be put at the end of the list in a section with the Finished header. The user can also delete all the items from the list by tapping the trash button. The UI for the to-do item list will look like this:

User stories:

  • As a user, I want to check a to-do item to mark it as...

Task detail view


The tasks detail view shows all the information that's stored for a to-do item. The information consists of a title, due date, location (name and address), and a description. If an address is given, a map with an address is shown. The detail view also allows checking the item as finished. The detail view looks like this:

User stories:

  • As a user, given that I have tapped a to-do item in the list, I want to see its details

  • As a user, I want to check a to-do item from its details view

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn test-driven principles to help you build apps with fewer bugs and better designs
  • Become more efficient while working with Swift to move on to your next project faster!
  • Learn how to incorporate all of the principles of test-driven development (TDD) in to your daily programming workflow

Description

Test-driven development (TDD) is a proven way to find software bugs early. Writing tests before your code improves the structure and maintainability of your app. Test-Driven iOS Development with Swift will help you understand the process of TDD and how it impacts your applications written in Swift. Through practical, real-world examples, you’ll start seeing how to implement TDD in context. We will begin with an overview of your TDD workflow and then deep-dive into unit testing concepts and code cycles. We will showcase the workings of functional tests, which will help you improve the user interface. Finally, you will learn about automating deployments and continuous integration to run an environment.

Who is this book for?

If debugging iOS apps is a nerve-racking task for you and you are looking for a fix, this book is for you.

What you will learn

  • Implement TDD in swift application development
  • Get to know the fundamentals, life cycle, and benefits of TDD
  • Explore the tools and frameworks to effectively use TDD
  • Develop models and controllers driven by tests
  • Construct the network layer using stubs
  • Use functional tests to ensure the app works as planned
  • Automate and streamline the building, analysing, testing, and archiving of your iOS apps

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 22, 2016
Length: 218 pages
Edition : 1st
Language : English
ISBN-13 : 9781785880735
Vendor :
Apple
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Feb 22, 2016
Length: 218 pages
Edition : 1st
Language : English
ISBN-13 : 9781785880735
Vendor :
Apple
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 92.98
Swift 2 Blueprints
$48.99
Test-Driven iOS Development with Swift
$43.99
Total $ 92.98 Stars icon

Table of Contents

9 Chapters
1. Your First Unit Tests Chevron down icon Chevron up icon
2. Planning and Structuring Your Test-Driven iOS App Chevron down icon Chevron up icon
3. A Test-Driven Data Model Chevron down icon Chevron up icon
4. A Test-Driven View Controller Chevron down icon Chevron up icon
5. Testing Network Code Chevron down icon Chevron up icon
6. Putting It All Together Chevron down icon Chevron up icon
7. Code Coverage and Continuous Integration Chevron down icon Chevron up icon
8. Where to Go from Here Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2
(10 Ratings)
5 star 70%
4 star 0%
3 star 10%
2 star 20%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Randy Mc Lain Oct 24, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Book clearly explains the concepts of TDD and how to implement the concepts with strict typing Swift code base. Covers a variety of useful concepts from Stubs, Mocks, and Network request ect.
Amazon Verified review Amazon
Amazon Customer Oct 03, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
So I literally just started reading and practicing the book. I have no prior experience with TDD... I am doing Xcode 8 & Swift 3, but the syntax isn't much different still doable, although there is Swift 3 version coming out.So far the Red, Green, refactor approach (from the first chapter) has change my whole thought process about what TDD is. He is literally walking you step by step.I have some problem with downloading the code. Wish it was made easier. The lack of code color is unpleasing to meI will update my review per each chapter I read...
Amazon Verified review Amazon
Florian Schlosser Apr 11, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The Book is really nice written and good structured. The only thing I miss is TDD with core data. Maybe in the next book ;)
Amazon Verified review Amazon
W. Adlani Oct 30, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
TDD has always proven to be a best practice to quickly make robust code and catch bugs before they happen. This book is essential if you want to add unit tests to your code. It's great as a guide and reference with all the buzz words and best practises rolled in to one.
Amazon Verified review Amazon
drollig28 Feb 27, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I was very (!) sceptical because there are already lot's of tutorials and short problem-solvers concerning TDD on the internet. Most of them give a good introduction or help solving a specific problem. What I was missing was a Testdriven example on a real project.Hauser has absolutely convinced me after a few lines of reading that his book is worthwhile bying. It goes far beyond some tutorial you find on the internet. And he has a great style of writing.He leads you through complete test-driven development of an example project on a very high level. Everything is explained in a very clear and structured way. He also covers the question, how to get from use cases to a good design that is testable. Also I like the way he integrates Interface Builder (also I am looking forward to his next book where he maybe will explain a "big" project doing UI stuff programmatically - I hope).Great Book! All Thumbs Up!
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.