スキル一覧に戻る
dredd-us

code-testing

by dredd-us

Web-based SSH session manager with chat-like interface

0🍴 0📅 2025年11月1日
GitHubで見るManusで実行

SKILL.md


name: code-testing description: Generate and run unit and integration tests using pytest (Python) or Jest (JavaScript) with fixtures, mocks, and async support. Use for test-driven development, code review validation, coverage verification, and regression testing. Target 80%+ code coverage. Supports pytest markers, Jest snapshots, and CI/CD integration. Triggers on "test", "TDD", "unit test", "integration test", "test coverage", "pytest", "jest".

Code Testing

Purpose

Generate and run comprehensive unit and integration tests following TDD principles with proper fixtures, mocking, and coverage measurement.

When to Use

  • Test-driven development (TDD)
  • Code review validation
  • Coverage verification
  • Regression testing
  • CI/CD pipeline integration
  • Ensuring code quality

Core Instructions

Python Unit Test (pytest)

import pytest
from my_module import calculate_total

@pytest.fixture
def sample_data():
    """Fixture providing test data"""
    return {
        "items": [
            {"price": 10.0, "quantity": 2},
            {"price": 5.0, "quantity": 3}
        ]
    }

def test_calculate_total(sample_data):
    """Test total calculation"""
    result = calculate_total(sample_data["items"])
    assert result == 35.0

def test_calculate_total_empty():
    """Test with empty list"""
    result = calculate_total([])
    assert result == 0.0

@pytest.mark.asyncio
async def test_async_function():
    """Test async operations"""
    result = await fetch_data()
    assert result is not None
    assert "id" in result

JavaScript Test (Jest)

import { render, screen } from '@testing-library/react';
import { calculateTotal } from './utils';
import Component from './Component';

describe('calculateTotal', () => {
  test('calculates total correctly', () => {
    const items = [
      { price: 10, quantity: 2 },
      { price: 5, quantity: 3 }
    ];
    expect(calculateTotal(items)).toBe(35);
  });

  test('handles empty array', () => {
    expect(calculateTotal([])).toBe(0);
  });
});

describe('Component', () => {
  test('renders correctly', () => {
    render(<Component />);
    expect(screen.getByText('Hello')).toBeInTheDocument();
  });

  test('handles async data', async () => {
    render(<Component />);
    const data = await screen.findByTestId('data');
    expect(data).toHaveTextContent('Loaded');
  });
});

Mocking External Dependencies

Python (unittest.mock):

from unittest.mock import Mock, patch

@patch('my_module.external_api_call')
def test_with_mock(mock_api):
    """Mock external API"""
    mock_api.return_value = {"status": "success"}

    result = my_function()

    assert result["status"] == "success"
    mock_api.assert_called_once()

JavaScript (Jest):

jest.mock('./api', () => ({
  fetchData: jest.fn()
}));

import { fetchData } from './api';

test('mocks API call', async () => {
  fetchData.mockResolvedValue({ status: 'success' });

  const result = await myFunction();

  expect(result.status).toBe('success');
  expect(fetchData).toHaveBeenCalledTimes(1);
});

TDD Workflow (Red-Green-Refactor)

1. RED: Write Failing Test

def test_new_feature():
    """Test for feature that doesn't exist yet"""
    result = new_feature(input_data)
    assert result == expected_output

Run test: pytest → ❌ FAILS (feature not implemented)

2. GREEN: Implement Minimum Code

def new_feature(input_data):
    """Minimal implementation to pass test"""
    return expected_output

Run test: pytest → ✅ PASSES

3. REFACTOR: Improve Code

def new_feature(input_data):
    """Refactored implementation"""
    # Clean, efficient implementation
    return process(input_data)

Run test: pytest → ✅ STILL PASSES

Coverage Measurement

Python (pytest-cov)

# Run tests with coverage
pytest tests/ --cov=src --cov-report=html --cov-report=term

# Generate HTML report
pytest --cov=src --cov-report=html
open htmlcov/index.html

# Fail if coverage below threshold
pytest --cov=src --cov-fail-under=80

JavaScript (Jest)

# Run tests with coverage
npm test -- --coverage

# Coverage thresholds in jest.config.js
module.exports = {
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80
    }
  }
};

Best Practices

Structure Tests (AAA Pattern)

def test_user_creation():
    # ARRANGE: Setup test data
    user_data = {"name": "John", "email": "john@example.com"}

    # ACT: Execute the function
    user = create_user(user_data)

    # ASSERT: Verify results
    assert user.name == "John"
    assert user.email == "john@example.com"
    assert user.id is not None

Use Descriptive Names

# Good
def test_calculate_total_with_discount_applied():
    pass

# Bad
def test_calc():
    pass

Test Edge Cases

def test_division_by_zero():
    with pytest.raises(ZeroDivisionError):
        divide(10, 0)

def test_empty_input():
    result = process([])
    assert result == []

def test_null_input():
    result = process(None)
    assert result is None

Use Fixtures for Setup

@pytest.fixture
def database():
    """Setup test database"""
    db = create_test_db()
    yield db
    db.cleanup()

def test_with_db(database):
    user = database.create_user("test")
    assert user.name == "test"

CI/CD Integration

GitHub Actions

- name: Run tests
  run: |
    pytest tests/ --cov=src --cov-report=xml

- name: Upload coverage
  uses: codecov/codecov-action@v3
  with:
    files: ./coverage.xml

Output Formats

JUnit XML (for CI):

pytest --junitxml=test-results.xml

JSON (for parsing):

pytest --json-report --json-report-file=report.json

Performance Testing

import pytest
import time

@pytest.mark.benchmark
def test_performance(benchmark):
    """Benchmark function performance"""
    result = benchmark(expensive_function, large_input)
    assert result is not None

@pytest.mark.timeout(5)
def test_timeout():
    """Test must complete within 5 seconds"""
    result = slow_function()
    assert result is not None

Dependencies

Python:

  • pytest - Test framework
  • pytest-cov - Coverage plugin
  • pytest-asyncio - Async test support
  • pytest-mock - Mocking utilities

JavaScript:

  • jest - Test framework
  • @testing-library/react - React testing
  • @testing-library/jest-dom - Custom matchers

Version

v1.0.0 (2025-10-23)

スコア

総合スコア

50/100

リポジトリの品質指標に基づく評価

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

0/10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

レビュー

💬

レビュー機能は近日公開予定です