Unit Testing - For Beginners in Dart - Setup with Flutter Project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my friends and welcome back to another episode of flutter explained today we want to continue our journey of testing and if you remember the video where i introduced you to the testing pyramid we want to take now a look into the basement of this pyramid which is called unit testing this video is following structured first we want to talk about what is unit testing secondly we want to understand what makes a good unit test and third we want to jump right into code and learn how we write our first unit test and now let's begin with what is a unit test what is unit testing essentially a unit test initialize a small port of your application or system and tests its behavior independent from other parts of the application typical unit tests contains out of three parts first initialization of pieces of the application arrange execute something on your application act and third we're talking about assert so observe the expected result these three phases are also known as the three a's and called arrange act and assert i want to give you five points that you should keep in mind if we are talking about unit testing first a good unit test should be easy writable and maintainable we as developers work mostly on code bases that has lots and lots of unit tests like around 1 500 i also already know from my project which is considered relatively small therefore it does not surprise that these unit tests or good utility tests has to be very easy to write and also easy to maintain a good unit test should be readable code in a project should follow along of the metaphor of a book it has like title subtitles chapters sub chapters and so on and so forth a good code base needs to be very easily readable so if you jump right into a unit test you want directly to understand which part of the application is affected tested and how you can implement and make it better so this will help you along the way to easily detect fix and debug bugs in your code a good unit test should be reliable think about a very big project where you have to run all the tests and all the tests and suddenly some of the tests are just failing without any particular reason the problem is because of single tests that are failing you start to detrust all other tests that means if the developer loses trust in the testing framework they start to skip testing or don't write tests anymore for example we run a single test which is always working but if we run it in a random order or even are dependent on other tests then the test could fail without any particular reason so be careful about these things because if the developers losing the trust into the tests we get some very big problems and keep in mind a good unit test and we will come to that later should be independent from external factors such for example execution order so if one test runs before another it shouldn't affect the test itself a good unit test should be fast we write a lot of tests inside of our project and if we would now wait every time like 5 minutes or 10 minutes or 20 minutes for our tests even though it is 5 1 500 tests we would try to be more productive by skipping testing so we would maybe not run our testing case cases correctly and we wouldn't understand the problems that happens a slow unit test indicates usually that we have some connections to external systems or making any environmental dependent things and a good unit test should be independent a unit test is not an integration test and that is very important to know therefore we have to make sure that our unit tests don't depend on databases file systems external factors and all of these because we want to get rid of all these dependencies if you keep these things in mind you will write easily much better codes i got yesterday also a nice little tweet that says me that you should keep your unit tests as your friend fast reliable independent easy of maintenance nearly compacted code and dependency should be less so if this could help you to memorize how you should keep your unit tests so now that we have covered the theory behind unit tests let us begin to write some code i will demonstrate it typically for this channel in flutter and dart but all of these parts and patterns can be also applied to other object-oriented languages like for example c-sharp or java as always you will find down in the video description below a link to the repository where you can find a list of to-do's that you can follow along this tutorial and because you are now such amazing people you want to give that video here a like and subscribe to our channel already and now let's jump into code all right welcome to my code base and let me explain what we want to do today as you can see on the right side depending on the time where we come into the app we will see a very big screenshot of an image we have four different images that can be visible inside of our screen depending on the time that would be afternoon evening morning or night so depending on the time the app will show us one of these four things also it will tell us in the top part good afternoon good evening good night and good morning depending on the time so now let's jump into our code for that we have a screen created my home screen for my home page and in this home page we ask for an asset image depending on a class called time helper which get us time of day and also the good part where we get from the text we get from this time helper if we jump now into the code we can see the following we have in time helper aesthetic method which returns us a string also what you can see is we get the current date time depending on that we return night morning afternoon or evening and now our goal is that we want to test this for that i created as always a lot of to do's and if you remember in intellij you press command shift and t for mac users and you jump right away into the test you will find down in the video description for visual studio code users an extension that will help you to make it also possible to jump between your tests and your normal files as you can see i created already the file for us and we have this test folder in every flutter project right util which is the same folder structure like here and then we have the time helper test dot dart make sure that the file name is exactly the same like the real class that we want to test but add an underscore test to it to make sure that this is recognized as a test so this is our first test that we want to write for that we will have to implement a main function and inside of this main function we can now write our very first test so let's write a test which comes from the dependency flutter test.dart and inside of this test we first have to pass down a description what we want to test so let's for example use one should be one and then we have to write a callback inside and this callback will be our test body so here inside we can test our stuff for example we learned that we want to use the free a's arrange assert so these are our goals that we want to achieve and now with a range we can say that we have an expected number which should be one another thing is now we want to act but in our case we don't do that so we keep that empty but we want to assert we expect that expected number is the same like one all right so if everything works well this test should be green now we have multiple possibilities to start up this test but what i want to do is going to the terminal and execute flutter test and easier is that our test is automatically running and as you can see all tests has been passed if we now change something in the test for example the expected number should be 2 we will see that if we run that again the test will fail but the good thing about this result is that we get here also the expected and actual so the expected value was 2 we wanted that this test gets a 2 here inside but actually we received a 1 so that means whatever is written on the left side is what we received and this part is what we expected so then we know that now we want to write some real tests here so for that i will create a test with the description of time helper should return night for example as the first test that we write so when we have a certain date time we want that it returns night for that we will call time helper we will import the time helper and then we ask for get time of the day and now what we can do is we expect whatever comes from here and should be night so some of you have maybe already seen a problem here but let's run this time helper what i did here is i run it inside of intellij you have the same possibility in vs code and we have there are some integration i will add a link down in the video description which explains that a little bit more further but what you can see is that the expected night was actually afternoon how can we test now this method beautifully if we jump inside we can see that our dependency to the date time object of now lives inside of the function which makes it impossible to unit test it so what we have to do is we have to pass down a date time object inside of the function and remove this part but as you can see now our clients will complain about that because they will still need the date time we could even improve our code further by inversion of control i will add a link down in the video description but we will tackle that in a different video for now for us it's important that we are now able to pass down here a value so what we want to do is again this is our assert right we want to act and we want to arrange now let's arrange our test we need a date time which has a certain hour inside to make it possible to send us the night for that we jump into get time of the date and we see that we receive night whenever we are between zero and six o'clock so we have to set up a date time with 2020 because if you remember we have to add these parts we just take the first month of the year the first day and now the hour should be something between zero and six so i will use five that's already enough to make this test work now we say we have here test date time which is our current time and this current time has to be passed inside of this function but because we want to keep the arrange act and the third part we act here in that place and this one gets a result of string time of day and now we expect that time of day is night if we did now everything correctly our tests should be run with a green tick so let's try that we run our test and you see we get a tar pass test we can also execute again in the terminal flutter test to see if the tests are running fine yes all tests are running so what we could do now is we can copy that whole part here where we create the current time and copy it here and just change the hours if we want to find the afternoon why would we create such a test you maybe ask but if we jump into time helper i could just easily change here for example the string to 90 that could be still right but it would break our code somehow because if we are for example here in the we are currently in the afternoony and if i'm checking the app you can see we have an embarrassing error here and additionally the image is not visible anymore so we can avoid these problems by running our unit tests so we have the afternoon and the afternoon is at the moment it is 15 and if we run these tests everything should be fine great so i will quickly add all the other tests here okay now if i add all the tests and run them you can see we get a lot of green ticks and we see that night afternoon morning and evening and the good thing is if i'm now making my spelling mistake here and let the tests running we will see that we get a broken test so one test is failed with the expected afternoon but the actual was after nui and that is of course not correct now that we know that all of these to do's are gone already besides of one as you can see we right now everywhere time helper in front of it so we can make that a little bit more easier and convenient for us so we can group tests so for example that we say we have a group of time helper and execute our command here and now we wrap all the tests inside of this group and this group don't do anything much but it gives us the possibility to group semantically different parts together so we can remove this time helper here in that case also here here and here and if we run now our tests with the help of intellij you will see the following here we have now the time helper as a parent and then the different parts of it and to make it more explicit it is not only the time helper that we test here actually it's the function get time of the day because it could be that we have later or some more so we could create even a second group responsible for the get time of the day task so if i'm running now again flutter tests i think everything should be fine and with that we created our very own first unit test perfect guys with that we created our first unit tests now you should be able to understand what a unit test is what it needs to write a good unit test and last but not least you can write a unit test in dart now it is your turn and you want to make your application fast reliable and also increase the software quality i hope you learned something today and now it is the time to hit the like button subscribe to our channel if you haven't yet thanks for watching until the next time
Info
Channel: Flutter Explained
Views: 10,522
Rating: undefined out of 5
Keywords: flutter tutorial, flutter unit test, unit test, unit testing, flutter testing, flutter tutorials, flutter tips, flutter unit testing, mobile app development, flutter sdk, flutter test, dart unit test, flutter tutorial 2020, flutter tutorial android studio, flutter tutorial beginner, flutter tutorial playlist, flutter unit testing tutorial, assets to invest in, dart unit testing, dart and flutter, Testing, Test integration, flutter tdd
Id: C1kzJH8SiuE
Channel Id: undefined
Length: 15min 2sec (902 seconds)
Published: Tue Sep 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.