yii.
← Writing
Flutter 技術 · · 4 min read · Read in English

了解剛出爐的 Honey E2E Testing,不是開發者也能幫忙寫 Flutter 測試!

這篇寫於 2023 年。工具和 API 變化很快,部分步驟可能已經不適用。

新的 HoneyTalk 測試語言

Honey 是一個很棒的套件,出了之後就持續在關注它,感覺對開發者來說非常簡單,也對非工程人員很有幫助,只需透過平凡口語的句子來撰寫測試,驗證使用者最常操作的流程與場景,讓我們的 APP 品質更有保障。趕緊來玩看看吧~


Introduction#

  • E2E Test framework
  • 作者為 ClickUp 的開發人員,知名開源者
  • 使用 HoneyTalk 語言搭配句子來撰寫測試,即便不是工程師也能參與,完全不需要了解實作細節
  • 默認為非同步,不需要額外等待耗時操作和動畫
  • 提供 accessibility trees,查看每個元件的可視資訊,包含屬性、位置、大小
  • 夥伴產品有 Hive、Isar 等套件

honey | Flutter PackageHoney is an end-to-end test framework for Flutter apps. It is designed to be really easy to set up and use. The…pub.dev

Installation#

dependencies:
 ...
  honey: <latest-version>

Setup#

在 main() 方法透過環境變數去判斷是否要執行 Honey 初始化,使用 HoneyWidgetsBinding

launch.json of VSCode
launch.json of VSCode
void main() {
  const kIsHoney = bool.fromEnvironment('HONEY');
  if (kIsHoney) {
    HoneyWidgetsBinding.ensureInitialized();
  }

  runApp(const MyApp());
}

Write Test#

在根目錄新建一個 honey 資料夾,每個測試檔的副檔名為 .honey

檢查文字#

  1. see
  2. expect that
  3. verify that
  4. make sure that
  5. assert that
  6. look that

點擊按鈕#

  1. tap
  2. click
  3. hit

輸入文字#

  1. enter
  2. type

滑動#

  1. swipe
  2. slide
  3. scroll
swipe left "list" by 100

呼叫方法#

autoLogin()

tap "Action" button
verify "New note" is visible

more..

條件判斷#

if "button" is visible then
  print "hello"
else
  print "world"
endif

本文範例提供了兩個頁面,我們撰寫自定義測試流程,主頁面為原本的計數器,第二頁顯示一些文字

verify that "0" is visible

tap "Increment" button

verify that "1" is visible

click "Increment"  button

make sure that "2" is visible
expect that "3" is not visible

click "Second" button

expect that "Second" is visible
expect that "Hello world" is visible

honey/honeytalk.md at main · clickup/honeyHoney tests are written in a natural test language called “HoneyTalk” which looks like English. There are many built-in…github.com

Run#

VSCode Configuration

VSCode Extension#

安裝 VSCode 套件,搜尋 Honey,支援 HoneyTalk language 的撰寫,協助點亮所使用的關鍵字

Demo#

App running
App running
Check details of widget
Check details of widget
Test info
Test info

Weakness#

  • 剛開始釋出不久,文件還不完整
  • 撰寫測試的前提,需要有英文對話的能力,文法正確才能寫好測試

Source Code#

GitHub - chyiiiiiiiiiiii/honey_test_example: Honey is a handy tool for people to run E2E tests on…Honey e2e testing is an awesome package. After I tried it for a while, I feel it was so easy and helpful for…github.com


關於我#

贊助#

謝謝你花費時間看完,非常感謝!

如果覺得文章不錯的話可以贊助,讓我有更多動力和熱情分享學習紀錄和生活!請我喝一杯咖啡吧~

https://www.buymeacoffee.com/yiichenhi

最後#

希望有幫助到你/妳,歡迎追蹤我,方便瀏覽最新的文章~

本文原刊登於 Medium。