Android 和 iOS 上整合測試的預設 golden-file 比對器已變更。
當使用 `package:integration_test` 在 Android 裝置或模擬器,或 iOS 裝置或模擬器上執行測試時,預設的 `goldenFileComparator` 已變更,並且現在正確地使用主機檔案系統。
摘要
#
除非在測試中手動設定使用者自訂的 goldenFileComparator,或透過
flutter_test_config.dart 檔案設定,否則 Android 和 iOS 裝置及模擬器/模擬器現在有新的預設值,會代理到本地主機的檔案系統,修正了一個長期存在的錯誤(#143299)。
背景
#
integration_test
套件,以及其與 flutter_test 的整合,過去一直存在一個錯誤,當使用
matchesGoldenFile
或類似 API 時,會拋出 FileSystemException。
部分使用者可能透過撰寫並使用自訂的 goldenFileComparator
來解決此問題:
import 'package:integration_test/integration_test.dart';
import 'package:my_integration_test/custom_golden_file_comparator.dart';
void main() {
goldenFileComparator = CustomGoldenFileComparatorThatWorks();
// ...
}
這類變通方法現在已不再需要,若對預設值進行型別檢查,也將無法像以往那樣運作:
if (goldenFileComparator is ...) {
// The new default is a new (hidden) type that has not existed before.
}
遷移指南
#在大多數情況下,我們預期使用者不需要做任何事——這在某種意義上是 新 的功能,用來取代原本無法運作且會導致未處理例外、進而造成測試失敗的功能。
如果你有自行撰寫自訂的測試基礎架構與比較器(comparator),建議考慮移除 goldenFileComparator
覆寫,改為依賴(新的)預設值,這應該會如預期般運作:
import 'package:integration_test/integration_test.dart';
import 'package:my_integration_test/custom_golden_file_comparator.dart';
void main() {
goldenFileComparator = CustomGoldenFileComparatorThatWorks();
// ...
}
有趣的小知識:現有用於 web 平台的程式碼已被重複利用。
時程
#
合併於版本:3.29.0-0.0.pre
穩定版發佈:3.32
參考資料
#相關 API:
-
flutter_test,介紹了flutter_test_config.dart及其功能。 -
goldenFileComparator,實作了比較功能,且可由使用者自訂。
相關議題 (Issues):
- Issue 143299,這是許多用戶回報長期存在錯誤的其中之一。
-
Issue 160043,詳細說明了為什麼
matchesGoldenFile會失敗的技術原因。
相關 PR:
Unless stated otherwise, the documentation on this site reflects Flutter 3.44.0. Page last updated on 2026-06-14. View source or report an issue.