How to Write TestCases For API Calls in Flutter?
Here we are going to a built app that calls an API and write test cases for it before we dive into it let’s go through some basic concepts.
Software testing is a process in which we test code to ensure that it produces the excepted results at any given instance.
Flutter tests consist of:
- Unit test – test for methods, functions, or class
- Widget test – test for a single widget
- Integration test – test for the large part of or the entire application
As we are going to test a function that calls the API we’ll be doing unit testing. Let’s create the app. You can create the app using the flutter create command or use the IDE of your choice. In the app, we’ll use the Numbers API which will give random trivia about numbers, and then will test the function that makes the API call. We are assuming you have some sort of experience with flutter app development.
First, we’ll add the http dependency in the pubspec.yaml file and pub get the dependencies.
Now let’s create the app. You can remove all the contents of the main.dart file in the lib folder and remove the file in the test folder. Now we’ll build the app in the main.dart file.
Run the app using the flutter run command or run button in your IDE.
In order to write a test, we need to understand the method to be tested. Here we have a function named getNumberTrivia that calls an API that returns a JSON response of number trivia upon success otherwise it returns an error message.
Now we can write two test cases first to test whether the successful API response returns a text containing the number trivia and second when the API call fails it returns an error message.
Before we start the test we need to understand that we should not make HTTP requests in the test. It’s not recommended. Instead, we must use mocking or stubs. Luckily the HTTP package provides testing.dart file for us to use.
It is recommended that we create a folder called test at the root of the project and write our tests in that. It is already present in our project when we created it. Make sure that you have flutter_test in your dev dependencies and http in the dependencies section of your pubspec.yaml file.
Before we test the function we should make the http dependency as a parameter to the function it will help us in the mocking and stubs part of the test.
Create a dart file you can name it as get_number_trivia_unit_test.dart.Make sure that you put _test at the end of the file name this helps flutter understand the files for the test while using the flutter test command.
Some of the functions to know for the flutter test. These functions come from the flutter_test package.
- group(description,body); You can group test cases related to a particular function using the group function. You can write the description of the test in the description parameter and the body contains all the test cases.
- test(description,body); You can write a test case using the test function to which you can provide the description in the parameter and the body will contain the test case itself.
- expect(actual,matcher); You can test the output by using the expect method by providing it the output of the function as an actual parameter and expected output as a matcher.
There is various matcher you can find in the official documentation.
In order to run the test enter the following command in the terminal