Unit Testing is the most important testing method, that is usually performed by the developer.
Unit Testing tests each individual modules (method, function or a class) for its proper functioning.
This indirectly means you have to maintain loose coupling in your application.
That is, each function or a method should be able to perform only one functionality.
This way it will be easier to test and even debug you codebase.
Letโs take an example.
Here, we have two classes and each of the classes have their own static method which performs addition and subtraction respectively.
Letโs say, these methods are used somewhere deep inside the application, ie, it might be in the last payment screen of your app.
You canโt possibly go to the last screen every time to check if these methods are returning proper data for various inputs right ?
Then, how will you check if these methods are returning the intended data ?
Unit Testing comes into picture.
Now comes the Dart code.
Head over to the project directory and create a folder ๐๐ฒ๐๐ and create a new file ๐ฐ๐ฎ๐น๐ฐ๐๐น๐ฎ๐๐ผ๐ฟ_๐๐ฒ๐๐.๐ฑ๐ฎ๐ฟ๐ .
We start off by importing the test package:
Then we write the following code:
Here, the first parameter is the title of the test case. Second parameter contains the code to be run for testing the result.
The first line is the init and execute stage, which initialises an object for the class that we are using and executes the method to return a value.
The second line is the Assert stage, where we check if the returned value is what we expected it to be!
If both the values are same, the test will be successful, else it will fail.
Letโs add the remaining code:
Now, the last two tests are somewhat similar right ? They are testing on the same method but on different results.
Why donโt we group those two tests ?
We do it using the code below:
The group method groups tests which belong to the same category, or which has to run together.
This way we donโt have to run the entire main function for testing these two methods, we can just run this group instead.
Now, for the important part. How will you run the tests ??
If you are using IntelliJ or Android Studio, you will have a run button against each of the tests/group or even against the main function.
If not, you can type the following command in your terminal to run the tests:
Or, if you wanna run only a particular group/test from the terminal, follow the command below:
Here, we need to pass the title of the test/group which we would like to run.
Well, thatโs all the basics you need to know to Unit test your applications.
Always keep it as a priority to test your units before going live. Itโs gonna make your life easier.
If you loved this thread, donโt forget to hit the like and retweet button, so that it can reach more wonderful developers like you! ๐
In my next thread, I will talk about the mockito package for creating mock dependencies while testing your code.
Donโt forget to hit the Follow button @GopinathanAswin to stay updated on latest threads where I talk about new things that I learn in the world of Dart and Flutter.
โข โข โข
Missing some Tweet in this thread? You can try to
force a refresh
Every time your UI refreshes (eg: using a setState), the future method is run again, which can turn out to be very expensive!
Sometimes this could be helpful, but most of the times itโs a curse for the application.
So how can you fix it ?
๐๐ป
We need to set the future in such a way that its run only once, and will never run again until you re-open the screen or you explicitly define a Swipe-down-to-refresh feature.
So, what is that one thing in Flutter, which will run only once in the entire life-cycle of your screen