text stringlengths 1 372 |
|---|
<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. |
IntelliJ |
VSCode |
<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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.