Blog-Image
  • Codeaamy
  • March 9, 2024

  • 0 Comments

Master Testing in Flutter.

You guys have seen this screen multiple times in your app development journey.

Flutter Index Value not in range error.

This is due to some code not working as it was supposed to. Or that you have missed handled some of the code. Altogether this is terrible behaviour which will push users to delete the application.

So how can we Prevent it? The answer is simple, fix the bug right?

Well, it’s not that simple. To fix the bug it’s very important to actually find the bug. This is when testing comes into play. The more you test your app, the more bugs you can find to fix and make your app bug free.

Adding more features also adds unnecessary bugs. Hence we should test apps from time to time as we do not want the application to crash in production.

In this blog series, you will learn how you can test your flutter app, different kinds of tests in Flutter, the amount of code you test covers and generate Goldens (UI Snapshot) to test the UI changes that take place in the app.

This series will be divided into multiple sections making it easier to follow and jump to required the section as per your requirement. We will be Testing a Todo Application — The application does an Http request to fetch todos and displays them on the list. I would suggest following the tutorial from the start so it will be easier to understand and follow along

Introduction to Testing and Types of Testing in Flutter

Ways of Testing an App ( Brief — just key point)

There are two ways you can test your app.

  • Manual Testing — Run the app and test Manually
  • Automated Testing — Write code which runs and tests the application.

Disadvantages of Manual Testing

  • Time-Consuming.
  • The app can work as expected but can have underneath bugs, this will be missed by manual testing.
  • Difficult to test big apps.
  • Bad for the TDD approach,

Advantages of Automated testing

  • Automated Tests are Fast and do not require a separate person to run them.
  • You cover all/most of the code.
  • Increasing the app won’t hamper the test process.
  • Easier to find bugs and fix them.
  • Best to have a TDD approach.

Different Types of Tests in Flutter

  • Unit Test — Tests a single function, method or class.
  • Widget Test — Test a single Widget or flow of Widgets. (Runs headless in Console)
  • Integration Test — Tests the complete app or a large part of an app. (Requires a device, physical or Simulator)

CMDE Table — Confidence Maintenance Dependencies Execution

CMDE Table

CMDE table lets us know the values we get from each test. As you can see,

  • Confidence — Unit test has the lowest confidence as you are just testing a small function or a class. Whereas the Integration test has the highest confidence as you are testing an Entire widget that too with a given environment.
  • Maintenance — It’s easy to maintain unit tests as compared to widgets and integration tests. We will see the reason later on.
  • Dependencies — The Dependencies management in the unit test is few and is the most in the Integration.
  • Execution — Unit Tests are executed very quickly and hence save a lot of time while running. And Integration test consumes the most time while running as it actually installs the entire application on the device and then tests it.

Which Tests To Run When (Non-TDD approach)

  • Unit Test after every function.
  • Widget and Integration Test before creating PR.

This is a general introduction to Tests and Testing in Flutter. In this article, you got an overview of different kinds of tests and their use cases. In the next blog, we will start with setting up our test project and writing our first Unit Test.

Thank You for reading my blog, if you have any doubts or recommendations please let me know in the comment section below. I would like to know what are the other way to globally listen to network connectivity.

You have 50 Claps per day. Claps do Motivate us to keep writing. Gotta Use Them All

Tags

Leave A Comment