Summary - Advanced testing
- Dart course
- Expert
- Lesson 42/47
- 5 min
Combine integration, property and coverage to write a test suite.
Summary - Advanced testing
This group covered advanced testing: integration tests (component flow), property tests (random invariants), mock (fake dependencies), coverage (scope measurement), golden tests (golden file comparison).
Core: integration tests reality, property tests invariants, mock tests dependencies. Coverage shows how much code is exercised. Golden catches output changes.
These techniques are used together: integration tests isolated with mocks, invariants with property tests, measurement with coverage, visual/text output checks with golden.
Practice Summary: calc + expect + loop
Define a Calculator class with int add(int a, int b) => a + b;. Define a global int testCount = 0; variable. Define a void expect(dynamic actual, dynamic expected, String label) function with testCount++; if (actual != expected) print('FAIL: $label');. In main create final calc = Calculator();; run 10 tests with for (var i = 0; i < 10; i++) { expect(calc.add(i, 0), i, 'identity $i'); }; finally add print('PASS: $testCount tests');.
Starter code
int testCount = 0;
void main() {
// define a Calculator class (add method)
// define an expect function (increments testCount)
// run 10 trials; finally print PASS
}Sample solution
int testCount = 0;
class Calculator {
int add(int a, int b) => a + b;
}
void expect(dynamic actual, dynamic expected, String label) {
testCount++;
if (actual != expected) print('FAIL: $label');
}
void main() {
final calc = Calculator();
for (var i = 0; i < 10; i++) {
expect(calc.add(i, 0), i, 'identity $i');
}
print('PASS: $testCount tests');
}Expected output
PASS: 10 tests
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which test checks invariants on random inputs?
- Integration test
- Golden test
- Property test Correct answer
- Coverage report
Explanation: a property test checks invariants on random inputs.
2 Match the test types
| Term | Meaning |
|---|---|
Integration |
Components |
Property |
Invariants |
Mock |
Fake |
Golden |
Reference |
3 The expect call
____(result, 5);
expect(result, 5);
Explanation: expect compares the result against the expected value.