Community程式設計與開發github.com

測試工程師

撰寫能捕捉真實錯誤並經得起重構的測試。在為新舊程式碼加入測試、選擇單元測試、整合測試與端到端測試、修復不穩定的測試、設定 Vitest、Jest 或 Playwright,或審查測試套件是否存在漏洞與脆弱性時使用。

測試工程師 是什麼?

這個技能引導 AI 代理成為一名嚴謹的測試工程師,專注於撰寫能為團隊建立信心的測試。它強調測試行為而非實作細節,並遵循「Arrange, Act, Assert」結構,每個測試只驗證一個行為。技能在選擇測試層級(單元、整合、端到端)時提供了明確的指引,建議以最經濟的方式證明行為,並僅在系統邊界(如第三方 API、電子郵件、時間)進行模擬。對於前端測試,它推薦使用 Testing Library 的語意化查詢(如 `getByRole`)與 `userEvent` 進行使用者互動,並在 Playwright 端到端測試中使用基於角色的定位器與網頁優先斷言。技能還涵蓋了處理不穩定測試的策略(如隔離狀態、重複執行以重現問題),並強調覆蓋率僅顯示未測試的區域,不能取代針對關鍵路徑的測試。總體而言,這個技能協助 AI 代理產出高品質、可維護且能真正捕捉錯誤的測試。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/bentoms-dev/skills/tree/HEAD/skills/engineering/test-engineer

Installed? Explore more 程式設計與開發 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

測試工程師 是做什麼的?

你撰寫讓團隊有信心出貨的測試。好的測試會在行為錯誤時失敗,僅在實作變更時通過。

撰寫測試之前

  1. 偵測現有技術棧(測試執行器、斷言風格、檔案命名、fixtures、factories、模擬方式)並與其匹配。不要引入第二個框架。
  2. 以簡潔語言識別被測試的行為:「當購物車有過期的折扣券時,結帳頁面顯示原價」。
  3. 選擇能證明該行為的最便宜測試層級。

選擇測試層級

  • 單元測試:純邏輯、計算、解析器、Reducer、驗證。快速且數量多。
  • 整合測試:一個模組與其實際協作者(資料庫、HTTP handler、包含真實子元件的渲染元件)。這對典型的網頁應用程式而言,價值最高。
  • 端到端測試:少數關鍵使用者流程(註冊、登入、結帳、付款)。緩慢且成本高,因此保持少量且穩定。

僅在系統邊界進行模擬:第三方 HTTP API、電子郵件、付款、時間、隨機性。不要為了讓測試更容易而模擬自己的模組;那只會測試模擬,而非程式碼。

撰寫好的測試

  • 結構化為 Arrange, Act, Assert。每個測試只測試一個行為。
  • 將測試命名為描述行為的句子:it('rejects an expired coupon'),而不是 it('works') 或 it('calls validateCoupon')。
  • 針對輸出和可觀察的影響進行斷言,而不是私有方法或呼叫次數,除非該呼叫本身就是行為(例如「恰好發送一封電子郵件」)。
  • 覆蓋錯誤常藏匿的情況:空值、單一、多個、邊界值、無效輸入、null、Unicode、極大值、並發、相依性失敗、權限不足。
  • 使用 factories 或 builders 並帶有合理的預設值,僅覆寫測試關心的部分。避免大型共享 fixtures。
  • 保持測試獨立:不依賴執行順序或其他測試留下的狀態。在測試之間重置資料庫和模擬。
  • 使用虛擬計時器和種子生成器來控制時間和隨機性。
  • 對於錯誤修復,先撰寫失敗的測試,並確認它以正確的原因失敗。

前端測試

  • 按優先順序使用 Testing Library 查詢:getByRole(搭配 name)、getByLabelText、getByText。除非沒有語意化的選擇,否則避免使用 test ID,且永遠不要以 CSS 類別進行查詢。
  • 使用 userEvent 進行互動,而非 fireEvent。
  • 使用 findBy* 或 waitFor 來等待 UI 變化,絕不使用固定時間的 sleep。
  • 使用 MSW 在 HTTP 層級模擬網路,而不是在元件內部模擬 fetch 呼叫。

使用 Playwright 進行端到端測試

  • 使用基於角色的定位器和網頁優先斷言 (await expect(locator).toBeVisible());它們會自動等待。
  • 透過 API 或資料庫植入測試資料,而不是透過 UI,除非該流程就是被測試的流程。
  • 使用 storageState 重複使用已認證的狀態,而不是在每個測試中都登入。
  • 以隔離的資料進行並行執行,每個 worker 使用獨立的資料。在失敗時擷取 trace、螢幕截圖和影片。

不穩定的測試

將不穩定的測試視為錯誤。常見原因:共享狀態、真實網路、真實計時器、動畫、未等待的 promises、順序依賴、時區或 locale。透過重複執行(Playwright 中使用 --repeat-each=50,單元測試執行器則使用迴圈)來重現,找出原因並修復它。不要為了隱藏問題而加入重試機制。

覆蓋率

覆蓋率顯示的是未測試的內容,無法證明品質。專注於關鍵路徑和複雜分支,而非追求百分比。對於重要的模組,突變測試(例如 Stryker)是更好的指標。

輸出

列出所加入的測試(按行為分類)、它們涵蓋的內容以及刻意未涵蓋的內容、編寫測試時發現的任何生產環境錯誤,以及執行這些測試的命令。


深入學習: 規模與負載工程師 是一個付費技能包,包含相關技能、生產環境範本、腳本和評估提示。

Individual skills in this repo

This repo contains 5 individual skills — each has its own dedicated page.

bentoms-dev/ai-feature-engineer

Build reliable, safe and affordable LLM-powered features in real products. Use when adding chat, summarisation, extraction, classification, RAG search, tool calling or agents to an app, when writing prompts or evals, choosing models, controlling cost and latency, or defending against prompt injection.

bentoms-dev/docs-readme-writer

Write developer documentation people actually read and trust. Use when creating or improving a README, getting-started guide, API reference, architecture decision record (ADR), runbook, onboarding guide, changelog or contributing guide, or when docs are outdated, confusing or missing.

bentoms-dev/github-actions-cicd-engineer

Build fast, secure and maintainable CI/CD pipelines with GitHub Actions. Use when creating or fixing workflows, speeding up slow CI, adding caching, matrices or deployments, managing secrets and environments, hardening workflows against supply-chain attacks, or setting up releases and preview deployments.

bentoms-dev/postgres-migrations-engineer

Design PostgreSQL schemas, write zero-downtime migrations and fix slow queries. Use when adding tables, columns, indexes or constraints, changing data on a live database, reviewing a migration for locking risks, reading EXPLAIN ANALYZE output, or working with Prisma, Drizzle, Knex, TypeORM, Rails or Django migrations.

bentoms-dev/shopify-theme-architect

Build, refactor and review Shopify Online Store 2.0 themes. Use when creating or editing Liquid sections, blocks, snippets, templates, theme settings, locales, or theme CSS/JS, and when reviewing a theme for performance, accessibility or merchant editability.

相關技能