Skills.MD
# Unit Test Generator
This skill provides automated assistance for unit test generator tasks.
## Prerequisites
Before using this skill, ensure you have:
- Source code files requiring test coverage
- Testing framework installed (Jest, Mocha, pytest, JUnit, etc.)
- Understanding of code dependencies and external services to mock
- Test directory structure established (e.g., `tests/`, `__tests__/`, `spec/`)
- Package configuration updated with test scripts
## Instructions
### Step 1: Analyze Source Code
Examine code structure and identify test requirements:
1. Use Read tool to load source files from {baseDir}/src/
2. Identify all functions, classes, and methods requiring tests
3. Document function signatures, parameters, return types, and side effects
4. Note external dependencies requiring mocking or stubbing
### Step 2: Determine Testing Framework
Select appropriate testing framework based on language:
- JavaScript/TypeScript: Jest, Mocha, Jasmine, Vitest
- Python: pytest, unittest, nose2
- Java: JUnit 5, TestNG
- Go: testing package with testify assertions
- Ruby: RSpec, Minitest
### Step 3: Generate Test Cases
Create comprehensive test suite covering:
1. Happy path tests with valid inputs and expected outputs
2. Edge case tests with boundary values (empty arrays, null, zero, max values)
3. Error condition tests with invalid inputs
4. Mock external dependencies (databases, APIs, file systems)
5. Setup and teardown fixtures for test isolation
### Step 4: Write Test File
Generate test file in {baseDir}/tests/ with structure:
- Import statements for code under test and testing framework
- Mock declarations for external dependencies
- Describe/context blocks grouping related tests
- Individual test cases with arrange-act-assert pattern
- Cleanup logic in afterEach/tearDown hooks
## Output
The skill generates complete test files:
### Test File Structure
```javascript
// Example Jest test file
import { validator } from '../src/utils/validator';
describe('Validator', () => {
describe('validateEmail', () => {
it('should accept valid email addresses', () => {
expect(validator.validateEmail('test@example.com')).toBe(true);
});
it('should reject invalid email formats', () => {
expect(validator.validateEmail('invalid-email')).toBe(false);
});
it('should handle null and undefined', () => {
expect(validator.validateEmail(null)).toBe(false);
expect(validator.validateEmail(undefined)).toBe(false);
});
});
});
```
### Coverage Metrics
- Line coverage percentage (target: 80%+)
- Branch coverage showing tested conditional paths
- Function coverage ensuring all exports are tested
- Statement coverage for comprehensive validation
### Mock Implementations
Generated mocks for:
- Database connections and queries
- HTTP requests to external APIs
- File system operations (read/write)
- Environment variables and configuration
- Time-dependent functions (Date.now(), setTimeout)
## Error Handling
Common issues and solutions:
**Module Import Errors**
- Error: Cannot find module or dependencies
- Solution: Install missing packages; verify import paths match project structure; check TypeScript configuration
**Mock Setup Failures**
- Error: Mock not properly intercepting calls
- Solution: Ensure mocks are defined before imports; use proper mocking syntax for framework; clear mocks between tests
**Async Test Timeouts**
- Error: Test exceeded timeout before completing
- Solution: Increase timeout for slow operations; ensure async/await or done callbacks are used correctly; check for unresolved promises
**Test Isolation Issues**
- Error: Tests pass individually but fail when run together
- Solution: Add proper cleanup in afterEach hooks; avoid shared mutable state; reset mocks between tests
## Resources
### Testing Frameworks
- Jest documentation for JavaScript testing
- pytest documentation for Python testing
- JUnit 5 User Guide for Java testing
- Go testing package and testify library
### Best Practices
- Follow AAA pattern (Arrange, Act, Assert) for test structure
- Write tests before fixing bugs (test-driven bug fixing)
- Use descriptive test names that explain the scenario
- Keep tests independent and avoid test interdependencies
- Mock external dependencies for unit test isolation
- Aim for 80%+ code coverage on critical paths
## Overview
This skill provides automated assistance for unit test generator tasks.
This skill provides automated assistance for the described functionality.
## Examples
Example usage patterns will be demonstrated in context.
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「generating-unit-tests」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次