Dart 里的测试
Software testing, an important part of app development, helps verify thatyour app is working correctly before you release it.This Dart testing guide outlines several types of testing, and pointsyou to where you can learn how to test yourmobile,web,and server-side apps and scripts.
Terminology: widget vs. componentFlutter, an SDK for building apps for iOS and Android, defines itsGUI elements as widgets. AngularDart, a web app framework,defines its GUI elements as components.This doc uses component (except when explicitly discussing Flutter),but both terms refer to the same concept.
Kinds of testing
The Dart testing docs focus on three kinds of testing, out of themany kinds of testingthat you may be familiar with: unit, component, and end-to-end(a form of integration testing). Testing terminology varies,but these are the terms and concepts that you are likely toencounter when using Dart technologies:
Unit tests focus on verifying the smallest piece of testablesoftware, such as a function, method, or class. Your test suitesshould have more unit tests than other kinds of tests.
Component tests verify that a component (whichusually consists of multiple classes) behaves as expected.A component test often requires the use of mock objectsthat can mimic user actions, events, perform layout,and instantiate child components.
Integration and end-to-end tests verify the behavior ofan entire app, or a large chunk of an app. An integration testgenerally runs on a real device or OS simulator (for mobile)or on a browser (for the web) and consists of two pieces:the app itself, and the test app that putsthe app through its paces. An integration test often measures performance,so the test app generally runs on a different device or OSthan the app being tested.
Generally useful libraries
Although your tests partly depend on the platform your code is intendedfor—Flutter, the web, or server-side, for example—thefollowing packages are useful across Dart platforms:
- package:testProvides a standard way of writing tests in Dart. You can use the testpackage to:
- Write single tests, or groups of tests.
- Use the
@TestOn
annotation to restrict tests to run onspecific environments. - Write asynchronous tests just as you would write synchronoustests.
- Tag tests using the
@Tag
annotation. For example, define a tag tocreate a custom configuration for some tests, or to identify some testsas needing more time to complete. - Create a
dart_test.yaml
file to configure tagged tests acrossmultiple files or an entire package.
- package:mockitoProvides a way to createmock objects,easily configured for use in fixed scenarios, and to verifythat the system under test interacts with the mock object inexpected ways.For an example that uses both package:test and package:mockito,see the International Space Station API library and its unittestsin the mockito package.
Flutter testing
Use the following resources to learn more about testing Flutter apps:
- Testing Flutter AppsHow to perform unit, widget, or integration tests on a Flutter app.
- flutter_testA testing library for Flutter built on top of package:test.
- flutter_driverA testing library for testing Flutter applications on real devices andemulators (in a separate process).
- flutter/examples/flutter_galleryTests for the Flutter gallery example.
- flutter/dev/manual_testsMany examples of tests in the Flutter SDK.
Web testing
Use the following resources to learn more about testing Dart webapplications:
- Testing(a pagein the AngularDart guide)How to use the angular_testpackage to test AngularDart components and subsystems.
- package:webdriverA Dart package for interfacing withWebDriver servers.
Other tools and resources
You may also find the following resources useful for developing anddebugging Dart applications.
IDE
When it comes to debugging, your first line of defense is your IDE.Dart plugins exist for many commonly used IDEs.
Dart DevTools
Dart DevTools is a suite of performance tools for Dart and Flutter.For details, see theDart DevTools documentation.
Continuous integration
Consider using continuous integration (CI) to build your projectand run its tests after every commit. Two CI services for GitHub areTravis CI (for OS X and Unix) andAppVeyor (for Windows).
Travis has built-in support for Dart projects.Learn more at the following links:
- Building a Dart Projectcovers how to configure Travis for Dart projects
- The shelfexample uses the
dart_task
tag (in.travis.yml
) to configurethe build.