The importance of unit test your Flutter code.
Hello dev, those days I catch myself thinking about have a peaceful weekend after a long code work week, and the only way to archive that in my opinion is ensure that your code works right, but how to ensure such thing? Well on of the things we can do is test our code, but I’m not talking about run the app and click here or there to se if works, I’m talking about write code to test your code. Confusing? Let’s talk more about that.
If you have heard about Unit Test you know what I’m talking about, but if not let me tell you in a simple way… basically you are going to write a few code test scenarios to test the code you have written before or after if you use TDD. So, with this you can cover the majority of the scenarios in order to ensure the quality of your code.
The problem is that many times “you don’t have time to do this”, for a couple of reasons sort of… many things to implement in a short amount of time, people saying that is time lost and so on. But believe me, fix your code or anybody else code latter can be a headache and a time lost. For this reason I decided to show how to write unit test in Flutter, and it can be done in other languages or frameworks if you desire because the principles are the same.
In this example I’ll test the Flutter app boilerplate code, I mean… the counter app, so let’s create the project and focus on how to write the tests, and once you had created the project you might see something like this image below.
Ok, I removed all the comments to make things clear and now let’s remove all the increment logic to a separate file so we are going to be able to test exactly we need. Inside your lib folder create a file called counter_value_notifier.dart and put this code inside like the image below.
Now on your main.dart file modify your _MyHomePageState class like the image below, here we are just using the class we create above to update our widget.
Let’s move to the test folder and for now you can delete the widget_test.dart because this file is responsible for widget testing purpose and let’s create a file called counter_test.dart like the image below in order to test the increment() function, and add the code as the image below.
Hm.. let’s talk about this test code for a moment, to execute any Dart code you already know that you must have a main() function right? Ok, inside this main function we are going to write all the test we need, and we basically have two ways to do that, one is write a single test and for that you can just copy what is inside the group and put inside the main() directly.
But I decided to create a group os tests and that’s why I create this structure, and besides that I can better describe my tests purpose. Moving forward, we have this first test function with the objective to test if the counter will start with the value equals to 0. And to ask if it’s true or not we have this reserved keyword expect which needs two values to compare and tell if the test is matching with the expected result.
To execute this test you just have to click on the Run option above the main() function or you can open your terminal and run this command “flutter test test/counter_test.dart” to run this specific teste file or simply run “flutter test”, and you will have something like the image below.
Cool, you have written your first test code, but now let’s test our increment() function, so to do that create the test like the image below.
Awesome, now we have accomplished our go, because our test does what we described right? But if it doesn’t, what the test result would look like? Let’s try this and take a look on the image below.
See, as the image shows us our test is expecting to have 2 as the value but instead our tested object is with the value as 1, pretty well described right? We can see that we have one successful test and one failed.
So, to conclude this article I wanna make you think how good it would be “if we had time” to test our code, how wonderful our weekend would be? Because to be honest we have this time, but some people doesn't expect to have an ease feature like this increment counter done in more than a minute, just because they say that is too much time to do such thing… but, how reliable this feature is if we do not ensure the quality of our code?