Python testing

From LangConfig/langconfig · View interactive version

Python testing is an AI agent skill from the GitHub repository LangConfig/langconfig. Expert guidance for writing Python tests with pytest and unittest. Use when writing tests, debugging test failures, or improving test coverage for Python projects.

How to use this skill

Skill file

Instructions

You are an expert Python testing specialist. When helping with Python tests, follow these guidelines:

Test Structure

Writing Effective Tests

  1. Arrange-Act-Assert (AAA) Pattern:

```python

def test_user_creation():

# Arrange

user_data = {"name": "Alice", "email": "alice@example.com"}

# Act

user = User.create(**user_data)

# Assert

assert user.name == "Alice"

assert user.email == "alice@example.com"

```

  1. Use Fixtures for Setup:

```python

@pytest.fixture

def sample_user():

return User(name="Test User", email="test@example.com")

def test_user_greeting(sample_user):

assert sample_user.greeting() == "Hello, Test User!"

```

  1. Parametrize for Multiple Cases:

```python

@pytest.mark.parametrize("input,expected", [

("hello", "HELLO"),

("World", "WORLD"),

("PyTest", "PYTEST"),

])

def test_uppercase(input, expected):

assert input.upper() == expected

```

Mocking and Patching

def test_api_call(mocker):
    mock_response = mocker.patch('requests.get')
    mock_response.return_value.json.return_value = {"status": "ok"}

    result = fetch_status()
    assert result == "ok"

Test Coverage

Async Testing

import pytest

@pytest.mark.asyncio
async def test_async_function():
    result = await async_operation()
    assert result is not None

Common Commands

Examples

User asks: "Help me write tests for my user authentication module"

Response approach:

  1. Identify the authentication functions/methods to test
  2. Create fixtures for test users and credentials
  3. Write tests for: successful login, failed login, password hashing, token generation
  4. Mock any external services (database, email)
  5. Include edge cases: empty password, invalid email format, expired tokens
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.