Intro to Unit Testing in Swift

  Рет қаралды 32,216

Sean Allen

Sean Allen

Күн бұрын

Go to squarespace.com/seanallen to save 10% off your first purchase of a website or domain using code SEANALLEN.
Unit Testing in Swift is a fundamental skill that will save you from errors and regressions. In this video I tell you what Unit Tests are and discuss the benefits of writing them. Then I show you a real example in code using a Tip Calculator app and then we discuss if writing Unit Tests is worth it for your project.
My iOS Dev Courses:
seanallen.teachable.com/
Twitter:
Sean Allen - / seanallen_dev
Hired.com:
hired.com/x/1n01g
Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):
Paul Hudson's Hacking With Swift:
gumroad.com/a/762098803
Donny Wals - Combine:
gumroad.com/a/909014131
Mark Moeyken’s SwiftUI Books:
www.bigmountainstudio.com/swiftui-views-book/fzc51
Objc.io Books (Thinking in SwiftUI & Advanced Swift):
gumroad.com/a/656585843
Timestamps:
0:00 - What are Unit Tests?
1:40 - Benefits of Unit Tests
3:15 - Unit Test code example begins
4:55 - Add Test Target to project & files
6:55 - First Unit Test (Happy Path)
11:04 - Unit Test for negative number
15:12 - Tests aren't always worth it - here's why
#swift #softwaredeveloper #iosdeveloper

