Sign in to confirm you’re not a bot
This helps protect our community. Learn more
How to write your first Unit Test in Swift 📱 (Free Tutorial, Beginner Level)
110Likes
3,506Views
2023Feb 21
#iOS #swift #softwaredeveloper #iosdeveloper In this video, I'll show you how to write your first unit test in Swift. Unit testing is a software testing technique in which individual units or components of a software application are tested in isolation from the rest of the application. By writing unit tests, you can validate that your code works as expected, and catch any bugs or errors before they become a problem in your production app. In Swift, you can write unit tests using the XCTest framework. I'll show you how to set up a unit test project in Xcode, how to write and run your first test, and how to use XCTAssert to verify that your code works as expected. I'll also show you how to write tests that validate the behavior of your code under different conditions. By the end of this video, you'll have a good understanding of how to write unit tests in Swift. Thank you for watching this video 🙌 ➜ Website: https://www.swiftwithvincent.com ➜ Twitter:   / v_pradeilles   ➜ GitHub: https://github.com/vincent-pradeilles/ ➜ LinkedIn:   / vincentpradeilles   TRANSCRIPT I'm going to show you how to write your first unit test in Swift. So let's get started. I've implemented a simple view model that allows me to store a list of people along with a list of filters, and then only display the people that satisfy all of these filters. And now I want to write some tests to make sure that my view model does behave like I expect it to. For this I'm going to move into my testing target. If you don't already have a testing target in your project, you can create one by clicking on this plus icon right here, and then adding a new unit testing bundle to your project. You can notice that the Swift file that will contain my testing code looks a little bit different than the ones I have in my app. It imports XCTest, which is Xcode's testing framework. It does an atTestable import of my app. This is important because this atTestable import will allow me to access the properties of my view model that have an internal visibility and would otherwise be inaccessible outside my app. Finally, it defines a subclass of XCTestCase, which is the base class we need to use to write our tests. But before we start writing our first test, we are first going to add a static constant that will hold the data we're going to use in all our tests. And now we are all set to implement our first test. To implement a test, we simply start by defining a new method whose name begins with test and then describes the behavior we're testing. For this first test, I want to test that my view model behaves correctly when I give it a filter for the property firstName, so I write the name of the test accordingly. Then I'm going to structure the body of my test into three parts. Given, when, and then. In the given part, I set up the context, meaning I create my view model with its initial data. In the when part, I perform the action I want to test. Here I set up my filter. Finally, in the then part, I check that my view model has behaved as I expected by comparing the result it gives with my expected result. And that's it! I can now run my test to make sure that my view model is indeed behaving as I expect it to. Of course, one test is not enough to cover all the possible situations. So let's write a few more tests. Using the same approach than for the first test, I'm now going to write a test for the case where I set multiple filters. Then I'm going to write a couple more tests that will cover the edge cases, because that's often there that you find some bugs. First, I write a test for the case where a combination of filters produces no results. Then I write another test for the case where I set no filters. To finish, I'm going to run all the tests to make sure that my view model passes all of them. And that's it! We've implemented our first unit tests. As we've seen, the idea behind a unit test is pretty straightforward. We simply write code that asserts that under a given situation, our code does behave like we expect it to behave. That's all for this video, I hope you've enjoyed it. Remember to subscribe to the channel, it's really a big support for me. Thank you for watching, and see you next time!

Follow along using the transcript.

Vincent Pradeilles

18.7K subscribers