iOS Test Driven Development by Tutorials First Edition Learn Real World Test Driven Development Joshua Greene 2024 scribd download
iOS Test Driven Development by Tutorials First Edition Learn Real World Test Driven Development Joshua Greene 2024 scribd download
com
https://textbookfull.com/product/ios-test-driven-
development-by-tutorials-first-edition-learn-real-world-
test-driven-development-joshua-greene/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/test-driven-java-development-1st-
edition-farcic-viktor-garcia-alex/
textboxfull.com
https://textbookfull.com/product/test-driven-javascript-
development-1st-edition-gupta-ravi-kumar-prajapati-hetal-singh-
harmeet/
textboxfull.com
https://textbookfull.com/product/test-driven-java-development-invoke-
tdd-principles-for-end-to-end-application-development-2nd-edition-
alex-garcia/
textboxfull.com
https://textbookfull.com/product/thoughtful-machine-learning-with-
python-a-test-driven-approach-first-edition-kirk/
textboxfull.com
https://textbookfull.com/product/test-driven-development-in-ruby-a-
practical-introduction-to-tdd-using-problem-and-solution-domain-
analysis-paranj/
textboxfull.com
https://textbookfull.com/product/design-patterns-by-tutorials-
learning-design-patterns-in-swift-4-2-joshua-greene/
textboxfull.com
No7ce of Rights
All rights reserved. No part of this book or corresponding materials (such as text,
images, or source code) may be reproduced or distributed by any means without
prior written permission of the copyright owner.
No7ce of Liability
This book and all corresponding materials (such as source code) are provided on an
“as is” basis, without warranty of any kind, express of implied, including but not
limited to the warranties of merchantability, fitness for a particular purpose, and
noninfringement. In no event shall the authors or copyright holders be liable for any
claim, damages or other liability, whether in action of contract, tort or otherwise,
arising from, out of or in connection with the software or the use of other dealing in
the software.
Trademarks
All trademarks and registered trademarks appearing in this book are the property of
their own respective owners.
raywenderlich.com 2
iOS Test-Driven Development by Tutorials
Dedica7ons
"For my girls. I love you very much."
— Joshua Greene
— Michael Katz
raywenderlich.com 3
iOS Test-Driven Development by Tutorials
Manda Frederick is the editor of this book. She has been involved
in publishing for over ten years through various creative,
educational, medical and technical print and digital publications,
and is thrilled to bring her experience to the raywenderlich.com
family as Managing Editor. In her free time, you can find her at the
climbing gym, backpacking in the backcountry, hanging with her
dog, working on poems, playing guitar and exploring breweries.
raywenderlich.com 4
iOS Test-Driven Development by Tutorials
James Taylor is a tech editor for this book. He’s an iOS developer
living in San Antonio, Texas with both his wife and daughter. He
enjoys bicycle touring around the United States and spending way
too much time on YouTube. You can find him on Twitter
@jamestaylorios.
raywenderlich.com 5
iOS Test-Driven Development by Tutorials
raywenderlich.com 6
iOS Test-Driven Development by Tutorials
raywenderlich.com 7
iOS Test-Driven Development by Tutorials
raywenderlich.com 8
iOS Test-Driven Development by Tutorials
raywenderlich.com 9
iOS Test-Driven Development by Tutorials
raywenderlich.com 10
iOS Test-Driven Development by Tutorials
raywenderlich.com 11
iOS Test-Driven Development by Tutorials
raywenderlich.com 12
iOS Test-Driven Development by Tutorials
raywenderlich.com 13
I Introduc7on
Welcome to iOS Test-Driven Development by Tutorials! This book will teach you all
about test-driven development (TDD) — the art of turning requirements into tests
and tests into production code.
You'll get hands-on TDD experience by creating three real-world apps in this book:
By the end of this book, you'll have a strong understanding of TDD and be able to
apply this knowledge to your own apps.
raywenderlich.com 14
iOS Test-Driven Development by Tutorials Introduction
If you’ve worked through our classic beginner books — the Swift Apprentice https://
store.raywenderlich.com/products/swift-apprentice and the iOS Apprentice https://
store.raywenderlich.com/products/ios-apprentice — or have similar development
experience, you’re ready to read this book. You'll also benefit from a working
knowledge of design patterns — such as working through Design Patterns by Tutorials
https://store.raywenderlich.com/products/design-patterns-by-tutorials — but this
isn't strictly required.
As you work through this book, you’ll progress from beginner topics to more
advanced concepts.
Sec7on introduc7ons
I. Introduc7on
This is a high-level introduction to TDD, explaining why it's important and how it
will help you.
You'll also be introduced to the TDD Cycle in this section. This is the foundation for
how TDD works and guiding principles on the best way to apply it.
The chapters in this section build an example app called Fitness. This is the premier
fitness-coaching app based on the "Loch Ness" workout: You'll have to outrun,
outswim and outclimb Nessie (or get eaten)!
raywenderlich.com 15
iOS Test-Driven Development by Tutorials Introduction
You'll create an app called Dog Patch throughout this section. Dog Patch lets dog
lovers everywhere connect with kind breeders to help get the dog of their dreams.
You'll update an app called MyBiz throughout this section. MyBiz is an enterprise
resource planning (ERP) app for running a business, including employee
management and scheduling, time tracking, payroll and inventory management.
If you already have some experience with TDD, you can skip from chapter to chapter
or use this book as a reference. You'll always be provided with a starter project in
each chapter to get up and running quickly.
What's the absolute best way to read this book? Just start reading wherever makes
sense to you!
raywenderlich.com 16
W What You Need
• Xcode 11 or later. Xcode is the main development tool for writing code in Swift.
You need Xcode 11 at a minimum, since that version includes Swift 5.1. You can
download the latest version of Xcode for free from the Mac App Store, here:
apple.co/1FLn51R.
If you haven't installed the latest version of Xcode, be sure to do that before
continuing with the book. The code covered in this book depends on Swift 5.1 and
Xcode 11 — the code may not compile if you try to work with an older version.
raywenderlich.com 17
L Book License
• You are allowed to use and/or modify the source code in iOS Test-Driven
Development by Tutorials in as many apps as you want, with no attribution
required.
• You are allowed to use and/or modify all art, images and designs that are included
in iOS Test-Driven Development by Tutorials in as many apps as you want, but must
include this attribution line somewhere inside your app: “Artwork/images/designs:
from iOS Test-Driven Development by Tutorials, available at
www.raywenderlich.com”.
• The source code included in iOS Test-Driven Development by Tutorials is for your
personal use only. You are NOT allowed to distribute or sell the source code in iOS
Test-Driven Development by Tutorials without prior authorization.
• This book is for your personal use only. You are NOT allowed to sell this book
without prior authorization, or distribute it to friends, coworkers or students; they
would need to purchase their own copies.
All materials provided with this book are provided on an “as is” basis, without
warranty of any kind, express or implied, including but not limited to the warranties
of merchantability, fitness for a particular purpose and noninfringement. In no event
shall the authors or copyright holders be liable for any claim, damages or other
liability, whether in an action or contract, tort or otherwise, arising from, out of or in
connection with the software or the use or other dealings in the software.
raywenderlich.com 18
iOS Test-Driven Development by Tutorials Book License
All trademarks and registered trademarks appearing in this guide are the properties
of their respective owners.
raywenderlich.com 19
B Book Source Code &
Forums
• https://store.raywenderlich.com/products/ios-test-driven-development-source-
code
Forums
We’ve also set up an official forum for the book at forums.raywenderlich.com.This is
a great place to ask questions about the book or to submit any errors you may find.
raywenderlich.com 20
iOS Test-Driven Development by Tutorials Book Source Code & Forums
Buying the digital edition version of the book also has a few extra benefits: free
updates each time we update the book, access to older versions of the book, and you
can download the digital editions from anywhere, at anytime.
• https://store.raywenderlich.com/products/ios-test-driven-development.
And if you purchased the print version of this book, you’re eligible to upgrade to the
digital editions at a significant discount! Simply email [email protected] with
your receipt for the physical copy and we’ll get you set up with the discounted digital
edition version of the book.
raywenderlich.com 21
Sec7on I: Hello, TDD!
• Chapter 2: The TDD Cycle: TDD has four steps known as the Red-Green-
Refactor Cycle.
raywenderlich.com 22
1 Chapter 1: What Is TDD?
By Joshua Greene
3. Refactor
4. Repeat
This is called the TDD Cycle. It ensures you thoroughly and accurately test your
code because your development is... driven by testing!
raywenderlich.com 23
iOS Test-Driven Development by Tutorials Chapter 1: What Is TDD?
By writing a test followed by the production code to make it pass, you ensure your
production code is testable and that it meets all of your requirements during
development. As an added bonus, your tests act as documentation for your
production code, describing how it works.
On the surface, the TDD process seems pretty simple. Well, I’m sorry to tell you
that... wait, it actually is really simple!
Sure, there are special circumstances for how to implement this cycle at times, but
that’s where this book comes in! Once you get the hang of this process, it will
become second nature. You’ll learn a lot more about this process in the next chapter.
It’s hard to argue against testing your code, but you don’t have to follow TDD to do
this. For example, you could write all of your production code and then write all of
your tests. Alternatively, you could skip writing tests altogether and, instead,
manually test your code. Why is TDD better than these options?
Good tests ensure your app works as expected. However, not all tests are "good."
Writing tests for the sake of having tests isn’t a worthwhile exercise. Rather, good
tests are failable, repeatable, quick to run and maintainable.
• The first step is to write a failing test. By definition, this proves the test is failable.
Tests that can’t fail aren’t very useful. Rather, they waste valuable CPU time.
• Before you’re allowed to write a new test, all other previous tests must pass. This
ensures that your tests are repeatable: You don’t just run the single test you’re
working on, but rather, you constantly run all of the tests.
• By frequently running every test, you’re incentivized to make sure tests are quick
to run. All of your tests should take seconds to run — preferably, one second or
less.
A single test that takes a hundred milliseconds is too slow: After only ten tests,
your entire test suite will take one second to run. After fifty tests, it takes five
seconds. After several seconds, no one runs all of the tests because it takes too
long.
raywenderlich.com 24
iOS Test-Driven Development by Tutorials Chapter 1: What Is TDD?
• When you refactor, you update both your production and test code. This ensures
your tests are maintained: You’re constantly keeping them up-to-date.
• By iteratively writing production code and tests in parallel, you ensure your code is
testable. If you were to write tests after completing the code, it’s likely the
production code would require quite a bit of refactoring to fully unit test.
Nonetheless, the devil’s advocate in you may say, "But you could write good tests
without following TDD." You definitely could, but you may struggle to succeed. You
can definitely do it in the short term, but it’s much more difficult in the long term.
You’d need to be disciplined about writing good tests. Before long, you’d likely create
some sort of system to ensure that you’re writing good tests... you’d likely find
yourself doing a variant of TDD!
• Do write tests for code that can’t be caught in an automated fashion otherwise.
This includes code in your classes’ methods, custom getters and setters and most
anything else you write yourself.
• Don’t write tests for generated code. For example, it’s not worthwhile to write
tests for generated getters and setters. Swift does this very well, and you can trust
it works.
• Don’t write tests for issues that can be caught by the compiler. If the tested issue
would generate an error or warning, Xcode will catch it for you.
• Don’t write tests for dependency code, such as first- or third-party frameworks
your app uses. The framework authors are responsible for writing those tests. For
example, you shouldn’t write tests for UIKit classes because UIKit developers are
responsible for writing these. However, you should write tests for your custom
subclasses thereof: This is your custom code, so you’re responsible for writing the
tests.
Another exception is "sanity tests" that prove third-party code works as you expect.
These sort of tests are useful if the library isn’t fully stable, or you don’t trust it
raywenderlich.com 25
iOS Test-Driven Development by Tutorials Chapter 1: What Is TDD?
entirely. In either case, you should really scrutinize whether or not you want to use
the library at all — is there a better option that’s more trustworthy?
Fortunately, TDD gets faster once you get used to doing it. However, the truth is that
compared to not writing any tests at all, you’re writing more code ultimately. It likely
will take a little more time to develop initially.
That said, there’s a really big hole in this argument: The real time cost of
development isn’t just writing the initial, first-version production code. It also
includes adding new features over time, modifying existing code, fixing bugs and
more. In the long run, following TDD takes much less time than not following it
because it yields more maintainable code with fewer bugs.
There’s also another cost to consider: customer impact of bugs in production. The
longer an issue goes undiscovered, the more expensive it is. It can result in negative
reviews, lost trust and lost revenue.
If an issue is caught during development, it’s easier to debug and quicker to fix. If
you discovered it weeks later, you’d spend substantially more time getting up to
speed on the code and tracking down the root cause. By following TDD, your tests
ultimately help safeguard and protect your app against bugs.
However, an important question to ask: Should your project use TDD at all?
As a general rule of thumb, if your app is going to last more than a few months, will
have multiple releases and/or require complex logic, you’re likely better off using
TDD than not.
raywenderlich.com 26
iOS Test-Driven Development by Tutorials Chapter 1: What Is TDD?
If you’re creating an app for a hackathon, test project or something else that’s meant
to be temporary, you should evaluate whether TDD makes sense. If there’s really only
going to be one version of the app, you might not follow TDD or might only do TDD
for critical or difficult parts.
Ultimately, TDD is a tool, and it’s up to you to decide when it’s best to use it!
Key points
In this chapter, you learned what TDD is, why you should use it, what to test and
when to use it. Here are the key points to remember:
• Write tests for code that you’re responsible for maintaining. Don’t test code that’s
automatically generated or code within dependencies.
• The real cost of development includes initial coding time, adding new features
over time, modifying existing code, fixing bugs and more. TDD reduces
maintenance costs and quantity of bugs, often making it the most cost effective
approach.
• TDD is most useful for long-term projects lasting more than a few months or
having multiple releases.
raywenderlich.com 27
2 Chapter 2: The TDD Cycle
By Joshua Greene
In the previous chapter, you learned that test-driven development boils down to a
simple process called the TDD Cycle. It has four steps that are often "color coded" as
follows:
2. Green: Write the bare minimum code to make the test pass.
raywenderlich.com 28
iOS Test-Driven Development by Tutorials Chapter 2: The TDD Cycle
Why is it color coded? This corresponds to the colors shown in most code editors,
including Xcode:
This chapter provides an introduction to the TDD Cycle, which you'll use throughout
the rest of this book. However, it doesn't go into detail about test expressions
(XCTAssert, et al.) or how to set up a test target. Rather, these topics are covered in
later chapters. For now, focus on learning the TDD Cycle, and you'll learn the rest as
you go along.
GeXng started
In this chapter, you'll create a simple version of a cash register to learn the TDD
Cycle. To keep the focus on TDD instead of Xcode setup, you'll use a playground.
Open CashRegister.playground in the starter directory, then open the
CashRegister page. You'll see this page has two imports, but otherwise it's empty.
Naturally, you'll begin with the first step in the TDD Cycle: red.
raywenderlich.com 29
Another Random Scribd Document
with Unrelated Content
määrin. Kaupungin ahtaiden katujen varsilla, missä useimmat talot
ovat vielä vanhaan tapaan rakennetut, näkyy lukuisten venäläisten
kauppiasten ja käsityöläisten nimikilpien joukossa myös sellaisia
kirjoituksia kuin: Bottier de Paris (pariisilainen suutari) ja Marchande
de Modes (muotikauppiatar). Sellainen rehentely ei kuitenkaan voine
pettää herkkäuskoistakaan. Tavara, mitä näistä kaupoista voi ostaa,
on sellaista, ettei piintyneinkään takalistolainen voi olla epätietoinen
sen alkuperästä.
*****
Kauan ennen sitä aikaa, josta nyt on puhe, tuli Moreno eräänä
yönä jokseenkin myöhään kotiin iltakutsuista kenraalikuvernöörin
luota ja näki kaukaa autiolla kadulla, joka kulkee tataarilaiskaanin
entisen, nyt ruutimakasiiniksi muutetun palatsin ohi, naisolennon,
joka astui samaan suuntaan kuin hänkin. Oli talvi ja kylmä, lumi peitti
maata useampien tuumain vahvuudelta, kaikki oli jäässä ja yö
synkimmillään.
Kun hän suuttui, tuli häneltä verta suusta, ja hänellä oli tuntikausia
tuskia. Annoin siis perään.
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the terms
of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth in
paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com