Unit Testing Framework

aj-geddes/useful-ai-prompts/skills/unit-testing-framework

作者 aj-geddes3f5182cfd739無授權條款355 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫7 個月前更新

Write comprehensive unit tests with high coverage using testing frameworks like Jest, pytest, JUnit, or RSpec. Use when writing tests for functions, classes, components, or establishing testing standards.

AI 產生的概覽

指導使用 Jest、pytest、JUnit 或 RSpec 撰寫完整的單元測試,涵蓋 AAA 結構、模擬與邊界情況。

功能
提供跨語言與框架(如 Jest、pytest、JUnit、RSpec)撰寫單元測試的指引與參考資料。內容涵蓋 Arrange-Act-Assert 模式、依語言分類的測試案例、模擬與測試替身、非同步測試、覆蓋率與邊界情況,並附有完整的範例測試套件。此外還附帶一個用於建立測試的 shell 指令碼和一個 JavaScript 測試範本。
適用情境
適用於為新函式、類別或元件撰寫測試、提升測試覆蓋率,或建立團隊測試規範。也適合在測試保護下進行重構以及測試驅動開發。
執行需求
需要一個能閱讀 Markdown 參考資料並執行 shell 指令碼的代理。此技能附帶可執行的鷹架指令碼(scripts/scaffold-tests.sh)和一個 JavaScript 測試範本;受測專案通常需要 Jest、pytest、JUnit 或 RSpec 等測試框架。

Unit Testing Framework

Table of Contents

Overview

Write effective unit tests that are fast, isolated, readable, and maintainable following industry best practices and AAA (Arrange-Act-Assert) pattern.

When to Use

  • Writing tests for new code
  • Improving test coverage
  • Establishing testing standards
  • Refactoring with test safety
  • Implementing TDD (Test-Driven Development)
  • Creating test utilities and mocks

Quick Start

Minimal working example:

javascript
// Jest/JavaScript exampledescribe("UserService", () => {  describe("createUser", () => {    it("should create user with valid data", async () => {      // Arrange - Set up test data and dependencies      const userData = {        email: "[email protected]",        firstName: "John",        lastName: "Doe",      };      const mockDatabase = createMockDatabase();      const service = new UserService(mockDatabase);
      // Act - Execute the function being tested      const result = await service.createUser(userData);
      // Assert - Verify the outcome      expect(result.id).toBeDefined();      expect(result.email).toBe("[email protected]");      expect(mockDatabase.save).toHaveBeenCalledWith(        expect.objectContaining(userData),      );    });  });});

Reference Guides

Detailed implementations in the references/ directory:

GuideContents
Test Structure (AAA Pattern) [blocked]Test Structure (AAA Pattern)
Test Cases by Language [blocked]Test Cases by Language
Mocking & Test Doubles [blocked]Mocking & Test Doubles
Testing Async Code [blocked]Testing Async Code, Test Coverage
Testing Edge Cases [blocked]Testing Edge Cases
Example: Complete Test Suite [blocked]import { UserService } from "./user-service";

Best Practices

✅ DO

  • Write tests before or alongside code (TDD)
  • Test one thing per test
  • Use descriptive test names
  • Follow AAA pattern
  • Test edge cases and error conditions
  • Keep tests isolated and independent
  • Use setup/teardown appropriately
  • Mock external dependencies
  • Aim for high coverage on critical paths
  • Make tests fast (< 10ms each)
  • Use parameterized tests for similar cases
  • Test public interfaces, not implementation

❌ DON'T

  • Test implementation details
  • Write tests that depend on each other
  • Ignore failing tests
  • Test third-party library code
  • Use real databases/APIs in unit tests
  • Make tests too complex
  • Skip edge cases
  • Forget to clean up resources
  • Test everything (focus on business logic)
  • Write flaky tests

來源與署名

來源:aj-geddes/useful-ai-prompts位於skills/unit-testing-framework提交3f5182c

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架