kirodotdev/kirocrew/writing-tests

Claude Code and agent skill by kirodotdev/kirocrew

Trust tier 2
  • community
LLM review pending
Source check pending

Checking that the SKILL.md file is still in the repository.

Kiro Crew repo only: how to write a backend pytest test with NO side effects that does not flake. Use when adding, editing, reviewing or debugging a test there: which conftest applies, what leaks (temp dirs, data home, ~/.kiro, cron, threads), the seven flake classes, cross-platform traps.

Install

npx vskill@latest install kirodotdev/kirocrew/writing-tests

SKILL.md

The same SKILL.md was published earlier as kirodotdev/kirocrew/writing-tests.

Writing a Kiro Crew test that does not leak and does not flake

Scope guard: this skill applies ONLY to the Kiro Crew source repository (or a
worktree of it). Its rules are conventions of this repo's suite. In any other
project, ignore it.
The canonical, longer reference is
If this skill and that document disagree, the document wins. What this skill adds is
the decision order — what to check first, and what the failure looks like when you
get it wrong.

Where this skill stops, and which sibling takes over

This skill owns authoring a test: isolation, determinism, cross-platform behaviour, diagnosing a residue failure, and suite speed. Three siblings own the neighbouring steps, and none of them restates what is here:

You are doingSkill
Writing, fixing, or speeding up a testthis skill
Running the build gate in a worktree, and deciding whether a red is yourskirocrew-worktree-dev
Driving a branch to a review-ready PR and through CIkirocrew-prepare-pr
Polling that PR until it is greenbabysit

The line that matters most in practice: kirocrew-worktree-dev tells you whether a failure is yours (re-run it on origin/main, mine CI for what is genuinely flaky); once it is yours, the fix is here. Do not fix a flake from a summary of the determinism classes — pick the class from the symptom, in Rule 2.

The two properties, and why they are one problem

A test must be hermetic (no effect that outlives it, anywhere but its own tmp dir) and deterministic (same verdict every run, on every platform, in any order). They are the same problem because the suite runs -n auto --dist loadgroup: a side effect is not just untidy, it is the input to another test on the same worker, and it surfaces as a flake in a file you never touched.

Rule 0 — Know which conftest is under your file BEFORE you isolate anything

