Test generator

From matteocervelli/llms · View interactive version

Test generator is an AI agent skill from the GitHub repository matteocervelli/llms. Generate test scaffolding for modules with proper structure, fixtures, and mock configurations.

How to use this skill

Skill file

Test Generator Skill

Purpose

This skill provides comprehensive test scaffolding and templates for quickly setting up unit and integration tests with proper structure, fixtures, and mocking configurations. It guides the creation of well-structured, maintainable tests following best practices.

Activation

On-demand via command: /generate-tests <file-path>

Example:

/generate-tests src/tools/example/core.py

When to Use

Resources

testing-templates/unit-test.py

Complete pytest unit test template with:

testing-templates/integration-test.py

Complete integration test template with:

Provides

Test File Scaffolding

Fixture Setup

Mock Configurations

Coverage Analysis Helpers

Usage Examples

Example 1: Generate Unit Tests for Module

/generate-tests src/tools/doc_fetcher/core.py

Provides:

Example 2: Generate Integration Tests for API

/generate-tests src/api/endpoints.py

Provides:

Example 3: TypeScript/Jest Tests

/generate-tests src/components/Button.tsx

Provides:

Test Structure Patterns

Arrange-Act-Assert (AAA) Pattern

def test_function_name_condition_expected():
    """Test description."""
    # Arrange - Set up test data and conditions
    input_data = {"key": "value"}
    expected = "result"

    # Act - Execute the function under test
    result = function_under_test(input_data)

    # Assert - Verify the outcome
    assert result == expected

Test Class Organization

class TestClassName:
    """Tests for ClassName."""

    @pytest.fixture
    def instance(self):
        """Create test instance."""
        return ClassName()

    def test_method_success(self, instance):
        """Test successful method execution."""
        result = instance.method()
        assert result is not None

    def test_method_error_handling(self, instance):
        """Test method handles errors."""
        with pytest.raises(ValueError):
            instance.method(invalid_input)

Parametrized Tests

@pytest.mark.parametrize("input,expected", [
    ("test1", "result1"),
    ("test2", "result2"),
    ("test3", "result3"),
])
def test_function_with_parameters(input, expected):
    """Test function with multiple inputs."""
    result = function_under_test(input)
    assert result == expected

Mocking Patterns

Mock External Dependencies

from unittest.mock import Mock, patch

@patch('module.external_service')
def test_with_mocked_service(mock_service):
    """Test with mocked external service."""
    # Configure mock
    mock_service.return_value = "mocked_response"

    # Test function that uses service
    result = function_that_calls_service()

    # Verify
    assert result == "expected"
    mock_service.assert_called_once()

Fixture-Based Mocks

@pytest.fixture
def mock_database():
    """Mock database connection."""
    db = Mock()
    db.query.return_value = [{"id": 1, "name": "test"}]
    return db

def test_database_query(mock_database):
    """Test database query."""
    result = get_data(mock_database)
    assert len(result) == 1
    mock_database.query.assert_called()

Integration Test Patterns

API Integration Testing

def test_api_endpoint_integration(client):
    """Test API endpoint with real client."""
    # Arrange
    payload = {"data": "test"}

    # Act
    response = client.post("/api/endpoint", json=payload)

    # Assert
    assert response.status_code == 200
    assert response.json()["status"] == "success"

Database Integration Testing

def test_database_integration(db_session):
    """Test database operations."""
    # Arrange
    record = Model(name="test", value=123)

    # Act
    db_session.add(record)
    db_session.commit()

    # Assert
    result = db_session.query(Model).filter_by(name="test").first()
    assert result is not None
    assert result.value == 123

Coverage Considerations

Testing Requirements

Coverage Tools

# Python with pytest-cov
pytest --cov=src --cov-report=html

# JavaScript with Jest
jest --coverage

# View coverage report
open htmlcov/index.html  # Python
open coverage/lcov-report/index.html  # JavaScript

Best Practices

Test Naming

Test Organization

Test Independence

Test Readability

Mock Judiciously

Notes

Used When

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.