•  


Introduction | Flutter

An introduction to unit testing

How can you ensure that your app continues to work as you add more features or change existing functionality? By writing tests.

Unit tests are handy for verifying the behavior of a single function, method, or class. The test package provides the core framework for writing unit tests, and the flutter_test package provides additional utilities for testing widgets.

This recipe demonstrates the core features provided by the test package using the following steps:

  1. Add the test or flutter_test dependency.
  2. Create a test file.
  3. Create a class to test.
  4. Write a test for our class.
  5. Combine multiple tests in a group .
  6. Run the tests.

For more information about the test package, see the test package documentation .

1. Add the test dependency

#

The test package provides the core functionality for writing tests in Dart. This is the best approach when writing packages consumed by web, server, and Flutter apps.

To add the test package as a dev dependency, run flutter pub add :

flutter pub add dev:test

2. Create a test file

#

In this example, create two files: counter.dart and counter_test.dart .

The counter.dart file contains a class that you want to test and resides in the lib folder. The counter_test.dart file contains the tests themselves and lives inside the test folder.

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 , this is the convention used by the test runner when searching for tests.

When you're finished, the folder structure should look like this:

counter_app/

  lib/

    counter.dart

  test/

    counter_test.dart

3. 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 .

dart
class
 Counter
 {

  int
 value = 
0
;


  void
 increment
() => value++;


  void
 decrement
() => value--;

}

Note: For simplicity, this tutorial does not follow the "Test Driven Development" approach. If you're more comfortable with that style of development, you can always go that route.

4. 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.

dart
// Import the test package and Counter class

import
 'package:counter_app/counter.dart'
;

import
 'package:test/test.dart'
;


void
 main
() {

  test
(
'Counter value should be incremented'
, () {

    final
 counter = 
Counter
();


    counter.
increment
();


    expect
(counter.value, 
1
);

  });

}

5. Combine multiple tests in a group

#

If you want to run a series of related tests, use the flutter_test package group function to categorize the tests. Once put into a group, you can call flutter test on all tests in that group with one command.

dart
import
 'package:counter_app/counter.dart'
;

import
 'package:test/test.dart'
;


void
 main
() {

  group
(
'Test start, increment, decrement'
, () {

    test
(
'value should start at 0'
, () {

      expect
(
Counter
().value, 
0
);

    });


    test
(
'value should be incremented'
, () {

      final
 counter = 
Counter
();


      counter.
increment
();


      expect
(counter.value, 
1
);

    });


    test
(
'value should be decremented'
, () {

      final
 counter = 
Counter
();


      counter.
decrement
();


      expect
(counter.value, -
1
);

    });

  });

}

6. Run the tests

#

Now that you have a Counter class with tests in place, you can run the tests.

Run tests using IntelliJ or VSCode

#

The Flutter plugins for IntelliJ and VSCode support running tests. This is often the best option while writing tests because it provides the fastest feedback loop as well as the ability to set breakpoints.

  • IntelliJ

    1. Open the counter_test.dart file
    2. Go to Run > Run 'tests in counter_test.dart' . You can also press the appropriate keyboard shortcut for your platform.
  • VSCode

    1. Open the counter_test.dart file
    2. Go to Run > Start Debugging . You can also press the appropriate keyboard shortcut for your platform.

Run tests in a terminal

#

To run the all tests from the terminal, run the following command from the root of the project:

flutter test test/counter_test.dart

To run all tests you put into one group , run the following command from the root of the project:

flutter test --plain-name "Test start, increment, decrement"

This example uses the group created in section 5 .

To learn more about unit tests, you can execute this command:

flutter test --help
- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본