3.5 KiB
3.5 KiB
Discover Project Test Patterns
Run this discovery process once at the start of every autotest-writer session. Cache results — do not repeat searches for the same project.
1. Framework Detection
Search for test dependencies in pyproject.toml or requirements*.txt:
| Found | Framework |
|---|---|
pytest |
pytest (default assumption) |
unittest only |
stdlib unittest |
anyio, pytest-asyncio |
async pytest |
hypothesis |
property-based testing present |
If none found — default to pytest.
2. Test File Layout
Run a glob search for test_*.py and *_test.py files.
Determine:
- Root test folder:
tests/,test/, inline next to source, or mixed - Naming convention:
test_{module}.pyor{module}_test.py - Mirrors source tree? e.g.
src/auth/login.py→tests/auth/test_login.py
3. conftest.py Analysis
Read all conftest.py files found (root + subdirectories).
Extract:
- All fixture names and their scopes (
function,session,module) - Factory fixtures (return a callable/class)
- Common parametrize patterns
- Any custom pytest plugins or hooks registered
4. Read Sample Tests
Read 2–3 representative test files (pick files that seem most complete, not trivial ones). Extract the following patterns:
Import style
# Note: stdlib first? third-party second? local last?
# Note: are there "from __future__ import annotations"?
Class vs function tests
- Are tests in classes (
class TestLogin:) or bare functions? - If classes — do they inherit from anything?
Assertion style
- Plain
assert a == b assert a == b, "message"- Custom matchers:
assert_that(a).equals(b),expect(a).to.equal(b)
Mock / patch style
unittest.mock.patchas decoratorunittest.mock.patchas context managerpytest-mockviamockerfixtureMagicMock/AsyncMock/create_autospec
Async style
@pytest.mark.asyncio+async def test_anyiobackend marker- Synchronous wrappers around async code
Parametrize style
@pytest.mark.parametrize("input,expected", [...])
or inline in class docstring (hypothesis)?
5. Factory / Builder Patterns
Search for:
factory_boy—UserFactory,PostFactoryclassespytest-factoryboy—register()usage- Custom builder fixtures that return objects
fakerusage for test data generation
6. Database / External Service Patterns
- Does the project use
pytest-django,pytest-sqlalchemy, or custom DB fixtures? - Are there transaction rollback fixtures (
db,transactional_db)? - Are HTTP calls mocked via
responses,httpretty,respx, orpytest-httpx? - Is there a test database URL in
.env.testorpytest.ini?
7. Markers and Config
Read pytest.ini, setup.cfg, or [tool.pytest.ini_options] in pyproject.toml.
Note:
- Custom markers (
@pytest.mark.integration,@pytest.mark.slow) asyncio_modesetting (autoorstrict)testpaths— where pytest looks for tests- Any
filterwarningsthat should be preserved
Output Format
Summarize findings as:
Framework: pytest 8.x
Async: pytest-asyncio, asyncio_mode=auto
Test root: tests/
Naming: test_{module}.py mirroring src/
Classes: no (bare functions)
Fixtures: conftest.py at root; session-scoped db_engine, function-scoped db_session
Factories: factory_boy (UserFactory, ProjectFactory)
Mocks: pytest-mock (mocker fixture)
HTTP mocks: respx
Markers: integration, slow, unit
Assertions: plain assert