Testing (Flutter) Apps
CS 442: Mobile App Development
Why should we write tests?
Types of tests
• Unit tests
• Component/Widget tests
• Integration tests
Unit test
• Tests a single function or class (“unit”)
• Verify correct behavior with di erent input and conditions/state
• May depend on external dependencies (e.g., le data, database)
• External dependencies may be problematic!
• Slow down testing (e.g., http, remote database servers)
• External exceptions may break tests (but units are not at fault)
ff
fi
“Mock” dependencies
• Instead of using real/live external dependencies, create mock versions
• Emulate the live version, but without risk of slowdown/ akiness
fl
Component/Widget test
• Tests a single UI component (Flutter Widget)
• Very correct visual and interactive aspects
• Requires simulating “lifecycle” events (e.g., building widget tree, performing
layout) and user input / asynchronous events
• Flutter has comprehensive widget-testing facilities
Integration test
• Tests large portions of or the entire app
• Verify that all widgets, underlying logic, and external dependencies work
together (integrate) correctly
• Recording behavior over test may be used to pro le app performance
• Typically run in an emulator
fi
Trade-offs
Unit Widget Integration
Con dence Low Higher Highest
Maintenance cost Low Higher Highest
Dependencies Few More Most
Execution speed Quick Quick Slow
fi
Continuous Integration (CI)
• Service that runs tests automatically after each substantial code update
• Typically on new commits/pushes/merges
• Critical part of DevOps!
Flutter testing packages
• utter_test: the o cial testing library from Flutter
• Unit and Widget testing utilities
• test: general-purpose Dart testing library
• Unit tests for both pure Dart & Flutter
• mockito: object mocking library
• integration_test: for app-level testing
• Multi-widget/screen tests; can also be used for pro ling
fl
ffi
fi