write-python-unit-tests
Skill663 starsChanged 6 days agoApache-2.0View on GitHub
Write Python unit tests for Python code. Use when: creating new tests, improving test coverage, testing functions or classes in isolation, writing test cases for edge cases, or following TDD practices.
In this repository7 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- change-keywordSkill
- ci-failure-triageSkill
- write-node-unit-testsSkill
- parse-rf-resultsSkill
- write-robot-testsSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 28
Use assert statements to check the expected outcomes of the code being tested.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 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
Write Python Unit Tests
Python unit tests must always be written in the utest folder and using pytest.
Unit tests must be readable and maintainable. They should test one thing at a time, and be easy to understand. Use descriptive names for test functions and variables.
Linting must be done by calling inv lint-python --fix to ensure consistent formatting and style.
Example
@pytest.fixture(scope="module")
def cookie():
return Cookie(None)
def test_cookie_as_dot_dict_expiry(cookie: Cookie):
epoch = 1604698517
data = cookie._cookie_as_dot_dict({"expires": epoch})
assert data.expires == datetime.fromtimestamp(epoch, tz=timezone.utc)Assertions
Use assert statements to check the expected outcomes of the code being tested. If there need to
assert longer data structures, use approval tests. When test are run, ask user to look the
approval test file and approve it if the data structure is correct. If the data structure is incorrect, ask user to update the approval test file with the correct data structure and re-run the tests.
Running unit tests
Unit tests can be run with invoke:
inv utestThing to avoid
Because must test are readable, avoid comments or docstrings that explain what the test is doing. The test should be self-explanatory. If you find yourself needing to explain what the test is doing, consider refactoring the test to make it more readable. If there are complex data structures, consider using helper functions or fixtures to set up the test data in a more readable way or adding comments on the key point of the data structure.
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.

