sbknext/forge-frappe-skill

frappe-testing

Write and run tests for Frappe/ERPNext apps — FrappeTestCase, test records, fixtures, mocking, permission tests, running with bench run-tests, and CI.

Quelltext ansehen

Verwendung

  1. Installationsbefehl kopieren und das Quellpaket vor der Installation in der eigenen Agentenumgebung prüfen.
  2. Benötigte Werkzeuge, Zugangsdaten und Hosts im Original prüfen.
  3. Eingabe und gewünschtes Ergebnis beschreiben und die Ausgabe mit den Quellen abgleichen.
npx skills add https://github.com/sbknext/forge-frappe-skill --skill frappe-testing

Vor dem Start

  • Die Installation umfasst weder Modellabo noch API-Kontingent oder Dateizugriffsrechte.
  • Sterne und Installationen zeigen Aktivität, keine unabhängige Qualitäts- oder Sicherheitsprüfung.
Originales Skill-Dokument

Aus dem Quell-Repository gerendert; Überschriften, Beispiele, Code, Tabellen, Links und Bilder bleiben erhalten.

Frappe Testing

Frappe ships a unittest-based harness with DB isolation. Tests run in a transaction that's rolled back, so they don't pollute the site.

Basic test

python
# myapp/myapp/doctype/widget/test_widget.py
import frappe
from frappe.tests.utils import FrappeTestCase

class TestWidget(FrappeTestCase):
    def test_creates_with_defaults(self):
        w = frappe.get_doc({"doctype": "Widget", "title": "A"}).insert()
        self.assertEqual(w.status, "Open")

    def test_validation_blocks_bad_qty(self):
        with self.assertRaises(frappe.ValidationError):
            frappe.get_doc({"doctype": "Widget", "title": "B", "qty": -1}).insert()

FrappeTestCase wraps each test in a savepoint and rolls back — no manual cleanup needed.

Test records (fixtures)

python
# test_records.json next to the doctype → auto-loaded as dependencies
[ { "doctype": "Widget", "title": "Seed", "qty": 1 } ]

Or build inline with a helper:

python
def make_widget(**kw):
    return frappe.get_doc({"doctype": "Widget", "title": "T", **kw}).insert()

Permission tests

python
def test_clerk_cannot_submit(self):
    frappe.set_user("clerk@example.com")
    self.assertFalse(frappe.has_permission("Widget", "submit"))
    frappe.set_user("Administrator")        # restore

Mocking external calls

python
from unittest.mock import patch

@patch("myapp.services.sync.requests.post")
def test_sync_calls_api(self, mock_post):
    mock_post.return_value.status_code = 200
    run_sync("W-0001")
    mock_post.assert_called_once()

Running

bash
bench --site <site> run-tests --app myapp
bench --site <site> run-tests --doctype "Widget"
bench --site <site> run-tests --module myapp.tests.test_sync
bench --site <site> run-tests --app myapp --coverage
bench --site <site> run-tests --skip-test-records --skip-before-tests   # fast iteration
bench --site <site> run-tests --junit-xml-output=/tmp/junit.xml         # CI output

Enable a test site: bench --site <site> set-config allow_tests true. Tests must run on a site whose name starts with test_ to prevent accidental data loss on production sites.

Test planning (manual + regression)

Before automating, define smoke (login + one submit flow) and critical-path cases in a structured format (preconditions → steps → expected). Cover permissions, hooks, API/import paths — not only Desk happy path. Template and checklists: skills/frappe/test-planner.md.

Rules

  • Use FrappeTestCase, not bare unittest.TestCase — you get DB rollback + helpers.
  • Never hit production data; bench run-tests uses the site's test mode.
  • Make tests independent — don't rely on records another test created (savepoints roll back).
  • For background jobs, call the method synchronously (is_async=False) or invoke the function directly.
  • Assert behaviour and side-effects (DB rows, status), not implementation details.

Source: https://docs.frappe.io/framework/user/en/guides/automated-testing/unit-testing

aus demselben Repository

Weitere Skills

Alle Skills
sbknext
Community

frappe-app-development

Scaffold, architect, and build production-grade custom Frappe/ERPNext apps. Covers app structure, DocType design, hooks.py configuration, background jobs, service layers, REST APIs, permissions, and testing. Use when creating a new Frappe app, adding a module, setting up scheduler events, implementing docevents, writing whitelisted APIs, or debugging Frappe-specific issues. Trigger on: "create Frappe app", "new DocType", "add a hook", "background job", "frappe.enqueue", "whitelist API", "custom

Installationen / 1h
1
GitHub Stars
18
Aktualisiert
7. Juni
sbknext
Community

frappe-client-scripting

Customize Frappe desk forms and lists with client scripts — frappe.ui.form.on handlers, field events, setquery link filters, setdfproperty, custom buttons, frappe.call, and list view settings. Use when adding form interactivity, dynamic filters, validation, or buttons in the desk UI. Trigger on: "client script", "frappe.ui.form.on", "form event", "setquery", "custom button", "frappe.call", "list view settings", "curfrm".

Installationen / 1h
1
GitHub Stars
18
Aktualisiert
7. Juni
sbknext
Community

frappe-database-orm

Query and mutate data in Frappe safely — getdoc/getall/getlist, frappe.db.getvalue/setvalue, parameterized frappe.db.sql, Query Builder (frappe.qb), transactions, bulk ops, and performance. Use when reading or writing data, optimizing slow queries, or avoiding SQL injection. Trigger on: "frappe.db.sql", "frappe.getall", "query builder", "frappe.qb", "bulk update", "N+1", "db transaction", "getvalue setvalue".

Installationen / 1h
1
GitHub Stars
18
Aktualisiert
7. Juni
sbknext
Community

frappe-report-development

Build Frappe reports — Query Reports (SQL), Script Reports (Python), filters, columns, charts, formatters, and report permissions. Use when creating analytics/listing reports, dashboards, or exportable views in Frappe/ERPNext. Trigger on: "query report", "script report", "Frappe report", "report filters", "report chart", "columns formatter", "report builder".

Installationen / 1h
1
GitHub Stars
18
Aktualisiert
7. Juni