test-generator
13
总安装量
3
周安装量
#25077
全站排名
安装命令
npx skills add https://github.com/u9401066/med-paper-assistant --skill test-generator
Agent 安装分布
claude-code
3
opencode
2
antigravity
2
codex
2
gemini-cli
2
Skill 文档
æ¸¬è©¦çææè½
測試éåå¡
/\ E2E (å°é)
/--\ Integration (ä¸ç)
/----\ Unit (大é)
/------\ Static Analysis (åºç¤)
éæ åæå·¥å · (Static Analysis)
å·¥å ·ç¸½è¦½
| å·¥å · | ç¨é | å½ä»¤ |
|---|---|---|
| ruff | Linter + Formatter | uv run ruff check src/ |
| mypy | é¡åæª¢æ¥ | uv run mypy src/ --ignore-missing-imports |
| bandit | å®å ¨æ¼æ´ææ | uv run bandit -r src/ -ll |
| vulture | æ»ä»£ç¢¼æª¢æ¸¬ | uv run vulture src/ --min-confidence 80 |
Bandit å®å ¨ææ
# åªé¡¯ç¤º Medium+ å´é度 (æ¨è¦)
uv run bandit -r src/ -ll
# 顯示ææåé¡ (å
å« Low)
uv run bandit -r src/ -l
# å¸¸è¦ nosec 註解
# nosec B110 - ææç try_except_pass
# nosec B404 - ææä½¿ç¨ subprocess
# nosec B603 - ä¿¡ä»»ç subprocess å¼å«
# nosec B607 - ä¿¡ä»»çé¨åè·¯å¾å·è¡
Vulture æ»ä»£ç¢¼æª¢æ¸¬
# 80% 置信度以ä¸
uv run vulture src/ --min-confidence 80
# ç¢ç whitelistï¼æé¤èª¤å ±ï¼
uv run vulture src/ --make-whitelist > vulture_whitelist.py
Ruff é ç½® (pyproject.toml)
[tool.ruff]
target-version = "py310"
line-length = 100
[tool.ruff.lint]
select = ["E", "F", "W", "I", "UP", "B", "SIM"]
ignore = ["E501"] # è¡é·ç± formatter èç
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["F401"] # å
許 re-export
"tests/**" = ["S101"] # å
許 assert
[tool.ruff.format]
quote-style = "double"
Mypy é ç½® (pyproject.toml)
[tool.mypy]
python_version = "3.10"
warn_return_any = true
warn_unused_ignores = true
disallow_untyped_defs = true
no_implicit_optional = true
[[tool.mypy.overrides]]
module = ["mcp.*", "docx.*", "PyPDF2.*"]
ignore_missing_imports = true
éæ åæå·è¡æµç¨
# 1. Ruff èªå修復 (å
å·è¡)
uv run ruff check src/ --fix --unsafe-fixes
uv run ruff format src/
# 2. Mypy é¡å檢æ¥
uv run mypy src/ --ignore-missing-imports
# 3. Bandit å®å
¨ææ
uv run bandit -r src/ -ll
# 4. Vulture æ»ä»£ç¢¼æª¢æ¸¬ (å¯é¸)
uv run vulture src/ --min-confidence 80
# 宿´ä¸æ¬¡å·è¡
uv run ruff check src/; uv run mypy src/ --ignore-missing-imports; uv run bandit -r src/ -ll
uv run mypy src/ –ignore-missing-imports
3. å®å ¨ææ (å¯é¸)
uv run bandit -r src/ -ll
### å¸¸è¦ Mypy é¯èª¤ä¿®å¾©
| é¯èª¤ | è§£æ³ |
|------|------|
| `no_implicit_optional` | `def foo(x: str = None)` â `def foo(x: Optional[str] = None)` |
| `var-annotated` | `results = []` â `results: List[T] = []` |
| `arg-type` | æª¢æ¥ Optional æ¯å¦éè¦ `or default` |
| `return-value` | 確ä¿å½æ¸è¿åé¡åèè²æä¸è´ |
| `call-overload` | ä½¿ç¨ `# type: ignore[call-overload]` |
---
## Python 測試工å
·
| å±¤ç´ | å·¥å
· | é
ç½® |
|------|------|------|
| Static | `mypy`, `ruff`, `bandit` | pyproject.toml |
| Unit | `pytest` | tests/unit/ |
| Integration | `pytest` + `httpx` | tests/integration/ |
| E2E | `playwright` | tests/e2e/ |
| Coverage | `pytest-cov` | ç®æ¨ â¥80% |
---
## ç®éçµæ§
tests/ âââ conftest.py # å ±ç¨ fixtures âââ unit/ # å®å 測試 â âââ test_domain/ âââ integration/ # æ´å測試 â âââ test_api/ âââ e2e/ # 端å°ç«¯æ¸¬è©¦
---
## å®å
測試模å¼
### å¿
é æ¶µè
1. **Happy Path** - æ£å¸¸æµç¨
2. **Edge Cases** - éçæ¢ä»¶
3. **Error Handling** - é¯èª¤èç
4. **Null/None** - 空å¼èç
### ç¯ä¾çµæ§
```python
class TestUser:
def test_create_user_valid(self): # Happy path
...
def test_create_user_min_length(self): # Edge case
...
def test_create_user_empty_raises(self): # Error handling
with pytest.raises(ValidationError):
...
@pytest.mark.parametrize(...) # å¤åæ¸æ¸¬è©¦
def test_variations(self, input, expected):
...
æ´å測試模å¼
API 測試
@pytest.mark.integration
async def test_create_endpoint(async_client):
response = await async_client.post("/api/users", json={...})
assert response.status_code == 201
DB 測試
@pytest.mark.integration
async def test_save_and_retrieve(repository, db_session):
saved = await repository.save(entity)
retrieved = await repository.get_by_id(saved.id)
assert retrieved is not None
å¸¸ç¨ Fixtures
# conftest.py
@pytest.fixture
def sample_user():
return User(name="Test", email="test@test.com")
@pytest_asyncio.fixture
async def async_client():
async with AsyncClient(app=app) as client:
yield client
@pytest_asyncio.fixture
async def db_session():
async with AsyncSession() as session:
yield session
await session.rollback()
å·è¡å½ä»¤
# éæ
åæ
mypy src/
ruff check src/
# å®å
測試
pytest tests/unit -v
# æ´å測試
pytest tests/integration -v -m integration
# å
¨é¨ + è¦èç
pytest --cov=src --cov-report=term-missing --cov-fail-under=80
# E2E
pytest tests/e2e -v --headed # 顯示ç覽å¨
pyproject.toml é ç½®
[tool.pytest.ini_options]
testpaths = ["tests"]
markers = ["unit", "integration", "e2e", "slow"]
asyncio_mode = "auto"
[tool.coverage.run]
source = ["src"]
branch = true
omit = ["tests/*", "*/__init__.py"]
[tool.coverage.report]
fail_under = 80
show_missing = true
çæ Checklist
- ç¢ºèªæ¸¬è©¦ç®éçµæ§
- Happy path 測試
- Edge cases 測試
- Error handling 測試
- Fixtures è¨å®
- CI workflow æ´å
- è¦èç ⥠80%
ç¸éæè½
code-reviewer– å¯©æ¥æ¸¬è©¦å質