Add test dependency
# pubspec.yaml
dev_dependencies:
# If this is a dart package
test: ^1.17.4
# If this is a flutter package
flutter_test:
sdk: flutter
Slide 6
Slide 6 text
How to write test
// Test target
int addOne(int num) => num + 1;
// Tests are specified using the test() function
// Test assertions are made using expect()
void main() {
test('test add 1', () {
expect(addOne(9), 10);
// Or using matcher
expect(addOne(9), equals(10));
});
}
Slide 7
Slide 7 text
How to write test
// Test target
int addOne(int num) => num + 1;
// Tests are specified using the test() function
// Test assertions are made using expect()
void main() {
test('test add 1', () {
expect(addOne(9), 10);
// Or using matcher
expect(addOne(9), equals(10));
});
}
Slide 8
Slide 8 text
How to write test
// Test target
int addOne(int num) => num + 1;
// Tests are specified using the test() function
// Test assertions are made using expect()
void main() {
test('test add 1', () {
expect(addOne(9), 10);
// Or using matcher
expect(addOne(9), equals(10));
});
}
Slide 9
Slide 9 text
How to write test
// Test target
int addOne(int num) => num + 1;
// Tests are specified using the test() function
// Test assertions are made using expect()
void main() {
test('test add 1', () {
expect(addOne(9), 10);
// Or using matcher
expect(addOne(9), equals(10));
});
}
Slide 10
Slide 10 text
Test Future
Slide 11
Slide 11 text
Test Future
// Test target
Future addOneAsync(int num) async => num + 1;
Slide 12
Slide 12 text
Test Future
void main() {
test('Test addOneAsync via completion matcher', () {
Future data = addOneAsync(9);
expect(data, completion(10));
});
test('Test addOneAsync via async/await', () async {
int data = await addOneAsync(9);
expect(data, 10);
});
}
Slide 13
Slide 13 text
Test Future
void main() {
test('Test addOneAsync via completion matcher', () {
Future data = addOneAsync(9);
expect(data, completion(10));
});
test('Test addOneAsync via async/await', () async {
int data = await addOneAsync(9);
expect(data, 10);
});
}
Slide 14
Slide 14 text
Test Future
void main() {
test('Test addOneAsync via completion matcher', () {
Future data = addOneAsync(9);
expect(data, completion(10));
});
test('Test addOneAsync via async/await', () async {
int data = await addOneAsync(9);
expect(data, 10);
});
}
Slide 15
Slide 15 text
Test Future error
// Test target
Future willThrowException() async => throw Exception();
Slide 16
Slide 16 text
Test Future error
void main() {
test('Test future error via throwsException matcher', () {
expect(willThrowException(), throwsException);
});
}
Slide 17
Slide 17 text
Test hidden async operations
// Test target
class Counter {
var value = 9;
void addOne() {
Future(() => value = value + 1);
}
}
Slide 18
Slide 18 text
Test hidden async operations
void main() {
test('test counter addOne', () {
final counter = Counter();
counter.addOne();
// Assertion will fail, counter.value is still 9.
expect(counter.value, 10);
});
}
Assertion時点に、addOne中のFuture
タスクはまだ実行されていません