Your next career begins with
Save 50% off your seat in our next iOS Bootcamp. Limited time only. Sessions start April 3.
Your First iOS & SwiftUI App: An App from Scratch
Jan 11 2022 Swift 5.5, iOS 15, Xcode 13
Part 3: Coding in Swift
23. Intro to Test-Driven Development

Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
About this episode
Learn how you can use test-driven development to write tests for your code before you write the code itself and why this can be beneficial to you.
Instructors
Contributors
Instructor
Instructor
Illustrator
Video Editor
Over 300 content creators. Join our team.
Version history
iOS 15, Swift 5.5, Xcode 13 (Selected)
Jan 11 2022iOS 16, Swift 5.7, Xcode 14
Feb 13 2023iOS 14, Swift 5, Xcode 12
Dec 15 2020iOS 13, Swift 5, Xcode 11
Sep 3 2019iOS 12, Swift 4, Xcode 10
Jul 24 2018iOS 11, Swift 4, Xcode 9
Sep 19 2017iOS 10, Swift 3, Xcode 8
Nov 7 2016Swift 2
Sep 1 2015Leave a rating/review
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
This video Intro to Test-Driven Development was last updated on Jan 11 2022
Now that you understand the basics of Unit Testing, I want to introduce you to a way of writing apps that some developers enjoy: Test-Driven Development.
The basic idea of Test-Driven Development is rather than writing tests AFTER you write your code, you write your tests FIRST.
Obviously, at first your tests will fail, because you haven’t written the code yet. But then when you write your code, you have some nice confidence that it works exactly the way you intended it to.
One of the nice things about test-driven development is it forces you to put some thought up-front about how you want your code to work, before you write them. It’s also a great way to ensure you definitely write tests for all your code, instead of forgetting to test some bits. In some cases, it can also result in improved code quality, and increased speed of development.
Note that not every developer likes to use Test-Driven development; and even when developers do, sometimes they only use it for certain parts of their app, like only for the data models.
Basically, whether or not to use Test-Driven Development comes down to a personal choice if this is a way you like to work, or not. We’re going to try out Test-Driven development throughout the rest of the course, and that way you can understand how it works, and if it’s something you might like to incorporate into your workflow in the future.
For this particular episode, we’re going to try out Test-Driven Development to test the Game’s points method. Right now we’ve temporarily hard-coded the method to always return 999. The next item on our programming to-do list is to properly calculate the user’s score.
Again, we could jump straight into implementing the points method, but remember that TDD says you should write your tests first, THEN write the code. So let’s write a few tests that will currently fail, but should work later on once we write the code.
Let’s give this a try!
Delete test example method, and add these:
func testScorePositive() {
var guess = game.target + 5
var score = game.points(sliderValue: guess)
XCTAssertEqual(score, 95)
}
func testScoreNegative() {
var guess = game.target - 5
var score = game.points(sliderValue: guess)
XCTAssertEqual(score, 95)
}
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development — plans start at just $19.99/month! Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.
Learn more