writing-python-sdk-spector-mock-api-tests
Skill5.9k starsChanged 4 months agoMITView on GitHub
Writes TypeSpec http-client-python generator mock API tests (azure/unbranded/shared) from a Spector case. Use when given a Spector case link or a PR link that modifies Spector cases under http-specs/azure-http-specs.
- Installs packages
Trending
Quiet since September.about the whole repository
Who's talking about microsoft/typespec
- neovim/nvim-lspconfig“Support for typespec”GitHub · Jan 23, 2024 · 11 reactions
- github-linguist/linguist“Add TypeSpec”GitHub · Mar 27, 2024 · 9 reactions
- orval-labs/orval“Support unevaluatedProperties OpenAPI 3.1”GitHub · Jun 11, 2025 · 5 reactions
- Azure/autorest.typescript“Emitters should not crash on purpose”GitHub · Feb 20, 2025 · 2 reactions
- Azure/azure-sdk-for-java“Finalize removing JUnit, Reactor Test, and SLF4J Simple test dependencies”GitHub · Sep 25, 2024 · 2 reactions
The biggest 5 of 37 mentions linking to the repository that our crawl found. Each line is their own headline.
See all 37
Talked about on GitHub, Bluesky and Hacker News (37 times).
On GitHub · 32
- Jan 23, 2024Support for typespec — neovim/nvim-lspconfig11 reactions · 3 comments
- Mar 27, 2024Add TypeSpec — github-linguist/linguist9 reactions · 10 comments
- Jun 11, 2025Support unevaluatedProperties OpenAPI 3.1 — orval-labs/orval5 reactions · 1 comment
- Feb 20, 2025Emitters should not crash on purpose — Azure/autorest.typescript2 reactions
- Sep 25, 2024Finalize removing JUnit, Reactor Test, and SLF4J Simple test dependencies — Azure/azure-sdk-for-java2 reactions · 1 comment
27 more on GitHub
- Sep 20, 2026deps(deps-dev): bump the lockfile-updates group in /tools/schema-sync with 4 updates — Imagine-That-Ai/BurnBar1 reaction · 5 comments
- Sep 10, 2026Design required: asynchronous messaging contracts and AsyncAPI emission — cataggar/typespec1 reaction · 1 comment
- Sep 9, 2026deps(deps-dev): bump @typespec/compiler from 1.15.0 to 1.16.0 in /tools/schema-sync — Imagine-That-Ai/BurnBar1 reaction · 5 comments
- Aug 20, 2026deps(deps-dev): bump the lockfile-updates group across 1 directory with 4 updates — Imagine-That-Ai/BurnBar1 reaction · 3 comments
- Aug 6, 2026Fix Experimental Visitor Gaps — Azure/azure-sdk-for-net1 reaction
- Jul 25, 2026deps(deps-dev): bump the lockfile-updates group across 1 directory with 4 updates — Imagine-That-Ai/BurnBar1 reaction · 9 comments
- Jul 25, 2026deps(deps-dev): bump the lockfile-updates group in /tools/schema-sync with 4 updates — Imagine-That-Ai/BurnBar1 reaction · 2 comments
- Jul 13, 2026Add AZC0002 Suppression & ForwardsClientCalls attribute for Mgmt Back Compat Methods — Azure/azure-sdk-for-net1 reaction
- May 28, 2026[Bug]: tcgc mismatched enum type names for /specs/type/file/main.tsp — Azure/typespec-azure1 reaction · 1 comment
- Apr 22, 2026Adopt Additional XML Spector Tests in Azure Generator — Azure/azure-sdk-for-net1 reaction · 1 comment
- Apr 21, 2026Adopt Spector Header Encoding Scenarios — Azure/azure-sdk-for-net1 reaction
- Mar 30, 2026Bump @typespec/http-specs in Azure Generator & Adopt New XML Spector Tests — Azure/azure-sdk-for-net1 reaction
- Mar 2, 2026Emit multiple api-versions in our metadata file for multiple services — Azure/azure-sdk-for-net1 reaction
- Mar 2, 2026[python] add test case for https://github.com/Azure/typespec-azure/pull/3923 — msyyc/typespec1 reaction
- Mar 2, 2026[python] add test case for https://github.com/Azure/typespec-azure/pull/3922 — msyyc/typespec1 reaction
- Mar 2, 2026[python] add test case for https://github.com/Azure/typespec-azure/pull/3729 — msyyc/typespec1 reaction
- Feb 26, 2026Update XML Visitor to Use XmlRequestContent for FromEnumerable helper method — Azure/azure-sdk-for-net1 reaction · 1 comment
- Feb 26, 2026[python] add test case for https://github.com/microsoft/typespec/pull/9660 — msyyc/typespec1 reaction
- Feb 17, 2026Adopt New XML Pageable Spector Scenarios — Azure/azure-sdk-for-net1 reaction
- Jan 5, 2026[spector] Add a POST long running API whose response type is `bytes` — Azure/typespec-azure1 reaction · 2 comments
- Jul 3, 2025Automation is not updating the mgmt package.json version to reference new Azure generator version — Azure/azure-sdk-for-net1 reaction
- Jul 2, 2025Add integration test cases Response.StatusCodeRange — Azure/autorest.typescript1 reaction
- May 8, 2026[http-client-csharp-mgmt] Mgmt SDK regen broken by upstream BaseModelProvider resolution change (microsoft/typespec#10600) — Azure/azure-sdk-for-net0 reactions
- Mar 27, 2026Incorrect XML ETag Deserialization in Model Properties — Azure/azure-sdk-for-net0 reactions
- Mar 11, 2026[Bug]: Error: Unimplemented: transformJsonExprForType: Enum — timotheeguerin/typespec0 reactions · 1 comment
- Mar 11, 2026[Bug]: Error: Unimplemented: transformJsonExprForType: Enum — timotheeguerin/typespec0 reactions · 1 comment
- May 13, 2024Microsoft Typespec — PrismJS/prism0 reactions · 1 comment
On Bluesky · 4
- Jan 30, 2024🔥 Hot Repo! 🔥 (100+ new stars) 📦 microsoft / typespec ⭐ 1,776 (+120) 🗒 TypeScript — @github-trending-js.bsky.social2 likes and reposts · 1 reply
- May 31, 2024📦 microsoft / typespec ⭐ 3,427 (+76) 🗒 TypeScript — @github-trending-js.bsky.social1 likes and repost
- Apr 30, 2024🔥 Hot Repo! 🔥 (100+ new stars) 📦 microsoft / typespec ⭐ 2,578 (+177) 🗒 TypeScript — @github-trending-js.bsky.social1 likes and repost · 1 reply
- Jan 26, 2024🔥 Hot Repo! 🔥 (100+ new stars) 📦 microsoft / typespec ⭐ 1,311 (+151) 🗒 TypeScript — @github-trending-js.bsky.social1 likes and repost · 1 reply
On Hacker News · 1
- Sep 20, 2024TypeSpec1 point · 1 comment
In this repository13 other files it gives its agents.
- AGENTS.mdAGENTS.md
- .github/copilot-instructions.mdCopilot instructions
- packages/http-client-java/.github/copilot-instructions.mdCopilot instructions
- packages/http-client-js/.github/copilot-instructions.mdCopilot instructions
- code-reviewSkill
- csharp-spector-coverage-gapsSkill
- emitter-prep-for-prSkill
- http-client-python-bump-and-releaseSkill
- minimal-reproSkill
- pnpm-formatSkill
- diff-upstreamSkill
- generate-from-typespecSkill
- test-unbranded-emitterSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
30 rules. About 0 are common, 13 are distinctive, and 17 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈13
Distinctive · estimate
This author's own choices. Worth a look.
17
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 51
Before starting, ensure the build environment is ready:
- Very specific: line 82
If the Spector case comes from a PR that hasn't been released yet, you must bump the spec dependency in …
- Very specific: line 118
After bumping, run npm run install under packages/http-client-python to update the lock file.
- Distinctive: line 124
Write the python test in:
- Very specific: line 140
If the import root and client/model API surface you need are the same (same module path + same client entrypoint), write ONE shared test in …
- Very specific: line 141
If import paths differ (or one flavor lacks the needed client), write separate tests under both mock_api/azure and mock_api/unbranded.
- Distinctive: line 169
Search in the chosen folder for an existing test covering the same feature area.
- Very specific: line 170
Prefer extending an existing test_*.py when it already imports the same generated module.
- Very specific: line 173
If you extend mock_api/azure/test_<area>.py, also extend mock_api/azure/asynctests/test_<area>_async.py (or create it if missing).
- Very specific: line 174
If you extend mock_api/shared/test_<area>.py, also extend mock_api/shared/asynctests/test_<area>_async.py.
- Very specific: line 177
If no sync test exists, create a new test_<area>.py and also create the async counterpart under asynctests/.
- Very specific: line 182
Use a client() fixture that constructs the generated client and yields it via context manager.
- Very specific: line 184
Follow existing assertion style (direct equality for models.
- Distinctive: line 188
Read the Spector case to identify:
- Distinctive: line 196
call the method with the specified inputs
- Very specific: line 198
if the scenario requires sending data, call the corresponding PUT/POST/PATCH and assert no unexpected error
- Very specific: line 202
Prefer comparing with generated model instances (e.g., models.Foo(...)) when the SDK returns models.
- Very specific: line 203
If the response is a stream or iterator, materialize it (list(...)) before asserting.
- Very specific: line 205
If the scenario is about serialization (e.g., XML), assert round-trip via GET/PUT (pattern: assert client.foo.get() == model.
- Distinctive: line 214
Default: do NOT add new dependencies.
- Distinctive: line 216
Only if your new/extended test imports a package not already available:
- Very specific: line 218
Add it to the appropriate requirements file under packages/http-client-python/tests/requirements/:
- Distinctive: line 223
Avoid adding dependencies unless strictly required by the test.
- Distinctive: line 244
Before opening a PR, run your new or updated test.
- Distinctive: line 285
Wait until you see the server listening message before running tests.
- Distinctive: line 294
Verify that all tests pass before proceeding.
- Very specific: line 300
Create a changelog file under .chronus/changes/ to document the change.
- Distinctive: line 331
Keep the skill concise: prefer adding a single focused test per scenario.
- Distinctive: line 333
Use forward-slash paths only.
- Very specific: line 336
Do NOT run npm run regenerate without --name for verification — CI will handle full regeneration.
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Writing python SDK tests from a Spector case
Inputs
You may receive either:
- Spector case link (preferred): link to a specific scenario/case under:
https://github.com/microsoft/typespec/tree/main/packages/http-specs/specs/...https://github.com/Azure/typespec-azure/tree/main/packages/azure-http-specs/specs/...
- PR link: a GitHub PR that changes one or more Spector cases.
Spector cases define the expected request + response. The goal is to add/extend python tests that validate the generated SDK behaves accordingly.
Output
A python pytest test (sync) added to one of:
packages/http-client-python/tests/mock_api/azurepackages/http-client-python/tests/mock_api/unbrandedpackages/http-client-python/tests/mock_api/shared
And a corresponding async pytest test added under the matching asynctests/ folder:
packages/http-client-python/tests/mock_api/azure/asynctestspackages/http-client-python/tests/mock_api/unbranded/asynctestspackages/http-client-python/tests/mock_api/shared/asynctests
Workflow (copy as checklist)
Test-writing progress:
- Ensure prerequisites are met (pnpm install, package build)
- Identify the Spector case link (directly, or extracted from PR)
- Update spec dependency version if the case is from an unreleased PR
- Decide the destination folder(s): azure vs unbranded vs shared
- Regenerate the specific generated client (do NOT run full regeneration)
- Find existing test file to extend (or create a new one)
- Implement sync + async test(s) that match the case’s request/response expectations
- Update test requirements only if a new dependency is introduced
- Format changed python files with Black (
python -m black <paths> --config ./eng/scripts/ci/config/pyproject.toml) - Validate test locally (start Spector mock server + run pytest)
- Add a changelog entry under
.chronus/changes
Prerequisites — Environment setup
Before starting, ensure the build environment is ready:
- Install dependencies (from repo root):
pnpm install - Build the http-client-python package (required before any
tsp compile):cd packages/http-client-python npm install npm run build
⚠️ Do NOT run
pnpm buildat the repo root — it builds the entire monorepo (including the website) and takes 7+ minutes. Only the http-client-python package build is needed.
Step 1 — Identify the Spector case link
If input is a Spector case link
Use it directly.
If input is a PR link
- List changed files.
- From changed files, pick the ones under:
packages/http-specs/specs/(microsoft/typespec)packages/azure-http-specs/specs/(Azure/typespec-azure)
- Extract the specific case/scenario path(s) to target.
Step 2 — Update spec dependency (if needed)
If the Spector case comes from a PR that hasn't been released yet, you must bump the spec dependency in packages/http-client-python/package.json:
- For
Azure/typespec-azurecases: update@azure-tools/azure-http-specs - For
microsoft/typespeccases: update@typespec/http-specs
To find the latest dev version, check npm:
npm view @azure-tools/azure-http-specs versions --json | tail -5
npm view @typespec/http-specs versions --json | tail -5Pick the newest version that includes the Spector case you need. Prefer a stable version (e.g., 0.1.0-alpha.38) if one exists; only fall back to a -dev.X version (e.g., 0.1.0-alpha.38-dev.2) when no stable version contains the change yet.
Example 1 — a newer stable version is available:
// Before
"@azure-tools/azure-http-specs": "0.1.0-alpha.37",
// After (stable 0.1.0-alpha.38 exists and includes the case)
"@azure-tools/azure-http-specs": "0.1.0-alpha.38",Example 2 — no new stable, only a dev version:
// Before
"@azure-tools/azure-http-specs": "0.1.0-alpha.37",
// After (0.1.0-alpha.38 does not exist yet, use dev)
"@azure-tools/azure-http-specs": "0.1.0-alpha.38-dev.2",Same pattern applies to @typespec/http-specs.
After bumping, run npm run install under packages/http-client-python to update the lock file.
Step 3 — Choose where to put the test
Rule A: Spector in Azure/typespec-azure
Write the python test in:
packages/http-client-python/tests/mock_api/azure
Rule B: Spector in microsoft/typespec
You may need either:
- Option (a) shared only:
.../mock_api/shared - Option (b) both flavors:
.../mock_api/azureAND.../mock_api/unbranded
Decide with this concrete check:
- Locate the generated python package/module for the scenario in BOTH:
packages/http-client-python/tests/generated/azurepackages/http-client-python/tests/generated/unbranded
- If the import root and client/model API surface you need are the same (same module path + same client entrypoint), write ONE shared test in
mock_api/shared. - If import paths differ (or one flavor lacks the needed client), write separate tests under both
mock_api/azureandmock_api/unbranded.
Why: both azure and unbranded tox runs include mock_api/shared, so shared tests are preferred when they can import the same generated package.
Step 4 — Regenerate the specific generated client
Generated code is gitignored (packages/http-client-python/tests/generated/). You must regenerate the specific spec before writing tests.
Compile only the single spec you need (example for azure-core-page):
cd packages/http-client-python
npx tsx ./eng/scripts/ci/regenerate.ts --flavor azure --name azure/core/pageThe --flavor flag selects azure or unbranded. The --name flag is a case-insensitive substring match on the package name.
⚠️ Do NOT run
npx tsx ./eng/scripts/ci/regenerate.tswithout--name— it compiles ALL specs and takes 40+ minutes. Only regenerate the specific spec you need. ⚠️ Do NOT usenpm run regenerate -- --flavor ...— npm may strip the flags. Usenpx tsxdirectly.
Verify the generated client has the expected method:
grep -r "method_name" tests/generated/azure/ < package-name > /Step 5 — Find existing test file (or create one)
- Search in the chosen folder for an existing test covering the same feature area.
- Prefer extending an existing
test_*.pywhen it already imports the same generated module.
- Prefer extending an existing
- In parallel, find the matching async test in
asynctests/.
- If you extend
mock_api/azure/test_<area>.py, also extendmock_api/azure/asynctests/test_<area>_async.py(or create it if missing). - If you extend
mock_api/shared/test_<area>.py, also extendmock_api/shared/asynctests/test_<area>_async.py. - If you extend
mock_api/unbranded/test_<area>.py, also extendmock_api/unbranded/asynctests/test_<area>_async.py.
- If no sync test exists, create a new
test_<area>.pyand also create the async counterpart underasynctests/.
Conventions to match:
- Use
pytest. - Use a
client()fixture that constructs the generated client and yields it via context manager. - For async tests: use
async def client()fixture +async with ...and mark tests with@pytest.mark.asyncio. - Follow existing assertion style (direct equality for models;
list(...)for paged results).
Step 6 — Implement the test from the Spector expectations
- Read the Spector case to identify:
- operation name / route
- HTTP method
- parameter locations (path/query/header/body)
- request body shape + media type
- response status code + headers + body
- Translate into SDK calls:
- construct the client
- call the method with the specified inputs
- assert the returned value matches the expected response
- if the scenario requires sending data, call the corresponding PUT/POST/PATCH and assert no unexpected error
Practical guidance:
- Prefer comparing with generated model instances (e.g.,
models.Foo(...)) when the SDK returns models. - If the response is a stream or iterator, materialize it (
list(...)) before asserting. - Async iterator pattern (seen in existing tests):
result = [item async for item in client.list(...)]. - If the scenario is about serialization (e.g., XML), assert round-trip via GET/PUT (pattern:
assert client.foo.get() == model; client.foo.put(model)).
Async client import patterns (match the folder you’re writing to):
- Azure: import
aiosubmodule alongside models, e.g.from specs.<...> import models, aio, thenasync with aio.<Client>()andawait client.<op>(...). - Shared/unbranded generated clients often expose
.aiomodules, e.g.from <pkg>.aio import <Client>.
Step 7 — Dependencies (only when needed)
Default: do NOT add new dependencies.
Only if your new/extended test imports a package not already available:
- Add it to the appropriate requirements file under
packages/http-client-python/tests/requirements/:base.txt— shared dependencies (pytest, pytest-asyncio, etc.)azure.txt— Azure-specific dependencies (azure-core, azure-mgmt-core)unbranded.txt— unbranded-specific dependencies (corehttp)
Avoid adding dependencies unless strictly required by the test.
Step 8 — Format changed files
Format any python files you changed using Black with the project's shared config:
cd packages/http-client-python
python -m black ./eng/scripts/ci/config/pyproject.toml < paths > --configReplace <paths> with the specific files and/or folders you modified (relative to the http-client-python root).
Alternatively, you can format all test files via the npm script:
npm run format -- --generatorStep 9 — Validate your test locally
Before opening a PR, run your new or updated test. You will need two terminals: one to run the Spector mock API server, and another to run pytest.
- Determine the test flavor. Pick the flavor that matches the test you changed:
- Azure tests →
azure - Unbranded tests →
unbranded
- Azure tests →
Regenerate and install packages (if not already done):
cd packages/http-client-python npx tsx ./eng/scripts/ci/regenerate.ts --flavor <azure|unbranded> --name <spec-name>Create and activate a virtual environment (if one does not already exist):
cd packages/http-client-python/tests python -m venv .venv # Windows: .venv\Scripts\activate # Linux/Mac: source .venv/bin/activateInstall dependencies and the generated SDK:
pip install -r requirements/base.txt pip install -r requirements/<azure|unbranded>.txt # install only a single generated SDK: pip install --no-deps -e generated/<flavor>/<sdk-folder-name>Start the Spector mock API server in a separate terminal:
cd packages/http-client-python npx tsp-spector serve node_modules/@azure-tools/azure-http-specs/specs/ node_modules/@typespec/http-specs/specs/Wait until you see the server listening message before running tests.
Run the test in the original terminal (with venv activated):
cd packages/http-client-python/tests pytest mock_api/ < azure | unbranded | shared > / < test_file > .py -vVerify that all tests pass before proceeding.
Important: If you only added or changed test files, confirming the changed test passes is sufficient. However, if you modified emitter source code (under
generator/oremitter/), you must run the full test suite for the affected flavor(s) to catch regressions before proceeding.
Step 10 — Add a changelog entry
Create a changelog file under .chronus/changes/ to document the change. The file should be a Markdown file with YAML frontmatter specifying the change kind and affected package(s).
File naming convention: <short-description>-<YYYY>-<M>-<DD>-<H>-<m>-<s>.md
Template:
---
changeKind: internal
packages:
- "@typespec/http-client-python"
---
<Brief description of what was added or changed.>Available changeKind values:
| Kind | When to use |
internal | Internal changes not user-facing (most test PRs) |
fix | Bug fixes to existing features |
feature | New user-facing features |
deprecation | Deprecating an existing feature |
breaking | Breaking changes |
dependencies | Dependency bumps |
For test additions, use changeKind: internal and list @typespec/http-client-python as the package.
Notes
- Keep the skill concise: prefer adding a single focused test per scenario.
- Don’t duplicate existing coverage: extend an existing file when reasonable.
- Use forward-slash paths only.
- Generated code is gitignored — do NOT attempt to commit it. Only commit: test files, and
package.json/package-lock.json(if dependency versions were updated). - You should still validate your test locally (Step 9) before pushing. CI runs the full regeneration and the complete test matrix, but catching failures early locally is faster.
- Do NOT run
npm run regeneratewithout--namefor verification — CI will handle full regeneration.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

