Testing Modules and Methods in Flutter
-
Objective: Ensure the correctness of modules, small units, and methods. I want to make sure that both counters work correctly, so I will use unit tests in this case.
-
Testing Package: We will use the built-in Flutter testing package.
-
Directory Structure:
- Tests will be organized in a dedicated test folder.
- Test files must end with _test.
-
Entry Point:
- The testing process will start with the main method in the counter_test file.
-
Running Tests:
- Use the flutter_test command to execute the tests.
-
Grouping Tests:
- Each module will be organized into groups, each with a specific group name.
-
Counter Class Instance:
- Create an instance of the Counter class for testing purposes: late Counter counter;
-
Setup Method:
- The setup method will initialize the counter instance and prepare it for testing.
-
Individual Test Cases:
- Each test will be executed independently, ensuring that it does not rely on the results of other tests.
- Each test will have a descriptive name and corresponding implementation.
- The name should reflect the specific case being tested.
-
Expectation:
- Use expect to compare the current state or value with the expected state or value.
-
Isolation of Test Cases:
- Each test case operates independently, meaning the setup state or value is reset for each test.
- This ensures tests do not run sequentially or depend on one another.