setup.cfg declares two testpaths and they do not get the same fixtures. The table is in testing-conventions.md § Which conftest you are standing on — read it rather than a second copy here, which would drift. The short version: only test/ gets test/conftest.py; src/kiro_crew/apps/builtins/**/tests/ (and the one **/*_tests/ suite) gets the rootdir conftest.py plus that suite's own conftest.py where one exists.

The rootdir conftest.py is the host floor — the guards that protect the developer's machine, so they hold everywhere. That covers damage (temp dirs, the data home, services) and resource exhaustion: the xdist worker budget is registered there, from the repo-root xdist_budget.py, because a run that spawns one worker per core on a small laptop takes the machine down and does not care which testpath asked. Everything else is in test/conftest.py.

Getting this wrong is the most expensive mistake in this suite, because it fails silently and asymmetrically: an in-package test that assumes test/conftest.py's fixtures passes on CI (where the operator's home is empty) and damages a real install locally. When you add isolation, ask: could a test in ANY testpath damage the host — or consume enough memory, cores or disk to take it down — without this? If yes it belongs at the rootdir; if no, put it in test/conftest.py, where the in-package suites pay nothing for it.

Rule 1 — The side-effect floor: what actually leaks

Work down this list. Each item is a real leak that has happened here.

1a. Temp directories — register the destruction in the SAME scope

Prefer tmp_path. If you need mkdtemp(), register cleanup with addCleanup on the next line — never an rmtree in tearDown, because unittest skips tearDown entirely when setUp raises, so that shape leaks on exactly the failing runs nobody watches. The before/after example is in testing-conventions.md § Rules.

ignore_errors=True also hides a cleanup that could not finish, so it is not proof of anything. The floor helps, but it is not your discipline: the rootdir conftest redirects tempfile's base per run, removes it, and reports residue. CI's Backend Tests* jobs set KIROCREW_TMP_RESIDUE_STRICT=1, so a leak of yours turns that shard red; a local run only warns unless you set it. Need a data home? Use the conftest's per-test KIROCREW_HOME pin, not a private temp home: a background worker can re-create a home you removed. The same goes for a store root that its own reads mkdir (the Issue Radar store does): in a unittest class, set it from tmp_path in an autouse fixture method. (Details: testing-conventions.md § Rules.)

Running the suite from a Kiro Crew agent needs no setup. The agent's shell inherits the live KIROCREW_HOME, and the rootdir conftest sees KIROCREW_SCRATCH beside it and gives the run its own home under $TMPDIR (the agent's scratch, reclaimed by the gateway) instead of refusing. Do not make a home under /tmp/<name>, and do not fake HOME: a fake HOME makes uv, npm and pip rebuild their caches under it, 10k+ inodes per run, and nothing an agent may run can delete it afterwards. If a harness must fake HOME, keep the caches shared: XDG_CACHE_HOME=<real home>/.cache UV_CACHE_DIR=<real home>/.cache/uv npm_config_cache=<real home>/.npm on the commands the harness itself runs. The conftest keeps only its own bytecode and hypothesis caches on the real home.

Why it earns a guard: /tmp is often a tmpfs with a fixed inode budget (1,048,576 on the hosts this was measured on) and it returns ENOSPC to every other process on the machine while 90% of the bytes are free. It is not a tidiness issue, it is a "your shell stops working" issue.

1b. The operator's data home, and ~/.kiro, which is a DIFFERENT axis

KIROCREW_HOME is pinned per test at the rootdir; ~/.kiro is a DIFFERENT axis that pin does not cover. Read references/data-home.md before a test reads or writes either, or spawns a process that might.

1c. A child process inherits pytest's CWD, which is the repo root

This is the leak a reviewer cannot see: no line says open(..., "w"), the write happens in a grandchild, and the test can assert against tmp_path and pass while the artifact sits in the checkout. An empty file produced this way has been committed and shipped from this repo.

  • Pass cwd=<a directory under tmp_path> to any child that MAY create a file, and to

every helper that spawns one.

  • Scope the assertion to where that child's CWD actually is. An assertion over

tmp_path proves nothing about a child that ran somewhere else — and a security test whose payload escapes its own assertion is worse than no test, because it reports a guarantee it never checked.

  • A read-only query is exempt, and sometimes must be: git check-ignore against the

checkout is asking a question about the checkout.

The rootdir conftest fails the run on new non-ignored entries at the repository root, which is how this announces itself.

Two more things a spawning test owes, both learned from processes that outlived the run by six days, spinning at 1464% CPU between them:

  • `HOME` and `PATH` are a PAIR. If you hand a child a fabricated environment,

substituting one while inheriting the other is the defect. An inherited PATH on a developer host routinely leads with a version-manager shim directory (mise, asdf, pyenv, volta, nodenv), and a shim resolves its tool set from HOME — so with a substituted HOME the bare name python3 or node reaches the MANAGER, which finds no tool state and never execs anything. It spins, forever. Pin the real interpreter's own directory first on PATH, or resolve the tool to its real executable before building the env. Do not drop the HOME substitution instead: it is usually a blast-radius bound somebody chose on purpose. The shim also reads $XDG_CONFIG_HOME and the version files above the CWD, and the floor redirects $XDG_CONFIG_HOME for EVERY test, so a tmp_path CWD alone breaks it. node is handled once for the whole run (_put_the_real_node_first_on_path puts the directory process.execPath names first on PATH); another shimmed tool needs the same treatment: ask the tool where it lives, under the operator's environment, first.

  • Reap the process GROUP, on every exit path. A child is routinely a wrapper that

forks, so Popen.kill() reaps the wrapper and leaves the real work running — and a bounded wait() ending in a bare pass then reports success. Use start_new_session=True + os.killpg on POSIX and CREATE_NEW_PROCESS_GROUP + taskkill /T /F on Windows; test/installer_test_helpers.run_bounded is the reference. A try/finally around the whole post-spawn body, not just the timeout branch, is what makes "every exit path" true.

Two host traits a spawning test must not inherit. A suite started under nohup has SIGHUP ignored, and an ignore survives exec: spawn a child you will interrupt inside spawn_test_helpers.unblocked_signals(). And sleep can be a coreutils shebang script whose kernel-visible argv is the interpreter's: a test that reads a child's argv or name back spawns plain_sleeper_argv() instead.

1d. Background lifecycle — the one that beats every filesystem cleanup

A singleton with a daemon thread cannot be cleaned up by tidying files. The worked example is sel.py: SecurityEventLog is a process singleton, its writer is a daemon thread, and _init_locked binds the directory once from whatever _default_dir() resolved then. So the first test to call sel() fixes it for the whole worker, the thread keeps writing after that test ends, and _flush_batch opens with mkdir(parents=True, exist_ok=True) — re-creating the directory after tearDown deleted it. MEASURED: exactly one stray directory per run of the ops-mission-control suite. Full telling, with the stack it came from: testing-conventions.md § Rules.

The fix was not better cleanup. It was giving the singleton a session-scoped directory belonging to no individual test. When you touch a subsystem with a background worker, ask: which directory did its thread capture, and does anything delete that directory underneath it?

The same shape, one level up: a stub that replaces a `shutdown`/`close`/`stop` is not a stop. Three tests needed to observe that the metrics provider's shutdown was called, so they replaced it with a recorder — and the real shutdown is what stops OpenTelemetry's exporter thread. That thread then survived for the life of the worker, and because the OTel SDK reinstalls it in every fork child via os.register_at_fork, the sandbox's userns probe forked a MULTITHREADED child. unshare(CLONE_NEWUSER) implies CLONE_THREAD, which the kernel refuses with EINVAL unless the caller is single-threaded, and EINVAL is indistinguishable from "no CONFIG_USER_NS" — so the worker cached "this host has no sandbox backend" and every later sandboxed spawn failed closed. 19 red tests in two app suites, each passing alone, none of them a metrics test. Spy and delegate; never replace a lifecycle method. The rootdir conftest now fails the test that leaves an exporter thread running.

Two general lessons worth carrying out of that one: a thread whose target is a bound method keeps its own object alive, so dropping references never collects it; and anything registered with os.register_at_fork runs inside os.fork(), before it returns, so a fork child is not reliably single-threaded.

Related traps in the same family:

This is the start of the file. Read the full SKILL.md on GitHub.

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files.
  • Model reviewNot runA model review is scheduled for the next rescan.

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
5,059
Repository forks
1,091
7-day trend score
42

Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/kirodotdev/kirocrew/writing-tests/badge)](https://verified-skill.com/skills/kirodotdev/kirocrew/writing-tests)

Something wrong with this skill or its page? Tell us.