
testing-patterns
โ 108by langchain-ai ยท part of langchain-ai/skills-benchmarks
Unit testing and integration testing best practices
๐ฅ๐ฅ๐ฅโ VerifiedFreeQuick setup
๐งฉ One of 7 skills in the langchain-ai/skills-benchmarks package โ works on its own, and pairs well with its siblings.
This is the playbook your agent receives when the skill activates โ you don't need to read it to use the skill, but it's here to audit before installing.
Testing Patterns
Write effective, maintainable tests using modern patterns.
Test Structure (AAA Pattern)
def test_user_registration():
# Arrange
user_data = {"email": "test@example.com", "password": "secure123"}
# Act
result = register_user(user_data)
# Assert
assert result.success is True
assert result.user.email == "test@example.com"Mocking External Services
from unittest.mock import Mock, patch
@patch('services.email.send_email')
def test_sends_welcome_email(mock_send):
mock_send.return_value = True
register_user({"email": "test@example.com"})
mock_send.assert_called_once_with(
to="test@example.com",
template="welcome"
)Fixtures and Factories
import pytest
from factories import UserFactory
@pytest.fixture
def user():
return UserFactory.create(role="admin")
@pytest.fixture
def authenticated_client(user):
client = TestClient(app)
client.login(user)
return client
def test_admin_dashboard(authenticated_client):
response = authenticated_client.get("/admin")
assert response.status_code == 200Integration Tests
@pytest.mark.integration
def test_full_checkout_flow(db_session, stripe_mock):
# Create test data
user = create_user()
product = create_product(price=100)
# Execute flow
cart = add_to_cart(user, product)
order = checkout(cart, payment_method="card")
# Verify
assert order.status == "completed"
assert stripe_mock.charges.create.calledTest Coverage Goals
- Unit tests: 80%+ coverage
- Integration tests: Critical paths
- E2E tests: Happy paths only
Copy & paste โ that's it
npx skills add https://github.com/langchain-ai/skills-benchmarks --skill testing-patternsRun this in your project โ your agent picks the skill up automatically.
No common issues documented yet. If you hit a problem, the repository's GitHub Issues page is the best place to look.
Licensed under MITโ you can use, modify, and redistribute it under that license's terms.
View the full license file on GitHub โ