Reputation: 307
I am required to create unit tests in Flutter and the problem is that I don't really get what they are for, how they make an app better or safer? Also I would be happy if you gave me the source where I can learn them
Upvotes: 1
Views: 213
Reputation: 339
this answer might be not 100% accurate because i tried to make it simple as possible to make you get started tests will not only help you make your application stable it will force you to write the code in a better way
for example :-
you cant tests this method since it depend on another class instance
and it creates it within the method this is call coupling
and it will prevent you to make some test cases for this so you have to go back an decouple these dependencies to be able to test it which will make the code safer and cleaner
also if you add a feature or fix a bug you might change the code behavior for instead of throwing some error you return a false or you calcurlate some thing un expected way or any thing even smallest changes code change the behavior which will lead to breaking some thing in the end
if you working on a tested project , simple use the command flutter test
to make sure every thing is working as expected instead of going on production and find out you have bugs over a silly mistakes
another way to imagine it like if you like the IDE to find your syntax errors while writing the code or waits until you push your code and the reviewer sends you back because you forgot a ; 😄
testing be in different levels
the simplest one which you can do on your on in the local machine without needing any other services
is Unit tests
so the purpose of it to split the code to small units and test each unit by it self
also there is
Widget test can be done on local machine also with flutter_test
package
you can find more on tests in flutter here :
Testing Flutter Apps - Making Sure Your Code Works (The Boring Flutter Development Show, Ep. 21)
Flutter Testing For Beginners - The Ultimate Guide
Upvotes: 2