Unit Testing in Flutter 101

A mega thread ๐Ÿงต

#flutter #flutterdev
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
ใ€€

Keep Current with Aswin Gopinathan ๐Ÿ’™๐Ÿ‡ฎ๐Ÿ‡ณ

Aswin Gopinathan ๐Ÿ’™๐Ÿ‡ฎ๐Ÿ‡ณ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @GopinathanAswin

13 Dec
In my latest tweet, I talked about how you can prevent multiple calls to your future methods using a variable approach.

What if I say, there is one more efficient approach to it!

Thanks to @DmytroGladkyi for telling me about it

Read on to know about it ๐Ÿ‘‡๐Ÿป

#flutterdev #flutter
The approach is using the AsyncMemoizer class.

To use the class, import the following package:
Now, letโ€™s create an instance of the AsyncMemoizer class, and initialise it inside the initState method (or you can initialise during declaration).
Read 7 tweets
13 Dec
When working with Futures and FutureBuilders, it is recommended not to execute the Future methods inside the FutureBuilder.

I learned it the hard way!

Read on to know why ๐Ÿ‘‡๐Ÿป

#flutter #flutterdev
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
Read 10 tweets
12 Dec
Future v/s Streams

A restaurant analogy thread ๐Ÿงต๐Ÿ‘‡๐Ÿป

#flutter #flutterdev
Imagine you go to a restaurant and your date gives you two options:

1. Takeout and eat at your home

2. Find a table and eat there itself

Well these options in the Flutter world means:

1. Future

2. Stream
Okay! Lets see the first option: Takeout

You ordered:

1. Burgers ๐Ÿ”
2. Fries ๐ŸŸ
3. Pizza ๐Ÿ•

The waiter gave you a token and asked you to wait.

<Few moments later>

Your takeaway is ready and you both leave the place.
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(