Testing your Flutter app? For smooth sailing, here are some tips!

Ensure that your app is reliable and user-friendly by testing it.

Shirsh Shukla
7 min readMar 5, 2023

In this article, we’ll look at how to test your Flutter application. So we will see what testing is, what the different categories are, and how you can perform real testing. I would like to give you an overview of testing. What exactly is testing?

Basically, testing is just a piece of code. Which we usually use to automatically validate another piece of code.

The purpose of testing is to check or confirm the validity of other codes by using a specific piece of code. It’s necessary because it’s not feasible to manually test everything.

So why testing is important. That might be your other question.

Let’s say you get a template for counter application when you start your Flutter project for the first time. You might not need testing in that case, but once you add more features, it will become very difficult to test everything manually.

It is ideal that your Flutter application has these three types of tests: unit tests, widget tests, and integration tests.

However, there can be more categories, such as functional testing. For now, let’s focus on the unit test, widget test, and integration test which is the most important part of Flutter testing.

If we go into details, what is a unit test? In a unit test, you are testing a particular function, method, or class, which means you’re testing a very small piece of code or a function.

On the other hand, widget testing is used to test UI or components. And for example, you get the counter application. You have a button over there which when you click on it increases the counter. If we have to test that button, if the tapping is working and the behavior is the same, then we will use widget testing.

In addition, we have integration testing, which basically means testing a particular flow in its entirety. Obviously, it can also be a complete application, or it can be a large part of your application. As a result, integration testing is like combining many things and testing like a flow. let’s also see this test table which I say is CMD.

CMD is essentially a feeling of confidence. It stands for confidence, maintenance, dependency, and execution speed. Thus, the unit is always on the lower side and integration is always on the upper side. A confidence value, for example, indicates how much you can rely on a particular test.

As a result, unit tests have low reliability, widgets have higher reliability, and integration has the highest maintenance cost. Additionally, unit testing has very few low, higher, highest, and same-thing dependencies.

It depends on what you are testing whether there are more dependencies and integration, and execution speed. It will be fast for unit tests, obviously for widgets, and ideally, integration is slow, let’s say you are testing the entire application, then it will be even slower.

The way it works is as follows. You might wonder what tests to use, when to use them, and how to use them.

So I would say here, this is my opinion that you should follow the test pyramid which is ideally like if you see here on the top you see there is a narrow gap and that’s where the widget test fits.

In the middle there is an integration test, in the bottom, there’s a unit test which is more stretched. So what I mean here is that you should do as many of the unit tests as you can because the unit test is easy to perform, it can cover a lot of things as a different method itself.

An integration test is something to which you should give the second priority which is like if you can test a particular flow a lot and then at the end you can give the third priority to the widget test which can be lesser than integration and unit test. Why? Because these days most of the time you can check your smartphone, you can see the UI and everything.

I mean you should definitely do it but yes, unit testing is what you should more focus on than integration and then Widget testing. So that’s more or less about this particular discussion which was just a quick introduction. now we will move to code.

So here we will do unit testing with a counter class example, below image you can see I have already set a project here which is nothing but you don’t have to focus on that as of now let’s go to pubspec.yaml and I’m talking about the Flutter application here. also for more simplicity, I go to the flutter unit test documentation, which gives us more clarification regarding unit testing.

There’s not going to be many things over here. So if we go to dev_dependencies, you will see we have something called flutter_test, which is required to do testing over here. now we could have this test if it was just a dart dependency and you can just use any version whatever you want, will work.

So right here I will go with Flutter Test, this is what is required at the pubspec.yaml. when you have to test, then you have to make sure. So if you are making like if you are using the regular template, then you will get this test folder. Otherwise, ideally, you should create this test folder. Although the name can be different as well.

But I would suggest you have a test folder. And here you will write all your test cases or whatever you want to do with testing.

So let’s say I have to write testing or some test cases for this Counter class, how I will do it and then also I have to make sure that I’ll also cover the real scenarios as well. So please pay attention to this particular thing as much as you can because this is more important. Then only you will understand the real scenarios. let’s go inside it and create a file called counter_test.

one thing to pay attention over here is that this underscore test(counter_test) is really important. this is how when you write a command flutter test or something like that, this is how your Flutter framework will understand that, you are talking about these files where you have underscore test which means these are the test files and it will test all those files.

that’s why this is really important and you have to apply the underscore test with everything. So now we have counter_test.dart

So you see what I did here, these steps I follow,

  • At first, I wrote the “void main()” function
  • Then inside it calls the predefined “test” method for the unit test first place is to take test details like I wrote ‘counter value should be incremented’ text.
  • Then I took the instance of the class in the first step because I want to test the counter class.
  • Then in the second step, I call the “increment method” of the counter class.
  • Then I will use the “expect” function, which will allow me to check whether my “increment” method is working properly.
  • Here the expect function asks for 2 values, “expect(actual, matcher)”
    original or matcher,
  • “Actual” means my result, like here the result is “1” because I incremented.
  • “matcher” means what should be my result like “1” should come.
  • So in step 3, I pass both of these,
  • then run the test

So you see here my test case ran successfully, I used a simple example to get an idea of ​​how the test works, this way you can test even complex functions.

In the next step, we see that we have to check both the methods, “increment” or “decrement” together, whether are working correctly or not.

For this either you can write one more test case like written above for “increment”.

Or you can use the predefined “group” method in which we can check multiple cases together. like this,

I have also attached the source code link here, so you can try it out

Testing your Flutter applications early on can help you catch potential bugs and deliver a high-quality product. In addition, testing ensures that any changes made to the code do not break existing functionality.

In conclusion, testing is essential to the development of high-quality Flutter applications. The better your codebase is tested, the more reliable your applications will be.

If you got something wrong? Mention it in the comments. I would love to improve. your support means a lot to me! If you enjoy the content, I’d be grateful if you could consider subscribing to my YouTube channel as well.

I am Shirsh Shukla, a creative Developer, and a Technology lover. You can find me on LinkedIn or maybe follow me on Twitter or just walk over my portfolio for more details. And of course, you can follow me on GitHub as well.

Have a nice day!🙂

--

--

Shirsh Shukla
Shirsh Shukla

Written by Shirsh Shukla

SDE at Reliance Jio | Mobile Application Developer | Speaker | Technical Writer | community member at Stack Overflow | Organizer @FlutterIndore

No responses yet