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 从公开仓库中收录这些内容。

举报或申请下架