MacHu-GWU/sqlalchemy_upsert_kit-project / .claude/skills/pywf-oss-base
pywf-oss-base
FlaggedShared conventions, references, and scripts of the pywf-oss skills for pywf open-source Python libraries: setup, layout, code style, tests, user docs and the agent skills that hold API and maintenance knowledge, CI and hosting, release, dependencies, and secrets. Load it before ad-hoc work on modules, tests, or docs in such a repo, for a question about these conventions, or when unsure which pywf-oss skill fits a job.
references/11-test-writing.md
View on GitHub---
description: "How a test file body is written: the template, imports with the subject under test first, naming and grouping, parametrize, docstrings, and independence from order and working directory. Read when writing or reviewing the test functions inside a test file."
---
# Writing Tests
Where test files live and how they end is in `10-test-layout.md`. This file covers what goes between the header and the `run_cov_test` block.
---
## 1. Template
```python
# -*- coding: utf-8 -*-
from <package_name>.subpackage.module_name import func_name
from <package_name>.subpackage.module_name import ClassName
import pytest
from pathlib import Path
from <package_name>.paths import path_enum
def test_func_name():
result = func_name(...)
assert isinstance(result, ClassName)
class TestClassName:
def test_method_name(self):
...
if __name__ == "__main__":
from <package_name>.tests import run_cov_test
run_cov_test(
__file__,
"<package_name>.subpackage.module_name",
preview=False,
)
```
---
## 2. Import order in test files
This differs from source files:
1. **The subject under test, first:** the names from the module this file maps to, imported absolutely from that exact module. They are the file's protagonists.
2. `import pytest` and `import typing as T`, if needed.
3. Standard library.
4. Third party.
5. Other modules of this package, as absolute imports (`from <package_name>.x import y`), including helpers from `<package_name>.tests`.
Test files use absolute imports only. One imported name per statement still applies (`09-code-style.md`).
---
## 3. Writing good tests
- **Naming:** `test_func_name` or `test_func_name_case`. Group the tests of one class in `class TestClassName:`.
- **Docstrings:** one or two lines on what is verified and why it matters; none when the name says it all.
- **Parametrize** tests that differ only in data, instead of writing near-duplicate functions.
- **Independence:** never depend on run order or the working directory. Use `path_enum` or `tmp_path` for files.
- **Behavior over lines:** target behavior and edge cases. Coverage is the check, not the goal.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 471k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |