Unit Test
Flutter has become a popular framework for developing cross-platform mobile applications
What is Flutter Test? Flutter Test is a testing framework provided by Flutter for writing and executing unit tests for Flutter applications. It is based on the Dart programming language, which is used for Flutter app development. Flutter Test provides a comprehensive set of utilities and APIs for writing tests, organizing test suites
Before we start, it is necessary to add flutter_test: under dev_dependencies: in the pubspec.yaml file.
In general, test files should reside inside a?test?folder located at the root of your Flutter application or package. Test files should always end with?test.dart (Examble : countertest.dart , homepage_test.dart), this is the convention used by the test runner when searching for tests.
Create a class to test :
Next, you need a “unit” to test. Remember: “unit” is another name for a function, method, or class. For this example, create a?Counter?class inside the?lib/counter.dart?file. It is responsible for incrementing and decrementing a?value?starting at?0.
Write a test for our class :
Inside the?counter_test.dart?file, write the first unit test. Tests are defined using the top-level?test?function, and you can check if the results are correct by using the top-level?expect?function. Both of these functions come from the?test?package.
领英推荐
Counter Test Function:
In this example, we are testing the initial value of the page before performing any functions.
Note : The expect function has two parameters: the first one is the value we are testing, and the second one is the expected result
In this example, we are testing the incrementCounter function of the Counter class.
In this test, we are evaluating the decrementCounter function of the Counter class.
This is the introductory article on unit testing. In the second article, we will discuss unit tests for classes that fetch data from live web services or databases, and we will use the Mocktail package.
Software Developer | Mobil Application Developer | Flutter Developer | Android Developer(Native)
1 年Love this ????