Unit Tests for Mobile Apps: why your project needs it and best practices for Flutter

As a trending technology with high demand, Flutter apps are currently one of the most interesting solutions for business applications. Flutter is a technology that allows Mobile (IOS and Android), Web, and Desktop (Windows and MacOS) apps in a single codebase, so it is understandable why Flutter apps are getting a lot of attention lately. […]

Category

QA/Testing

Posted

Olha

Oct 3, 2024

As a trending technology with high demand, Flutter apps are currently one of the most interesting solutions for business applications. Flutter is a technology that allows Mobile (IOS and Android), Web, and Desktop (Windows and MacOS) apps in a single codebase, so it is understandable why Flutter apps are getting a lot of attention lately.

However, there’s a problem with developing multiple applications in a single codebase. To make sure that all apps are working as expected, excessive manual testing needs to be done. That’s where Unit Testing gets handy: it helps to reduce the time needed for Manual/UI Testing, ensures against bugs and errors, and generally improves the quality of code. 

Unit Testing in Flutter is a lot easier if developers use architecture correctly. It doesn’t matter if Flutter App uses MVP/MVC/MVVM, BloC, GetX, or Redux – all of them are testable. What matters is a good understanding of the chosen architecture and making sure that the codebase is written consistently according to the selected architecture.

There is a difference in how logic is divided between classes and functions in every architecture, but there are two common rules for every architecture out there to be testable — each project class should support a single objective and each function within a class should fulfill only its scope. There should be common practices about how the team uses architecture and all nuances should be in the project wiki or other forms of documentation.

If the codebase of a project is ready, let’s take a look at the basics of Unit Testing. Because our classes depend on other parts of the app to test them, developers should create a mock, fake, or stub of classes that the class in the test depends on. Commonly in Flutter, to perform Unit Tests, developers create mockups using a library called Mockito. It can mock full classes with all its internal functions, and developers can even add custom behavior for these functions to test complex logic. 

After all preparations, it is time to write some tests. Unit tests in Flutter commonly consist of the following parts: Setup, Execution, and Assertions.

Setup, Execution, and Assertions 

In setup, a developer should prepare the necessary environment and dependencies for the unit test. This may include creating instances of classes, initializing variables, and configuring any required dependencies. 

The execution phase involves running the code you intend to test. For unit tests, this usually means calling a function or method with certain inputs and capturing the resulting output.

During the assertions phase, you check whether the output aligns with the expected behavior. Flutter offers a variety of assertion functions to help you ensure the correctness of your code. 

Using these basic practices developers can start to write Unit tests for the Flutter app or even implement full TDD practice in a project. Even though writing tests takes some additional time while developing the app it will improve the quality of your product, it will make your codebase more consistent, and save time during testing the app manually. 

Olha is an Android developer and has been a part of the team at Swan since 2021. She enjoys the friendly team at Swan. She also appreciates the beautiful office setting. She often takes walks around the office on her lunch break or after work. Her choice of career is no surprise since she’s been fascinated by computers since she was four-years-old. Outside of work, she has a host of hobbies that keep her busy, including music, art, and tabletop games.