How to write unit tests in Swift 5 using Xcode 12

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
have you asked yourself the following questions what is a unit test how do i write one where do i write one how do what is even testing well if so then grab your cup of coffee or whatever you're drinking at the time and i'll answer those questions in roughly 10 minutes so make sure to stick around for the rest of the video now if you're asking yourself why you should be writing to unit tests then go ahead and check out the video way over there and make sure to come back so that you can actually learn how to so first off what exactly is a unit test well a unit is the smallest piece of code that can be tested so it's the very teeny tiniest piece of your code that you can actually test a unit test is basically a function that goes and calls that piece of code and makes sure that the output from that code is exactly what you specified in that test you can all you always want to make sure to assert proper outcomes so that if your test and if your test fails you don't go rework the test to make sure that it passes you go and rework the code to make sure it passes the test that's just always the rule of thumb so now how do you add a test to an app in xcode here we have an app an app in the xcode it basically when you hit the button and it get when you hit the button add five to the number it just adds five to that number what we're going to test because there are no tests in here if you look in here there are no tests is making sure that adding function is actually working correctly i mean it looks like it's hiding correctly but we don't know exactly if it is how do we do that how do we add a test to an existing app so how do you add a test to an app an xcode maybe you already have an existing app of course when you start a new app it's really easy to just check that button and adds in but you know not not all the time we're starting new apps maybe we need to add one in um so what we're going to do is go into our xcode here of course hit unit test and you'll see that there's targets we actually need to make a new target inside the app so we're going to hit this plus sign down here and there's all these things that pop up and there should be one in here that has unit testing of course if you're doing um if you're doing mac os you might want the mac os watch os all that kind of stuff but in here underneath test not ui testing bundle unit testing bundle go ahead and hit next we have to give it a product name um i'm just going to call it calc app tests or and goes inside here and now we have all the now we have this folder and this target for unit testing app and if we go in here you'll actually see that it already has a folder for tests and it has a bunch of different options in here already given to you this is the first one set up with error tear down with error then we have test example test performance example so the setup with error that's any setup code that you actually want to put in there so maybe if you need to initialize some variables stuff like that that's where setup with error comes in tear down with error it puts the tear is anything that you need to destroy after every test so you always want to make sure your tests are clean you don't want tests to influence each other so you want to make sure that your tests are always clean and then these are just test examples how you write tests that kind of thing we're actually not going to need these test examples or test performance so let's go ahead and take those out so the first thing we're going to do now that we've gone through our test file after we've deleted the test performance in the test example we're going to actually write a test in swift now how do you do that well here's what you do so in import xct test right after that the first thing we need to do is actually import the test app itself so you're going to import whatever app you're working on now the important thing here is that you can't just import right import and then whatever the app name is you actually before the import you actually have to put at test at testable by putting at testable we let xcode know that this unit test app is now testable so we can go through and call these testing functions on itself now the next thing we're going to do is actually create a variable called set system under test that's what set stands for so by having set here it's going to just allow us to quickly call perform functions on it that kind of thing so var set and we have to give it a type so the type that the set is going to be is whatever method function struct whatever you're actually testing on so for us that's going to be the calc function here so inside adding functions there's this calc struct so this calc struct we're going to test the add five and that's the add five function is inside calc so we have to go into the calc out test and give it a give such a calc type and we know it's always going to be calc type so let's just go ahead and force it into that type all right so now in setup with error here's where we actually set up sut to equal the count so the first thing we need to do though is call the super of this claw of this function and we have to use this try because if we notice it has a throws so it could throw an error um try uh sorry i typed that wrong try super dot set up with errors and now we'll do set equals calc that way we know it's new and so now what this is basically allowing us to do is every single time we run this um every time we run this test every time the test a test runs it will always call set up with error and give sut a brand new a brand new calc strut in it so we won't be every time a test is run we won't be using this the same set it'll be a brand new instance we won't be using the same calc it'll be a brand new instance of a calc tear down with air we don't really need anything inside the tear down but if you had something you need to clear out so maybe if this wasn't a a if this wasn't forced to be a calc and it was an optional calc we could return sut back to nil that way at the end of every at the end of every test sut could go back to being to being nil to be nothing so why don't we just keep it that way all right so let's write the actual test so like i said these are tests are functions so tests are functions that test that test something so we're going to test the adding function calc add 5 calc so to do that each test inside xcode must begin with the word test so we'll write a function test and then i like to put an underscore next to test um and then actually describe what function feature whatever i'm testing that's what gets tested so test underscore add five we don't need any parameters to come through and we're not returning anything so now we have our basic test added in here so we know that the add5 function returns an int so we need somewhere to store that int so let's do um let answer equals such dot add five and what number do we want to add it we always want to keep whatever number here add five as a um something really easy so tests we always want to keep super easy so that we know yes it's working or no it's not working so i'm just gonna go ahead and choose five um now we're gonna do now we're gonna write assert equal and what xct assert equal is going to do is it's going to return a success or a failure so based on whether or not expression number one is equal to expression number two so let's hit um answer is expression number one what's five plus five it's ten so we need to make sure that answer is equal to ten that's why we give something easy here so we know that that answer is always right once we hit command u what we'll see is these things here where these play buttons are these little diamonds once these play buttons are will turn green these are these actually give you the access to run tests individually but if you just hit command u it'll run all the tests inside your app and here we are we have green um which is it's nice it tells us what exactly is going on here now if we were to say great answers equal 10 let's go and see what happens when we get a failure so i'll just change this number to six maybe we had a typo now if i go back in here i hit command u it's going to run and we fail the test xct assert failed 11 is not equal to 10. so now we know that there is a failure why is there a failure we can go and check that out and if you see here inside your um inside the inside the file navigator type area there's also a test navigator so you can actually see where your tests are failing now let's go fix that let's put it back to five and then hit command u and we are back to succeeding now so everything is successful which is again what we want you always want to see green mark green check marks along the way that way you know that you're running it correctly all right so this is just one type of an assert there are also you might be wondering at this point like what other type of asserts are there well if we go into apple's documentation what you'll actually see here is there's a lot of different types of asserts so the um you have boolean asserts which will um which will just so they're called xct assert true assert false those are useful if you are returning like a boolean or you just want to make sure the function is actually true or false maybe if you're testing a toggle type function that kind of thing check if your functions if what your if your test condition is nil or non nil so make sure it actually has a value this might be useful if you're taking your set into a if you're taking your site into that nil state um equality inequality you can compare things okay so that's at developer.apple.com documentation xct xc test it's just really really useful to have all these different types of values in here so even the comparable it actually tells you how you should write this um all that kind of stuff so if we go into the one that we just worked on which was equality xct test assert equal you can actually see what the function looks like um and then what the parameters that will it will take um and you can even so you can even see exactly the line number all this kind of stuff in here um and if you notice there's actually a third a third a third expression that can go inside the test option um and that's just gives you a so that you can actually log a message as well in here so if we go in here we can give it a third message of um incorrect edition and let's go into adding functions again change it let's mess it up let's run the test and when you run the test we can see there's an error and it also it will now output the incorrect edition as well here so that's just useful if you have a bunch of test cases you can actually start you know putting in descriptive messages that kind of thing here what is the best structure for writing a test because i didn't actually explain that well the best structure for for writing a test is the given when then structure so how does that exactly work the given when then structure given these conditions remember set up with errors that that's runs every single time you run your tests so this is your given your given is your setup function your when is when these actions are performed so when we actually add five that is your win your then is your assert equal so your then is is your answer actually equal to your 10. so then this is your outcome and that's what i have to say about testing if you have any questions about what i covered or you just want to like join a super cool community make sure you join the discord links in the description below while you're down there and if you like this video make sure to drop a comment or hit the like button and make sure to hit subscribe especially because i have videos about testing swift ui views test driven development and some other testing focused videos coming out soon that you probably don't want to miss if you really enjoyed this video um and if you're looking for more ios testing videos and you're and you're considering going to a continuous integration continuous deployment um process for your app then make sure to check out the playlist and i will catch you next time bye
Info
Channel: maeganwilson_
Views: 412
Rating: undefined out of 5
Keywords: rwite unit tests in swift, unit testing in xcode, swift unit testing tutorial, xcode unit testing swift, how to write a unit test in swift, unit testing in swift, write a unit test in swift, swift testing, write a test for ios app, how to write a test for an ios app, unit testing in xcode swift, swift testing framework, unit testing in swift medium, unit testing in swift tutorial, unit testing in swift 5, swift unit testing
Id: ubUFSpSNehU
Channel Id: undefined
Length: 14min 23sec (863 seconds)
Published: Thu Sep 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.