AgentSkillsCN

testing

为brickston-ai编写并运行pytest与Playwright测试

SKILL.md
--- frontmatter
name: testing
description: Write and run pytest and Playwright tests for brickston-ai

Testing Skill

Overview

Standardized patterns for writing and running tests in brickston-ai.

File Locations

  • Backend tests: apps/api/tests/
  • Frontend tests: apps/web/__tests__/
  • Test scripts: scripts/test_*.py
  • Config: pytest.ini, run_tests.sh

Backend Testing (pytest)

Running Tests

bash
cd apps/api
pytest tests/ -v                    # All tests
pytest tests/routes/ -v             # Specific directory
pytest tests/test_file.py -v        # Single file
pytest tests/test_file.py::test_fn  # Single test

Test Structure

python
import pytest
from httpx import AsyncClient
from app.main import app

@pytest.fixture
async def client():
    async with AsyncClient(app=app, base_url="http://test") as ac:
        yield ac

@pytest.mark.asyncio
async def test_get_endpoint(client):
    response = await client.get("/api/v1/resource")
    assert response.status_code == 200
    assert "data" in response.json()

Database Test Fixtures

python
@pytest.fixture
async def db_session():
    async with AsyncSession(engine) as session:
        yield session
        await session.rollback()  # Clean up after test

Frontend Testing (Playwright)

Running Browser Tests

bash
cd apps/web
npx playwright test                    # All tests
npx playwright test --ui               # Interactive mode
npx playwright test tests/page.spec.ts # Single file

Test Structure

typescript
import { test, expect } from '@playwright/test';

test('should load dashboard', async ({ page }) => {
  await page.goto('/');
  await expect(page.locator('h1')).toContainText('Dashboard');
});

test('should filter properties', async ({ page }) => {
  await page.goto('/properties');
  await page.fill('[data-testid="search"]', 'San Francisco');
  await page.click('button:has-text("Search")');
  await expect(page.locator('.property-card')).toHaveCount(5);
});

Test Patterns

Mocking External APIs

python
from unittest.mock import patch, AsyncMock

@patch('app.services.external_api.fetch_data', new_callable=AsyncMock)
async def test_with_mock(mock_fetch, client):
    mock_fetch.return_value = {"data": "mocked"}
    response = await client.get("/api/v1/endpoint")
    assert response.status_code == 200

Testing Database Queries

python
async def test_query(db_session):
    result = await db_session.execute(text("SELECT 1"))
    assert result.scalar() == 1

CI Integration

Tests run automatically on PR via GitHub Actions.

Checklist

  • Test file created in appropriate tests/ directory
  • Fixtures used for setup/teardown
  • Async tests marked with @pytest.mark.asyncio
  • Mocks used for external dependencies
  • Run tests locally before committing