openaec-foundation/frappe_claude_skill_package/frappe-testing-unit
Claude Code and agent skill by openaec-foundation/frappe_claude_skill_package
- community
Checking that the SKILL.md file is still in the repository.
Use when writing unit tests, integration tests, creating test fixtures, or running tests with bench run-tests. Prevents flaky tests from missing fixtures, incorrect test isolation, and wrong test base classes. Covers frappe.tests.utils, IntegrationTestCase, UnitTestCase, test fixtures, bench run-tests flags, test naming conventions. Keywords: unit test, integration test, IntegrationTestCase, fixtures, bench run-tests, frappe.tests, test_*.py, how to write test, test fixtures, run tests, test fails, bench run-tests example..
Install
npx vskill@latest install openaec-foundation/frappe_claude_skill_package/frappe-testing-unitSKILL.md
The same SKILL.md was published earlier as openaec-foundation/frappe_claude_skill_package/frappe-testing-unit.
Unit & Integration Testing
Quick Reference
| Task | Command / Class | |
|---|---|---|
| Run all tests | bench --site test_site run-tests | |
| Run tests for app | bench --site test_site run-tests --app myapp | |
| Run tests for doctype | bench --site test_site run-tests --doctype "Sales Order" | |
| Run single test method | bench --site test_site run-tests --doctype "Sales Order" --test test_submit | |
| Run tests for module | bench --site test_site run-tests --module "myapp.mymodule.doctype.mydt.test_mydt" | |
| Run with profiler | bench --site test_site run-tests --doctype "Task" --profile | |
| Run with failfast | bench --site test_site run-tests --failfast | |
| Generate JUnit XML | bench --site test_site run-tests --junit-xml-output /path/report.xml | |
| Skip fixture loading | bench --site test_site run-tests --skip-test-records --skip-before-tests | |
| Base class (v14) | from frappe.tests.utils import FrappeTestCase | |
| Unit test class (v15+) | from frappe.tests.classes import UnitTestCase | |
| Integration test class (v15+) | from frappe.tests.classes import IntegrationTestCase |
Decision Tree: Which Test Base Class?
Version note: In v14, FrappeTestCase is the ONLY base class. In v15+, it still works (deprecated wrapper) but ALWAYS prefer UnitTestCase or IntegrationTestCase for new code.
Test Base Classes
FrappeTestCase (v14: still works in v15+ as compatibility wrapper)
Behavior: Resets frappe.local.flags after each test. Database transactions start before each test and rollback afterward. ALWAYS call super().setUpClass() if you override setUpClass.
UnitTestCase (v15+): No Database Access
Behavior: Sets frappe.set_user("Administrator") in setUpClass. Auto-detects doctype from module path. Provides normalize_html(), normalize_sql(), assertDocumentEqual(), assertQueryEqual(), assertSequenceSubset().
IntegrationTestCase (v15+): Full Database Access
Behavior: Extends UnitTestCase. Calls frappe.init() and sets up site connection. Loads test record dependencies via make_test_records(). Provides primary_connection() and secondary_connection() context managers. maxDiff = 10_000.
Test File Structure
ALWAYS place test files in the doctype directory following this naming convention:
Rules:
- ALWAYS prefix test files with
test_— the test runner ignores files without this prefix - ALWAYS use
test_{doctype_in_snake_case}.pyfor doctype tests - NEVER place test files outside the doctype directory for doctype-specific tests
- Non-doctype tests can live in any module, but MUST follow the
test_*.pynaming
Test Fixtures
Method 1: test_records.json (Static Fixtures)
Create a test_records.json file in the doctype directory:
Rules:
- ALWAYS prefix test data values with
_Testto distinguish from production data - The test runner auto-loads these before running tests for the doctype
- Link field dependencies are resolved automatically — the runner builds records for linked DocTypes first
Method 2: _test_records List (In-Module Fixtures)
Method 3: Programmatic Fixtures (Recommended for Complex Data)
ALWAYS use frappe.flags to prevent duplicate fixture creation across test methods.
Testing Patterns
Testing Document Lifecycle
Testing Permissions
Testing with User Context (v15+ Context Manager)
Testing Whitelisted Methods
Mocking External Services
Testing with Settings Changes
Testing with Hook Overrides
Context Managers Reference
| Context Manager | Available On | Purpose | |
|---|---|---|---|
set_user(user) | UnitTestCase, IntegrationTestCase | Temporarily switch user context | |
change_settings(dt, **kw) | UnitTestCase, IntegrationTestCase | Temporarily modify settings | |
patch_hooks(overrides) | UnitTestCase, IntegrationTestCase | Temporarily override hooks | |
freeze_time(time) | UnitTestCase, IntegrationTestCase | Freeze time for deterministic tests | |
debug_on(*exceptions) | UnitTestCase, IntegrationTestCase | Drop into debugger on exception | |
timeout(seconds) | Decorator | Fail test if it exceeds time limit | |
enable_safe_exec() | IntegrationTestCase | Enable server scripts temporarily | |
switch_site(site) | IntegrationTestCase | Switch to a different site | |
assertQueryCount(n) | IntegrationTestCase | Assert exact SQL query count | |
assertRedisCallCounts(**kw) | IntegrationTestCase | Assert Redis command counts | |
assertRowsRead(n) | IntegrationTestCase | Assert row-level DB access limits |
Database State Management
- IntegrationTestCase: ALWAYS rolls back database after each test — no cleanup needed
- UnitTestCase: No database connection — NEVER use
frappe.dbcalls - Each test gets a clean state: transactions start in
setUpand rollback intearDown - NEVER call
frappe.db.commit()in tests — this breaks test isolation - Use
frappe.flags.in_testto check if code is running under the test runner
Detecting Test Mode
NEVER use frappe.flags.in_test to skip validation logic — tests MUST exercise the same code paths as production.
Common Pitfalls
- NEVER forget `super().setUpClass()` — omitting this breaks fixture loading and user setup
- NEVER call `frappe.db.commit()` in tests — this persists data across tests and breaks isolation
- ALWAYS reset user in `tearDown` if you called
frappe.set_user()directly (v14 pattern) - ALWAYS prefix test data with `_Test` — makes cleanup and identification easy
- NEVER rely on test execution order — each test MUST be independent
- ALWAYS use `frappe.flags` to guard fixture creation — prevents duplicate inserts
See Also
- references/examples.md — Complete test examples
- references/anti-patterns.md — Common mistakes and fixes
- references/fixtures.md — Fixture patterns in depth
- references/api-reference.md — Full API reference for test utilities
- frappe-testing-cicd — CI/CD pipeline setup
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/source/testing/frappe-testing-unit/SKILL.md
Popularity
- Repository stars
- 91
- Repository forks
- 26
- 7-day trend score
- 23
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 30 Apr 2026, 7 months ago.
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
[](https://verified-skill.com/skills/openaec-foundation/frappe_claude_skill_package/frappe-testing-unit)