Agent skill
test
Usar al crear o modificar tests con pytest
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/test-eduardo-lezama-intranet-dashboard
SKILL.md
Cuándo usar esta skill
- Al crear tests para código nuevo o existente
- Cuando se pida "testear", "añadir tests", "verificar"
- Después de arreglar un bug (test de regresión)
Contexto necesario antes de empezar
- Código a testear (leer y entender)
- Comportamiento esperado
- Casos edge a considerar
- Dependencias externas a mockear
Pasos
1. Verificar estructura de tests
Si no existe tests/, crear:
mkdir tests
touch tests/__init__.py
touch tests/conftest.py
2. Crear conftest.py (si no existe)
# tests/conftest.py
import pytest
from app import create_app
@pytest.fixture
def app():
"""Crear app en modo testing"""
app = create_app('development')
app.config['TESTING'] = True
return app
@pytest.fixture
def client(app):
"""Cliente de test Flask"""
return app.test_client()
3. Crear archivo de test
Nombrar: tests/test_[módulo].py
# tests/test_endpoints.py
def test_api_status_returns_ok(client):
"""GET /api/status debe retornar status ok"""
response = client.get('/api/status')
assert response.status_code == 200
data = response.get_json()
assert data['status'] == 'ok'
4. Mockear APIs externas
from unittest.mock import patch, Mock
def test_energy_client_handles_error(client):
"""Endpoint debe manejar errores del cliente"""
with patch('blueprints.main.EnergyClient') as mock:
mock.return_value.get_energy_summary.side_effect = Exception("API down")
response = client.get('/api/energy')
assert response.status_code == 500
assert 'error' in response.get_json()
5. Ejecutar tests
pytest tests/ -v # Todos
pytest tests/test_app.py -v # Un archivo
pytest -k "test_api" -v # Por nombre
pytest -x # Parar en primer fallo
<patrones_criticos> SIEMPRE:
- Un assert principal por test
- Nombres descriptivos:
test_[qué]_[condición]_[resultado] - Mockear APIs externas (nunca llamar servicios reales)
- Usar fixtures para setup repetitivo
NUNCA:
- Tests que dependen de servicios externos reales
- Tests que dependen del orden de ejecución
- Hardcodear datos que deberían ser fixtures
PREFERENTEMENTE:
- Tests pequeños y enfocados
- Cubrir casos de error además de happy path
- Documentar con docstring qué se está testeando </patrones_criticos>
Estructura de tests recomendada
tests/
├── __init__.py
├── conftest.py # Fixtures compartidos
├── test_app.py # Tests de la app Flask
├── test_endpoints.py # Tests de endpoints API
├── test_energy_client.py # Tests del cliente Energy
├── test_mealie_client.py # Tests del cliente Mealie
└── test_pihole_client.py # Tests del cliente Pi-hole
Checklist de validación
- Tests pasan:
pytest tests/ -v - Tests son independientes entre sí
- APIs externas mockeadas
- Casos de error cubiertos
-
ruff check tests/pasa
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?