Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


Create tests for Flutter apps

Amey Sunu


Why purpose do tests serve? We can use them to create a miniature runtime version of our code that highlights and runs the most efficient part of the program.

This ensures that our created code has nothing to do with the developer’s environment or any other such issues.

How to create tests

To get started with simple Flutter tests, you need to understand two of the utmost testing parameters: test and expect. test is where the developer writes code for the test to occur/take place. expect is the expected outcome of the test, which is then plotted against the actual outcome, and hence decides if the test was successful.

Sample test

Say we have a Flutter app with a button that adds two values, and returns the output to Text on the app.

  • There are two textfields, where the user enters the numbers to be added. Let’s say these numbers are 4 and 6.

  • When the Add button is tapped, it adds 6 and 4 and returns the output, 10.

So, our test here is to ensure that the addition function on the button works, no matter what the circumstance is.

void main() {
  var firstNumber = 8;
  var secondNumber = 2;

  test('check if addition happens', () {
    var result = firstNumber + secondNumber;
    expect(10, result);

In the code above, we have two variables: firstNumber and secondNumber. We add both of these values and then save the outcome to result. This particular value of result is then compared with the actual addition value of the firstNumber and secondNumber.

To run your test, simply run:

flutter test

If your test is correctly passed, then you’ll get the following message printed on the console.

00:06 +1: All tests passed!  


The reason why we test our app is because later on, when our app is deployed on a large scale platform, it becomes really hard for developers to sit and debug every line of the code. Testing helps us do this in advance.

When we consistently push various updates to our app, we may break our code. As a result of this breakage, very small hard-to-reach parts of the code may also be affected. Testing will let us know in case any such thing has gone haywire.



View all Courses

Keep Exploring