text stringlengths 1 474 |
|---|
see the test package documentation.<topic_end> |
<topic_start> |
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:<topic_end> |
<topic_start> |
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:<topic_end> |
<topic_start> |
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. |
<code_start>class Counter { |
int value = 0; |
void increment() => value++; |
void decrement() => value--; |
}<code_end> |
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.<topic_end> |
<topic_start> |
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. |
<code_start>// 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); |
}); |
}<code_end> |
<topic_end> |
<topic_start> |
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. |
<code_start>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); |
}); |
}); |
}<code_end> |
<topic_end> |
<topic_start> |
6. Run the tests |
Now that you have a Counter class with tests in place, |
you can run the tests.<topic_end> |
<topic_start> |
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.IntelliJVSCode<topic_end> |
<topic_start> |
Run tests in a terminal |
To run the all tests from the terminal, |
run the following command from the root of the project:To run all tests you put into one group, |
run the following command from the root of the project:This example uses the group created in section 5.To learn more about unit tests, you can execute this command: |
<topic_end> |
<topic_start>Mock dependencies using Mockito |
Sometimes, unit tests might depend on classes that fetch data from live |
web services or databases. This is inconvenient for a few reasons:Therefore, rather than relying on a live web service or database, |
you can “mock” these dependencies. Mocks allow emulating a live |
web service or database and return specific results depending |
on the situation.Generally speaking, you can mock dependencies by creating an alternative |
implementation of a class. Write these alternative implementations by |
hand or make use of the Mockito package as a shortcut.This recipe demonstrates the basics of mocking with the |
Mockito package using the following steps:For more information, see the Mockito package documentation.<topic_end> |
<topic_start> |
1. Add the package dependencies |
To use the mockito package, add it to the |
pubspec.yaml file along with the flutter_test dependency in the |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.