Test Driven Development (TDD) in iOS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
is tdd what is a death driven development in ios okay so quickly we'll see it's not a big enough like tutorial but yes but after this completion of this session like you will be able to implement your uh testing part into your existing project or in the fresh projects right so this is my target so one by one like we'll execute more things and we'll go dig deeper as well so this tdd series will uh evergreen longer so we'll use this in the future sessions as well so we are starting quickly starting with the tdd what tdd is and how we can just implement into the ios right so how many types of testing in the ios like there are three types unit test ui test and performance test unit test is simply same like functional testing the logical things whatever things like dig deep testing will be there which which will be executed by the unit testing ui testing is simply like you can just automate the system like whatever i'm just i'm running a application it will run automatically and we'll check outer area and outer edge cases and everything over there with this black box equivalent to the black box testing right now performance testing is saying like there are time execution is happening into some functions how you can just check and identify time uh complexity and everything like over there into the function into the loops or somewhere like big complex functions so for this like we require performance test testing as well and how we will execute i'll tell you now how we will achieve testing unit ui performance in ios yeah how to see code coverage of testing after writing test tests into the ios like code coverage will be there like how many tests you've written how much code coverage area they are just covering and how much better quality project you have and how much area of your project is completely tested so that also will consider apple define it beautifully so let let's get start with me and we can just execute one project so you can understand like i will tell you ddd example and in this tdd example what i will do i'll create a simple export project that project will be the tdd example project i don't want to waste your time and quickly as much as you can just understand properly so that's my target into the tdd example i'll go there yeah so there i checked uh if again if i will go there and quickly check this like once i created this project on that time i was not able to show you like yeah tdd example will be there and include the test before clicking hit button like include test or not like yeah if i'm including this they will automatically create over there so in a fresh project it's perfectly fine but what will happen into my you know the existing project if i did not created test cases so what i will do here two boxes are created ex test and tdd ui test right ex that means unit testing one file will be there which will import your tdd ex like that but if an xc test is the thing it's a mother who is just doing two things for the if you will go to the ui here you will see xc test xc test case with the ui performance also and with the exit xc ui and xc simple test things will be there for the unit test so xc test is the mother and by this testing is able to produce into the ios and unit test will be this one and ui test will be this one with two separate classes functions right now if i want to delete all the things and more than that two targets also created here so i want to delete them because if i will execute into my existing project then how i will do that i'm also sharing with you okay so you need to delete all the things so that like you can just go and just add those perfectly into your project now things are perfectly working there i think i i compiled it and it's running perfect perfectly right now i'll go to the file new and target remember target you need to select the target once you will select the target you can just go to the u unit test bundle right that is the unit test under target tdd example is fine you can change whatever name you want to give but yeah for being now it's fine again it created like this and it created this target same like that you will repeat the process and you will go and add the target through the target you can just search ui space testing so that testing will be there so ui test bundle you can create any name but yeah this will be good like if you will create with the project name two targets are there two files are there and how you can just now you remember like one at the rate testable uh feature was there this is the keyboard keyword like any project you are just importing here that should be testable so i'm just doing dd ex my project as a testable project right now if remember if you are adding anything into this project now suppose i created a uh file here the user right the user user file i'm creating here and remember don't forget to check them like this so those will be the part of your test cases and ui test cases otherwise it will give you error right so the user is completely there there now you can just create a user suppose i created a user with a variable first name first name string and then variable last name string and then you can just create an initializer and parameterize initializer like first name uh first name string and it can be nil and last name optional parameterized function i'm creating so i'm giving value as a nil okay so it will be in nil value and everything is optional and self dot first name equals to first name and self dot last name equals to last name right so those things i created properly right now what you need to do you can create a condition check here because i am just going to create a unit test feature on this class right so how you can just assume like i'm just going to use this first name user i'm just going to introduce this user on my home page and that you can you view controller you can assume like it's my home page right before coming here if you are just writing and you are focused on tdd approach then go to the test driven programming approach right so think like think from here not from there right so once you will think from here your uh like false cases will come in your mind first right so suppose if what will happen if my first name will not okay last name will not okay if both will not there then how you will just handle them right so you can just uh quickly you can just create a in a nil condition in right you can create a nail condition in a new condition condition in a right condition of nil conditions right now conditions it's fine so f name is nil right and you can just say f name is nil right and same like so on l name is name right and it can be both l name and f name s name right arm and same same like that everything is filled everything is filled right so like that you created a nail condition thing right it will very helpful for the testing and for the real-time programming as well right so it will kill two birds you can just execute a function here within this user like fun and check check nil conditions right of properties and nothing you don't need to give anything and it will return nothing right what it will do it will create where condition in my condition what is the condition of user in the property of first name and last name right so it will be a nil condition of properties and condition you can say nail conditions and nail conditions what's happening into the nail conditions you can just use this nail condition like that and then conditions and you can make this as a nil because right now first name and last name is nil right so you can just say everything is name or you can say everything is name right everything is nil that's all so everything is nil it's fine perfectly fine now you can just create a function here if which will give you the calculated property observation like this will be nil and l name uh l name is not equals to n then it will give me uh this result like what result like uh condition will be my conditions nil conditions will be dot my l name first name is name right act name is name so on like you can just create quickly so that we can just test it into my unit testing right or you can use the else right as so three things will be there if first name will be not empty and the last name will be empty then l name is nil and if everything will be empty then you need to say everything is empty everything is nil and you can see everything is filled and here you can see like that right so uh in this case like no need to give anything you can just use else and you can give this condition here like that right that's all so now quickly we created this check condition nil condition function and if something is going good there then we can just give a beautiful greeting message so greeting message will depend on you know uh on the conditions what we are just filling there so you can just say like if greeting message is nil right now right so if i will do the greeting message as well here greeting message string and this is nil right now right it's also nil and nothing is there for the greeting message right so now if greeting message is okay or you can just you you can avoid this greeting message from here itself like you can just modify this greeting message in whatever class it is there you can use that getting message only there right so this is perfectly fine something will be there that conditions will come into my test driven example now come here and understand this like what is the xc test case there is a star option and there is two functions also test performance example measure i'll delete this and this also i'll delete this also right two functions are there set up with error and test tier down with the error two are it's like this testing machine will start with this function and end with this function so i have four conditions right so into the four conditions i can write four test cases over there right proper test cases so one will be fun remember if you will not write test in front of your function it will not give you the star same like that right so you it's compulsory and mandatory you need to write the test don't forget forget it so test fname snl right that you can just test star is there right if you will remove the test what will happen star will went out now you can just copy this paste it here paste it here paste it here now test fname test n name right and test f name and test everything is nil right and test everything is filled that's all so if you will do these things it will work perfectly right i don't want to print anything i will make the breakpoints and i'll show you show you the life cycle like how those functions will execute and how you need to write a user class here right so you need you know very well this is a user class you need to check cross check like yeah it's added into the ttd example test it otherwise it will not come there now you can just create a variable my user and i want to test this user right with its values right so what i will do i'll create this user object here once my machine will start with this function setup with error then i'll create this fresh user here now it's my duty to nail this user at the end once my test will execute right so i have one two three four function so one time like test it will execute first function before that it will set up the object then it will execute this one and then it will go to uh the tier down again it will come so everything will be nil over there so it's very important like make nil things over there otherwise it will flash to other properties as well right so don't create simply like that and you can just use that thing like this no you need to understand those things and you can just make every time fresh object for each test cases now once i hit everything you can hit everything together or also one by one if you will play this only one diamond will execute only one test case will execute okay so only one test case unit test case is executing so first set up with error is calling then i'm just calling my test function whatever function i'll write here i'll do my stuff here and then it will finally call tl down on that time i need to delete the object like that and it will tell me yeah it succeeded properly same if you want to do all the things together then there are two features like you can run from here the whole thing and one by one every method will call and again and again things will call and so on and things will call perfectly at the end right so uh everything is calling from here itself from here itself also one case is still pending once those will hit everything will be hit perfectly it's taking time so i'm breaking it now you can run from run from here as well like if you will execute all the cases will call from here itself right so one case execute so start and end will be there second case will execute start and end will be there third case will execute start and end will be there this is the life cycle okay now i want to tell you one thing here what i will do i'll use a xc i'll what i will do and use a user dot chat nil condition before that i'll give user dot i know very well like i need to fill the last name equals to uh coder right and it will taxi assert and i'm assuming like my xc asset should be true like my my expression will be like true because if i'll get the condition if user dot condition if i will get nil condition will be equal to equal to f name is nil right what was the condition i need to check here yeah so that is the condition if i will go there into check f name is name the name of these conditions and go there and print f name equals to net right then only i can go and just make it xc i said true and if something is going wrong then what i can just do like yeah something is going wrong then what i can just say yeah it's failing right so a boolean variable like if xc asset is true that will come from its status can be a false status first but yeah if condition will match and things will be okay then it can be true and then this is status otherwise like i'll print a name f name is not nil and case is failing right somewhere so whatever you are writing these things will effect over there right so same like that you can just create these things for other things right this will use first name and this will be like that and this will be the l name l name is nil right and name is name and it will work for this like name is name not net and case is feeling so if everything is okay then you need to give both condition fill and then like the coder will be there and check everything and if everything everything is filled what was the condition like everything is filled then things will be perfect and yeah something is unconditional is failing right so like that you can write test cases and things will be there right and it will be and this these things are by default already there so it will give you nil result only right so again i forgot this one everything is there i'll go there and use this everything is in it right so everything is net but something is not like that you are just making stories in your mind and just writing some code and if you will hit those things my all test cases should perform very well otherwise i'm wrong somewhere right test cases is succeeded properly now what i will do i'll do like in my test l name is nil and if i will fill some last name over there like as a coder then what will happen something will break there something will break over there and first if you will see and check what is breaking so you will see something is breaking here i need to check yeah this one is breaking and what is the reason it is saying l name is not nil and case is failing right and you are assuming that and name is right like that you can capture your all things and things are perfectly working now i want to tell you like in my presentation also i gave you like how you will see the test cases code coverage right so in the code coverage area like how you will identify like whole code is uh checking there or not by us right we are not able to checking or we are able to check right so we will do will go there in the tdd only if you will go there and check there are features those are already defined right and if i want to see them properly right so from where i can just see them the code coverage area right so what you need to do you can just go to this documentary and inside the documentary if you will hatch at your here like show the report navigator report inside the report you will see build and log build and log is coming right building and log is coming right so test cases build log is coming but no coverage is showing here right why i need to tell you if you will go to the edit scheme right and you will go to the edit scheme of this tdd you will see the test cases right while running or while building so if you will go to the run thing and you see uh of this test performance cases uh information is there diagnosis is there and if you will see when code coverage option will come into the build nobel test into the debug mode uh information option gather coverage for right all targets or some target no i want all targets that you need to select right into the options you will go to the code coverage option and you will check this for the all targets now again i'll go there and see something uh i'd done this right now i'll go to my execution again and i'll correct my things over there like it can be the first name only uh it can be the last name only whatever yeah last name is not nil right and i want first name as a ios yeah so if i will execute this again what will happen i need to check right so once everything is succeeded properly now i will go there and check again my documentary here coverage is showing right before it was not showing why because we edited the scheme we went there and check it out like code coverage area for the all targets that's why it's coming here now you will go there and you will see my application is there and all conditions are available in front of you so in appdelegate it's percentage is 28 percent in the user it's 100 right i consumed everything right from that particular class that's why i just done uh whole things there right so if you will come there and check it out uh this is my class and it's covering everything here right so if you will go it will give you the green symbol right it's giving the green symbol that means these things are perfectly working fine right perfectly well working fine and the coverage area also you are able to see right so like that you can see like i have tested those those many things suppose i will do uh one more thing like i will do again the bad thing into my first name like my first name is uh something there right then it will fail then what will happen i need to check right so once i'll go and just check the latest one it is giving the error execution and once i'll go there and go into my class and i'll look into this something is there maybe something is breaking then red symbol are coming can you see this is failed why because it was assuming like first name should be should not should be there nil but it is coming filled so that's why it's crashing and uh the last name is coming like that so and also the uh yeah yeah i'm sorry this these cases are still not covered and these lines we are not able to cover right now this is covered and only this condition is covered first name and last name right because we are checking the first name and last name the last name property and so whatever things are covering those things will be green and rest things will be red right so again i'll do this thing and everything will be clean right so like that you can just see the code coverage into your uh project right whatever things are there those percentages will come there now the lastly i want to tell you like what is the performance the measurement right so suppose i have in this user only i'll create one more function like fun fun and i'll create some names over there right where names and it will be a string array of names and i'll create that array as well like empty array also i'll create here and i'll make it lazy so till that i if i will not use it will not generate into the heap now names if i want to fill the names fill names fill names and count you can just make set with the integer it can be in 64. uh ui and unsigned or signed into 0s no like end 64 will be there or in 32 will be there according to the os environment now you can just create a for loop and just make some index there and just you can make a close range over there uh with this it will start from zero and it will load till your three objects close range will be there and count whatever count you will get from the user it will come and it will append the uh list right the names list dot append uh my name right so like name one name two name three so on right so index will be like that so this is the dependent on the performance right how loops will increase like that it will increase so i gave you example so i'll do one test function test performance of offending names right and you can just do this very simply like you can just call a major block in the measurement you can just use user dot fill names and count can be right now 100 right and once you will hit this only this one it will give you something here that will be magical right it's saying no baseline is set it up right now so don't worry about this i'll tell you like time is zero zero second no baseline you set it up so it will give you performance result matrix is a time result is no baseline average is 0.4 second it taken before 100 right and print it's printing also and baseline if you want to set suppose average rate is taking 0.00492 second if you will set something there like if i want to set rather than this like it should not go more than three seconds right so if i will edit this and i will do like baseline is my three seconds right more than three second result will be there then it will convert into error right so i'll make slowly and gradually i'll increase one zero over there and print also so that you can just see the names condition as well right after after this for loop right so now if in the hundredth thing like it will work perfectly because baseline is i completely added over there and time is 0.3 second now no baseline is not coming but yeah 100 better right now if i will add one more and will see it is coming like more than 0.13 it will come it's saying like in thousand the time consumption is taking more than three seconds right saying baseline 10 yeah it's covering like uh if result will be the 95 better then it's fine fine and i just said set it up like three seconds right so it's saying like you're it's very dangerous like it's going uh like more than ten percent right more than 10 percent it says 0.14 right so more than 10 percent it is and it's giving you time standard deviation is 104 max allowed only 10 right that's why it's giving you the error so like that you can just make like if i will do this one will be 50 and then i'll execute then what will happen we'll see so if i will succeed this it's saying yeah still it is uh 74 right so it's uh giving me the max allowed per performance is 50 so time standard deviation is 74 right so it's a 74 percent so if i will give like uh 85 max standard deviation then what will happen it will also give me yeah now this time it's perfectly working because i said like if it is 85 more than that then it should work perfectly right so like that you can just adjust your things and things will work over there right so hope you understood these things and if i will increase this 10 second then what will happen and this will be i will make as a 10 percent then what will happen i will see again 10 percent is there and i'll run this it will succeed i think yeah it's succeeding because i just made 10 percent right and i can increase the uh performance by plus 0 right so now it's 10 000 rather than 1000 and it's performing over there and still it's taking some time and time complexity is increasing right so once it will print everything it will come here and result will show here like that you can just execute your test cases for the performance right so still it's going on and it's printing and at the end it will show the result so this is a performance test and uh you just understood like uh you just understood like uh yeah it succeeded but i don't want because it's taking so much time if you will see and result will be there like it took so much time and it's very hard to you know again and again go there so everything will be there so performance you can just test like this you can test your unit test like this now the ui test is remaining right so how you can just go and just check the ui test so for the ui test suppose like i have a view controller i'll create another view controller quickly i'll go and just create another view controller here and that will be my another view controller i'll go and just click on this view controller yeah there are two view controllers and i want to you know just make them parallel so i will make them in parallel and then i'll create a quickly uh another view controller as well like view controller 2 so i'll take a quick touch class and vc2 will be there vc2 will be there my view controller 2 will be there and this vc2 will create here and i'll attach this vc2 to my controller which i created from here so if i will just attach this into my view controller like that and storyboard id will be ec2 and i will place a one more text field on this view like ui text field will be there and if i will use this text field here for better understanding like i'll give a background color so that you can just see this quickly and go and just give a background color of this particular text field and in this text field like whatever will be there the text color it will look like that so i want to add this on my assistant of vc2 so vc2 will be my assistant of this text field and i'll create a outlet like text field and also one thing you need to take care like once you will you want to create the ui testing there is a identifier for each user interface object so there there is identifier like if you will go and just check of this text field one identifier will be there so and go there and check where is my identifier can you see into [Music] uh this identities inspector you need to give a identity of this text field like my text field right like that right and this will be the identity which will use when i will do it do the ui testing right so that is perfectly fine now into the view controller one i can go and just create a button so i'll create a button and this button will be here and it will be a click me and in this click me i can just make some center and horizontal like that it will be at the center now i want to open its assistant assistant class i open this and i'm just adding this here as a action like what will be the action of that button like click me action right and once i'll use this i'll take a let vc2 from here programmatically and what i will do ui storyboard dot init with name name is my main because if you will see it's a main storyboard name main and then inside that storyboard i want a view controller with my identifier right and my identifier will be the vc2 and vc2 will be there as a as a vc2 right so nothing to worry and self.present will be my vc2 object and after then like you can just make it true and quickly you can just run this manually right so once you will run this you will see one application will come like that you will click this one uh one more it's looking ugly so we can just go there and we can just do our work perfectly so it will be into the center x and y it's perfectly fine and also we need to give some width so width will give some like 200 kind of thing and it will be good now and also height you can just give so height can also not be shrink yeah it's fine so now if i will read on this it will give me a click button once i'll click on this it will open a text field i can enter my name right firstly right first right like that so if you want that flow properly through the ui test and you want to see like i want to reach on the next while tapping on the click me i need to open the second page and there i need to type something how you will do this flow with the ui test with the recorded ui test what you need to do here you can just create a where app object ui xc test application or something yeah you xe ui application object is there seem like that setup is there this is saying like if continuous after failing like suppose any click or somewhere you failed something then it should be continuously give the result or not right so when failure occur in the test like it is saying in ui test it is usually best to stop immediately when failure occurs right so you can make it true as well and if i need to initiate my xc ui application object right then what i will do app i'll create my own function i'll remove everything right and remove everything right teardown is fine and my function is fine that's all i don't want anything now test my first flow right and we'll automate it and then i can just do app dot launch because it's already created here so if it is created here then here it will be nil again after testing right that's your duty now app dot button inside uh my first view controller i did not give i forgot to give the button identifier which is my bad mistake right so i'll do like button or my button will be there right my button that i need to give you can give from here as well and also if you want to give the programmatically then also you can just give your like like that like if you will create a button programmatically then you can just do this button dot identifier uh accessibility id depend and you can give your same here as well so no need to worry about storyboard and programmatically things we can just do it at all aspect now buttons now i got a button which button my button and i want to tap it right there are lots of things tab swipe and all you can explore them but for this time like this video will be very long so i'm just using this tab only in future we will do lots of things so this button i created like i tapped on this now what i need to do i need to see text field on the next view right so once add dot text fields will be there and what will be my text field will be i need to see on storyboard like what i gave the identifier of that my text b that is my text field right so whatever text field is there that i will give the name here and type something into this and i'll we done this right so ui testing is performed right now like that you can just make some flow and you can run them right so i am executing this test what will happen it will generate one more test ex ui test right a common thing parallel common thing created started and it went there and it is saying hey listen in my it's failed right it's fade it's failed why because it's failed because i did not give the text field object is created and inside this what i done like failed system even neither neither nor or descending has keyboard focus even dispatch over there into the my text field right so that means i forgot to go and focus over there into the text field first so i need to tap right and then i can just enter then i will rerun this again what will happen i'll see it clicked it entered hey we done this and stopped so recording is stopped and build is succeeded right so overall like we understood in this chapter like how you can just go with the test driven development and what are the performance ui unit tests how you can just see the code coverage into the into your existing project and everything we covered here so you can just nicely implement the test driven development approach in your projects if you really like this video kindly share it with your friends and family members so that they can just explore it and we can just learn together right thank you so much for watching this thanks a lot
Info
Channel: iOSCoderAbhimanyuDaspan
Views: 2,345
Rating: undefined out of 5
Keywords: #testing, #ioscoderabhimanyudaspan, #swiftlang, #xcode, #unittest, #uitest, #ios, #iosdev
Id: P98DO2TO6VU
Channel Id: undefined
Length: 44min 22sec (2662 seconds)
Published: Mon Mar 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.