Пікірлер: 81
@seanallen
@seanallen Жыл бұрын
My iOS Dev Courses (Swift, SwiftUI & UIKit) - seanallen.teachable.com
@KeshenMac
@KeshenMac Жыл бұрын
Hi Sean, what course is this video from? Thanks!
@seanallen
@seanallen Жыл бұрын
This video is stand alone. It's not from an existing course. None of my current courses go into Unit Testing. I plan on implementing testing in a future course.
@mario_luis_dev
@mario_luis_dev Жыл бұрын
hey Sean, is there overlap between the "iOS Dev Job Interview Practice" course and the "SwiftUI Mega Bundle"? I'm getting into iOS development, and I would love to take one of your courses, but I'm not sure which of these two would be a better option for me.
@seanallen
@seanallen Жыл бұрын
No overlap. iOS Dev Job Interview Practice focuses on a take home project built in UIKit and programatic UI. The SwiftUI Mega Bundle is all my courses that focus on SwiftUI. So the answer to which way you should go depends on if you want to focus on UIKit or SwiftUI.
@mikewelch6970
@mikewelch6970 Жыл бұрын
I'm at the point where I'm learning job-ready skills. unit testing was next on my list to tackle and Then the goat pops out with a video about unit testing.
@seanallen
@seanallen Жыл бұрын
Happy to help, Mike. Best of luck getting ready for the job hunt.
@SuaveShit
@SuaveShit Жыл бұрын
What’s your job-ready checklist?
@cohaya1
@cohaya1 Жыл бұрын
@@SuaveShitGit , thoroughly understanding json and file systems, postman , unit testing, architecture, design patterns.
@absar66
@absar66 Жыл бұрын
Thank you Sean, you have a great ability of making any complex notion/concept into simple …keep going 👍
@keef_khan
@keef_khan Жыл бұрын
Straightforward and easy to grasp. Thanks I've been hearing about unit testing for years but didn't actually see it in action until now.
@seanallen
@seanallen Жыл бұрын
Happy to help.
@neil9251
@neil9251 Жыл бұрын
Thanks for the nicely explained summary! Easily the most straightforward explanation and example I’ve seen. Will look to adopt this at least partially into my apps.
@seanallen
@seanallen Жыл бұрын
Glad it was helpful, Neil!
@abs80900
@abs80900 Жыл бұрын
thank you so much! I find your teaching style works for me so well, I'm so lucky and thanks a lot for your hard work and kindness to share
@seanallen
@seanallen Жыл бұрын
Happy to hear you enjoyed it!
@ammarahmad9004
@ammarahmad9004 Жыл бұрын
I enjoyed this topic, Its bout 9 months since I start writing tests for my code, and I think it is very important (if your code will not change in the soon future) because it makes you sure that your app has a solid foundation and you can be built more features upon it. Thanks Allen
@seanallen
@seanallen 11 ай бұрын
Glad you liked it :)
@samuelp7847
@samuelp7847 8 ай бұрын
I have a mobile app on the App Store. Unit tests didn’t make sense building the MVP. Now that it’s live, it’s so helpful knowing my tests pass before releasing an update. That’s why they make sense for me.
@seanallen
@seanallen 8 ай бұрын
I agree. When you are just starting out with the MVP test aren't important. Your product is going to change too much to take the time to write tests (in my opinion). It's not worth the sacrifice in developments speed at that early stage in the products life.
@captainmichaelj2321
@captainmichaelj2321 Жыл бұрын
Thanks so much for this video! Now I really have very good visualization of unit tests and whether they are really important to me. Guess I will stick to my own simulator/device testing for the time being.
@seanallen
@seanallen Жыл бұрын
Glad it was helpful!
@user-fc8hd6kh2f
@user-fc8hd6kh2f Жыл бұрын
always happy to see a new video!!!
@seanallen
@seanallen Жыл бұрын
Hope you enjoyed it!
@jonw4671
@jonw4671 Жыл бұрын
Crazy. I have just been listening to swift over coffee s1, listening to you talking about testing or lack of testing in some cases 😂
@davidruvinskiy6347
@davidruvinskiy6347 Жыл бұрын
Thanks, Sean! Superb as always.
@seanallen
@seanallen Жыл бұрын
Glad you enjoyed it, David!
@rparham1997
@rparham1997 Жыл бұрын
Thanks! Been waiting for this one.
@seanallen
@seanallen Жыл бұрын
Hope you like it!
@CraigSchubert
@CraigSchubert Жыл бұрын
Great intro to testing - really helped me get an idea of where to start. Thanks!! 🙂
@seanallen
@seanallen Жыл бұрын
happy to help, Craig.
@femialiu
@femialiu Жыл бұрын
This is coming at the right time. Just getting into testing. Thanks a lot Sean. Will we see other testing videos?
@seanallen
@seanallen Жыл бұрын
Possibly. But I have a very long "idea list" of videos I need to get to.
@femialiu
@femialiu Жыл бұрын
@@seanallen I understand. Thanks for all the good stuff you put out.
@alejmc
@alejmc Жыл бұрын
This is my first time looking into anything XCode tests related frameworks. I like it and a lot… I have read and/or entertained Test Driven Development (as a hobby, never in practice) and can get behind the benefits. There’s some functions that just get harder and tricky to test by having to launch the whole app constantly and getting to the state where the bug might or might not trigger… Those tests can run larger loops to test a lot of different numbers, file or directory paths (for me a pain to handle always no matter the language if it has to go through the whole UI to test), so on and so forth. What makes me wonder though, does XCode testing tools offer the possibility to simulate an environment? Let’s suppose our app needs to save to the Documents folder, so a test that finds the right folder and file name paths is understandable. And whether the app has access to the Documents folder enabled or not can be handled by an injected Boolean. But would it be possible to test the process of that? As in: “testSaveToFileAsksForDownloadsFolderAccess() -> void { // assert that this asks for download folder access if there’s no access available }”, or something like that where before when the test is run, macOS itself removes access to that.
@yevhen_san
@yevhen_san Жыл бұрын
Thank you for clear explanation 🙏
@seanallen
@seanallen 11 ай бұрын
Glad it was helpful!
@TheRicherthanyouguy
@TheRicherthanyouguy Жыл бұрын
I've been coding a number of years and I really don't think any professional developer should deploy an app without at least about 10 % of something they have written having some form of unit testing. Even if its just we made the file did some set up and covered a single test case. Just enough to set up the foundation for adding more tests. I say this because I've found it's harder to add the very first test for code but once you get past that very first one. The rest are waaaaayyyyyy easier.
@aquinn4260
@aquinn4260 Жыл бұрын
Fantastic - Unit Tests are exactly the sort of skill that employers are looking for experience in when interviewing for Junior iOS Devs (I’m finding!)
@seanallen
@seanallen Жыл бұрын
This is true.
@af2b
@af2b 9 ай бұрын
thanks Sean!
@user-xd9gt6pc6d
@user-xd9gt6pc6d Жыл бұрын
Do you have any plans to offer courses on unit testing in the future? Thank you for your video; it has been very helpful to me.
@seanallen
@seanallen Жыл бұрын
I plan to implement unit testing into a future course. Unit testing won't be the focus of the course, but it will be added into an app we build.
@MassonLifestyle
@MassonLifestyle 4 ай бұрын
Another great video. I finally understand what those new folders that keep popping up on any new app that I make and why I need them. Sean, I wanted to ask, given that Xcode updates often and knowing they are requiring that all apps on the App Store are updated to Xcode 15 is that what are you using on your current apps, OR are you coding up to Xcode 17.2? Struggling to figure out how updated my app should be given users are not updating their devices as quickly as Xcode is being updated.
@seanallen
@seanallen 4 ай бұрын
This is a good question. Too much to give you the exact answer in a KZfaq comment, but you can separate certain code based on the user's iOS version. A basic example is... if iOS 17 { do this code } else if iOS 16 { do this code }. To point you in the right direction google the @available keyword in swift and you should find some tutorials.
@MassonLifestyle
@MassonLifestyle 4 ай бұрын
@@seanallen Thanks for answering. I do remember in your video for swift mentioning this. Thanks again, I will look this up. Til then! I saw you posted a new video so I will watch that one soon.
@seanallen
@seanallen 4 ай бұрын
@@MassonLifestyle Hope you enjoy!
@wayneosaur
@wayneosaur Ай бұрын
Thanks for the video. The `@testable import` helps resolve compiler time errors, but I still get build errors (linker errors). There must be something I need to place in the Build Setting to make this work. Furthermore, importing the entire app seems like overkill for really large projects.
@andrewboryk3904
@andrewboryk3904 5 ай бұрын
Documentation and tests are the most neglected elements of software development, but are the most critical reasons why engineering teams begin to unravel as they scale. My suggestion, whether you are writing simple or complicated functions, over-document it with comments. What is each variable being declared, why does this conditional cause an early return, what outputs can we expect from this function. For tests, even when the team is small and scrappy, I recommend using them if your application has complex logic on the frontend. Granted, I always recommend putting business logic on the backend, but there is still a lot of frontend logic that determines UI state and navigation that is worth writing tests for. One example is if you have a list of lists, and there is some multi-select functionality that takes into account minimums and maximums, and auto-navigating to the next lists, write a test for it. General rule of thumb: documentation comment as you write, and if it is complicated for you to explain, then it deserves tests.
@johnheaney3349
@johnheaney3349 11 ай бұрын
I've worked on embedded firmware for medical devices and unit testing is required. It's a very significant amount of work and unpleasant to boot. Other clients without FDA requirements rarely want to pay for unit testing, but our company did do some level of unit testing anyway to protect ourselves from unwanted surprises. Releasing code for manufacturing processes, for example, may not fall under FDA quidelines, but clients will still be very upset if something goes wrong. That said, one approach is to install unit tests in your project so that you have the infrastructure in place. Then when you do run into a bug, write unit tests to expose that bug. Then when you fix the bug you can be confident that a real world bug will not reappear some time in the future. You're probably covering some other bugs, as well. It also serves as a documented history of bug fixes.
@vamsi3877
@vamsi3877 Жыл бұрын
Nice Video Sean.Please made a video on API Testing with mock data.
@seanallen
@seanallen Жыл бұрын
I'll add it to the idea list!
@Art-is-craft
@Art-is-craft 7 ай бұрын
Unit testing is not just about finding errors it can be a useful tool to help design functions and can allow you to visualise data in terms of a function.
@christophrohde1415
@christophrohde1415 3 ай бұрын
What are the state-of-the-art style for benchmarks in Swift?
@Jake-Clean-Coder
@Jake-Clean-Coder 9 ай бұрын
When I try to create a target for testing it says none on targets to be tested and I can't figure out why it is not allowing me to select the main Target or any for that matter. Please help!!
@develocode7773
@develocode7773 Жыл бұрын
Thanks!
@seanallen
@seanallen Жыл бұрын
I appreciate the generous support! Glad it was helpful.
@theiosdeveloper555
@theiosdeveloper555 Жыл бұрын
Great code!
@seanallen
@seanallen Жыл бұрын
Thanks!
@theiosdeveloper555
@theiosdeveloper555 Жыл бұрын
@@seanallen you're welcome :)
@Neko-Pro-Watcher
@Neko-Pro-Watcher 5 ай бұрын
Thanks.
@seanallen
@seanallen 5 ай бұрын
You're welcome
@velcharangovindarajusreedh3048
@velcharangovindarajusreedh3048 Жыл бұрын
It’s really very useful video for developers, I followed all your playlists videos on time. Can you continue XCTestCases video using API calling.
@seanallen
@seanallen Жыл бұрын
I'll add it to the video idea list!
@AmitBiswastunebox
@AmitBiswastunebox Жыл бұрын
🎉 awesome Sean
@seanallen
@seanallen Жыл бұрын
Glad you liked it!
@AmitBiswastunebox
@AmitBiswastunebox Жыл бұрын
@@seanallen I always waited for your video lol. I love your teaching style, content progression and quality of video.
@youngdomsey
@youngdomsey 3 ай бұрын
Hello I am new to Swift. As soon as I rename my unit test from "testAdd" to just for example "add" or simply something without the "test" prefix my test disappears. Is it necessary in Swift for tests to have the "test" prefix?
@seanallen
@seanallen 3 ай бұрын
Yes, the test prefix is required. That's how the system knows that piece of code is a test.
@youngdomsey
@youngdomsey 3 ай бұрын
@@seanallen thank you!
@yevhen_san
@yevhen_san Жыл бұрын
The test class will have access to the Calculation struct if it will be a separate file.
@tylerwatt5651
@tylerwatt5651 Жыл бұрын
YESSS!!!!
@seanallen
@seanallen Жыл бұрын
Hope you enjoyed it!
@mikebillysauer-nickerson6136
@mikebillysauer-nickerson6136 8 ай бұрын
Actually, regression tests literally mean “going back” and testing what you did before to make sure nothing broke,it’s not that you are preventing code breakage from regressing functionality.
@jhoughjr1
@jhoughjr1 Жыл бұрын
I really miss mocks and swift makes cheap mocks impossible.
@rafael.setragni
@rafael.setragni 9 ай бұрын
"Look! I designed this exotic vehicle that can reach over 100 Km/H! It's all done; it just needs to be tested. Do you want to drive it?" "Look! I built this building entirely from scratch, and it's all done! It just needs the calculations checked. Do you want to buy it?" Come on, guys! If you want to ensure that your work is correct, you need TO TEST IT, PLEASE! There is no such thing as "Everything is ready to go, just needs testing."
@nroose
@nroose 6 ай бұрын
Why does it need a device to run a unit test? Seems so silly.
@JasonMitchellAZ
@JasonMitchellAZ Жыл бұрын
Unit testing is great (and important!)... We've added tests at my jobs, but now I need to figure out how to get my side app (which is using Core Data) to have some unit tests!
@seanallen
@seanallen Жыл бұрын
Test all the things! (kidding)
@JasonMitchellAZ
@JasonMitchellAZ Жыл бұрын
@@seanallen You better get that 100% coverage in all your apps!
@victorriurean
@victorriurean Жыл бұрын
Swift API Calls for Beginners (Networking) - Async Await & JSON
25:35
Swift Closures Explained
14:23
Sean Allen
Рет қаралды 61 М.
Playing hide and seek with my dog 🐶
00:25
Zach King
Рет қаралды 29 МЛН
Я нашел кто меня пранкует!
00:51
Аришнев
Рет қаралды 5 МЛН
- А что в креме? - Это кАкАооо! #КондитерДети
00:24
Телеканал ПЯТНИЦА
Рет қаралды 7 МЛН
The ONLY REASON To Unit Test
8:26
Theo - t3․gg
Рет қаралды 74 М.
Swift - What to Ask Them? - iOS Interview Questions
13:31
Sean Allen
Рет қаралды 31 М.
5 Types of Testing Software Every Developer Needs to Know!
6:24
I Tried Every AI Coding Assistant
24:50
Conner Ardman
Рет қаралды 737 М.
Swift. Как писать unit тесты
18:02
iOS Skills
Рет қаралды 5 М.
I Asked GPT-4 To Refactor My Legacy Codebase
12:39
Nick Chapsas
Рет қаралды 348 М.
Your App Will Get Rejected | New Privacy Rules - 2024
11:26
Sean Allen
Рет қаралды 35 М.
Swift - Class vs. Struct Explained
7:28
Sean Allen
Рет қаралды 31 М.
Choose a phone for your mom
0:20
ChooseGift
Рет қаралды 7 МЛН
EXEED VX 2024: Не өзгерді?
9:06
Oljas Oqas
Рет қаралды 46 М.