iOS Dev 30: Getting Started with Unit Testing | Swift 5, XCode 12

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome back to my channel for those of you who are new my name is manuel and today we're going to be learning how to write um uni tests for our ios applications right so just go ahead and we're going to create a new project and this is going to be an app well single view application i'm going to call this unit testing okay so um if you're going to be working with if you want to add like tests to a new application then just go ahead and check this box over here to include that from from the beginning right but if you're going to be um adding a test to an existing application i'm going to show you how that is done so i'm going to uncheck this for now and i'm going to click on next and then in our desktop i'm just going to go ahead and create this project awesome let's expand this expand that right now uh before we actually get to um like writing tests let me just take a few minutes to explain why um writing tests can actually be very beneficial to us right so what i'm going to do is i'm going to create a i'm going to create a class called um should we call this let's call it arithmetic all right so basically what we want this class to do is we want this class to um allow us to be able to perform certain arithmetic operations so things like addition subtraction multiplication division right so what i'm going to do here is i'm going to create a class called arithmetic like that and then right here i'm going to create a function called add and this is going to take in two parameters the first one is going to be num1 and then num2 and this is going to return an integer okay so um let's go ahead and just copy okay before then let's just add the function or the functionality like what i'm doing over here let's just type in so this is going to be num1 plus num2 simple enough right cool so we're going to create um three more and this is going to be sub this is going to be multiply like that and this is going to be div so um they are quite similar just the operations are different so this is going to be star starts and this is going to be divided okay so very simple right very simple now um in our view controller what we want to do is we want to use this class that we've just made so i'm just going to come over here and at the top i'm going to say let arith or let's call it math let math be equal to arithmetic what arithmetic like that and in our viewed load let's just perform some operations okay so i'm going to say print um addition of 2 and 2 is and then right here i'm going to say math dot add and this is going to take 2 as num1 2 is num2 right so this we're going to have subtraction we're gonna have division and we're gonna have multiplication so um this is going to be yeah let's let's actually just leave the values as like two throughout now um let's go ahead and run this and see what we have sweet so um this is our output and um the addition is four the subtraction is four well definitely something is wrong oh yeah i i use the same function so let's correct that so this is going to be sub this is going to be div and this is going to be i'm sorry so i'm sure you saw that all right so this is more like it so uh the addition of 2 and 2 is 4. the subtraction is 0 the division is 1 the multiplication is 4. very good now let's say for whatever reason someone went to [Music] our app and then they changed the let's say for example they change the multiplication function and rather than having um um star they change that to a plus right let's just say for example now um if you look over here you see that the results for multiplying 2 and 2 is 4. the addition for multiplying 2 and 2 is 4 as well all right so now let's say for example this person was just trying to refactor they saw that uh maybe um using aesthetics is uh i don't know is for whatever reason right so they decided to change that and do that refactor so after like making that change then they ran the application again and um after doing that they see that okay the addition of two and two is four and the multiplication of two and two is also four now in their mind they say okay yeah i refactored and everything still works um smoothly but um that's not the case right this worked for this particular um example this worked for this particular value but if you were to change this to maybe like five and run it again then you're going to see that the function is actually broken because 2 times 5 here this should be 5. so 2 times 5 is not 7 right so um this is one of the most common things that testing or writing tests could help you avoid so basically the idea of writing tests is you write well tests or certain conditions that expect a particular output for a particular input all right so what you're going to be doing is you're going to write those set a code to basically test a particular function to make sure that um under certain different circumstances the function returns valid results okay so what we're gonna do now is we're going to go ahead and add testing to this particular project now since this is an existing project how do we do that what we're going to do is we're going to go over to just click on your target over here and right here we're going to click on this plus icon and then we're going to add unit test unit testing bundle so just click on that then i'm just going to use the default name so you need testing just add a test to it and then we can just go ahead and click finish this is going to create our test bundle all right so if you click on if you expand this you're going to see that it also created a file for testing as well okay so now what i want us to do is i want us to write tests for our arithmetic class over here all right so i'm just gonna um ignore this particular file for a bit and instead i'm gonna create a new test file for the arithmetic class so what i'm going to do is i'm going to search for unit test again and this is going to be a uni test case class so just click on next we're going to specify the name of the class i'm going to say arithmetic so arithmetic tests if i got the spelling so these are tests for the arithmetic class basically so now that i have that make sure it's a subclass of xc text test case and the language of course is swift let's go ahead and click on next and of course you're going to have to make sure that um the this bundle is selected because we want to create this file into our test bundle right so i'll just make sure this guy's checked click on create so this has um generated a class for us and i'm just going to go ahead and remove both of these functions because we're not going to need those for now and then um it's time for us to test our arithmetic class okay so uh the first thing i want to do here is i'm going to create a variable that i'm going to call sut system under test so basically this is going to hold the class or the object that we want to be testing okay so what i want to test is arithmetic right so i'm just going to write arithmetic i don't know why this is pretty hard for me to spell so anyhow i'm just going to write arithmetic over here and i'm going to force this because i'm sure that this is going to be available before it's going to be used all right but if you are more comfortable then you can just use the um question mark right so um there's an issue over here i'm writing arithmetic but it says it cannot find arithmetic in scope and actually that is true because we are in a different um bundle wearing it like a different target so what we're going to need to do is we're going to need to import our main app into our testing system okay so how do we do that basically just going to say at testable import and what do we want to import you need testing which is like our main target right so when we do this um arithmetic is a member it exists within our unit testing so this is now going to be available within the scope of route test all right hope that makes sense so now that we have this we're going to need to um initialize our system to test so um if you look over here it says put setup code here this method is called before the um indication of each test method and the class so basically this function is going to be called before a test case is executed so what we want to do here is we're going to say sut be equal to arithmetic so we're just going to create an instance of our arithmetic class and then assign it to the sut then if you look over here it says put tear down code here this method is called after indication of each test method in the class so basically we're done with a particular test case what we want to do we want to basically reset our values we want to reset whatever configurations we've made so um in our case we simply want to set our sut to be equal to new that's it so this is going to be our setup this is going to be our um sort of destroy or tear down right so now we've done this and this is pretty good so uh the next thing we need to do is we need to actually test the different methods that are within our arithmetic class so um the first one want to test let's just go ahead and test the addition method so what i'm going to do is i'm going to create a function and i'm going to call this test addition all right so this function is going to allow this function is going to allow us to test our addition function so uh basically what you want to write test it's going to be very helpful for you to think along this line so first thing you want to say is given certain input when a condition or an operation is performed then this particular output is expected all right so there's just three phases first one given and you could actually just write it down like this so all of your tests should follow this format so given when then then okay so given certain input when a particular operation is carried out then i want these output should be um you know derived so in our case we're trying to test addition so we can say given um num1 equals two for example num2 equals uh let's say five and um and actually we could actually write our test to be like it is very good for your test to cover so many cases so uh what we can do here is we can say um first let's let us test the addition of positive numbers so test um positive addition or test addition of positive numbers whichever one makes sense to you okay so now in this case we're gonna be doing like two plus five so when so what operation do we wanna carry out we simply wanna say and we're gonna store this in a constant called results so when our system under test dots and we're trying to test the add function right so num1 and num2 so um when this right then what do we want we want to compare the results with the addition of both of these right so uh while writing the test i know that the addition of five and two is seven so what i want to do is i'm gonna say xct assert and there are actually different um options to we have over here so we can insert equal that's when we're trying to compare uh left value right value like compare two values we can um assert uh nil so let's say we we expect that the value it should be nil so we can check that you can also check that it is true so let's say for example the result is a boolean we're checking if the boolean is true you know you can just we have different things that you just basically compare with your expected result so um yeah we're just going to say xc assert and we want to assert equal to so basically we want to make sure that result is equal to seven right because we know that the addition of five and two is seven okay so um let's just go ahead and for for you to run a test just click on this play icon over here good so you can see that it ran the test and it actually passed which is very good so um what i'm going to do next is i'm going to create a test case or a test case actually for adding or making sure that the addition of positive numbers sorry negative numbers this time it's correct so um where we can actually do pretty much the same thing over here and um so here i'm just going to change this to negative numbers negative so here i'm just gonna do minus five and two then um so uh two plus minus five is what can you guess minus three of course so this is going to be -3 so um yeah we can now go ahead and run this and actually rather than clicking on a specific like test case like this you could actually click on the class and this is going to run all the tests within the class so in case you want to run all of your tests at once you just click on this icon right at the top the same way you can click on this icon right here and you see all the tests that are within your application you can just go ahead and click either this to run all the tests within the test bundle or you click on this to run all the tests within this particular test file or you run a particular test case okay so you have many options thanks yeah that's good so um yeah this is good now apart from just testing that a result is correct you can actually test that the result is not something so um for this particular example it may not be very useful but just to show you that it is possible i'm going to say um test addition of negative numbers how do i say this okay let's say test wrong addition right so for the other ones we're testing like correct so we can say test wrong addition right so um now we have two and we have zero for example so wanna add two and zero so we want to make sure that the answer is not 20. so let me show you again you can do xct assert and this time rather than using equal we're going to say not equal so we want to make sure that result is not equal to 20 right so you see why i said um this particular test may not be simple because uh at least for uh this addition you can't be checking every single case so not equal to 20 that it could not be equal to 100 it could be not equal to you know minus one you know just just but just to show you that this is a possibility so um we're gonna run this test and can you tell me whether or not this is gonna pass or fail very quickly yeah it actually passed because two plus zero the result was actually two so at this point it's the result is two and the same two is not equal to twenty which is good so now i've been showing you test cases that um passed but um what does it look like when a test case fails so um now we know our test is good so let's say that someone goes over to let's go back to our arithmetic class over here and rather than having like the plus operation over here they added the minus and let's actually just change this back so don't forget so now um they did some refactor to our addition function and they ran it and it looked good in the ui everything looked as it was but they didn't know that they have broken something somewhere else so um they run the application they see there's no syntax errors they see that there's um a result in the in the um console and they just assume everything is cool but um when you run your tests just gonna click on this to run everything uh let's see what happens oh so test failed ah everything red over here so you can see that um things actually broke so um two tests failed and they failed because certain conditions were false and as you can see over here it says xcts are equal failed minus 13 is not equal to seven so let's see what happened okay so we have two and five and remember from the change that was implemented we changed it to from a from a plus to a minus so two minus five is going to be minus three which is what we have over here and we expect it to be seven because we know that five plus two is seven so minus three not equal to seven it failed okay the same thing over here we have um two minus minus 5. the minus minus is going to become a positive so um 2 is not going to be 2 plus 5 which is going to be 7. so um the result at this point is 7 as you can see over here and then what we expected is 2 minus 5 2 minus 5 is what minus 3 which is this so after the refactor things broke and that's why the test was able to basically just tell us on time that look something is wrong so these are the things that didn't match before so maybe go consider fixing it right so like writing test is extremely important and i've actually had some personal experiences where this saved me a lot and if you are interested in hearing uh my story then just let me know and i'm just gonna make a video on that so um yeah we can now go back to arithmetic and now we can just fix this guy up good so um when we've done that we're just gonna click on play and it's gonna try to run all the tests again and everything passed and we know we're good to push right so um you can actually even do the same thing for the multiplication where we did the plus all right so we could go ahead in our arithmetic class and then we're going to create um we'll just take this one so we're going to create a a function to test multiplication right and we can say multiplication of similar numbers this time so similar numbers so this is going to be two and two and we expect the result to be four right so this is going to be we're going to be calling the multiplication so the same system on the test the multiplication method so we know that when we we're trying to test that when you are multiplying um two similar numbers then two times two is equal to four right and um so this is good and if you look at our um change over here this is wrong we know that there's a problem here but at the same time we know that um let's go ahead and run this oh not run it let's run our test and after running our test everything should pass because this is good right and this actually explains why when you're when you're trying to write um tests for a function try and test um as many cases or edge cases as you can think of okay so this is just a single test for the multiplication function and if that happens to be a wrong one then it's going to pass when in fact it shouldn't so what you want to do is you want to test like different ways you want to test multiplying like negative numbers um so this one is going to be like different numbers right so we've tried multiplying like similar numbers multiply different numbers so this is going to be 5 times 2 and we expect this to be 10 right so if we go ahead and run this let me see so if we go ahead and run this particular test case this is going to fail because um you know 5 times 2 is 10 but it is saying 7 but if we were to run all the tests again let's go and run everything again you're going to see that multiply of this particular multiplication test passed it's still passed but this one failed so this also tells us that something is wrong so we can now go ahead and check our function and we say what multiplication why is the addition over here so you just do like this change this then you go back to your test case and you can now run your application and everything should pass smoothly all greens beautiful so yeah this is um a very quick and basic introduction to like unit testing and um if you guys are interested in like learning more in depth because unit testing goes way beyond this that you can you know test different classes you can do something called mocking where you basically try to um replicate a class and then um in don't worry let me know confuse you but anyhow um so if you are interested in learning more about uni testing then just of course let me know and i'm just going to make that as a separate playlist or a separate um course so that um i'm able to you know touch every common and important concepts about unit testing all right but in our next video we're going to do like sort of introductory just like this to um ui testing so um you know if you haven't subscribed go ahead smash the subscribe button all right and turn on post notifications so that um when i release my videos you are aware immediately and um if you also found value in this video then i'd really appreciate it if you hit the like button as well so again if you have questions just put it in the comment section and i'll definitely respond um so until then have a great day [Music]
Info
Channel: Emmanuel Okwara
Views: 587
Rating: undefined out of 5
Keywords:
Id: 5lPtpikEbCo
Channel Id: undefined
Length: 24min 48sec (1488 seconds)
Published: Sat Oct 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.