Skip to content
Generate TDD Tests logo

Generate TDD Tests

Enforce the Iron Law of TDD for Frappe apps. Red-Green-Refactor cycle for DocTypes and Controllers.

SKILL.md

Full skill instructions

Generate TDD Tests (Iron Law)

"Write the test first. Watch it fail. Write minimal code to pass."

The Iron Law

NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST.

If you write code before the test: Delete it. Start over. This is non-negotiable.

Red-Green-Refactor Cycle

1. RED: Write Failing Test

Write one minimal test showing what should happen. Use FrappeTestCase.

import frappe
from frappe.tests.utils import FrappeTestCase

class TestSalesOrder(FrappeTestCase):
    def test_status_must_be_draft_on_creation(self):
        # We wish for this behavior
        doc = frappe.get_doc({"doctype": "Sales Order", "customer": "Test Customer"})
        self.assertEqual(doc.status, "Draft")

Verify RED: Run bench --site [site] run-tests --doctype "Sales Order".

  • Must Fail: If it passes, you are testing existing behavior. Fix the test.
  • Expected Failure: Confirm it fails because doc.status is NOT "Draft".

2. GREEN: Minimal Code

Write the simplest code in the controller (sales_order.py) to pass the test.

class SalesOrder(Document):
    def before_insert(self):
        self.status = "Draft"

Verify GREEN: Run the test again. It must pass.

3. REFACTOR: Clean Up

Clean up duplication, improve names, or extract helpers while keeping the test Green.

Frappe Test Patterns

Validation Tests

def test_customer_is_mandatory(self):
    doc = frappe.get_doc({"doctype": "Sales Order", "customer": None})
    self.assertRaises(frappe.ValidationError, doc.insert)

Permission Tests

def test_regular_user_cannot_submit(self):
    frappe.set_user("[email protected]")
    doc = frappe.get_doc("Sales Order", "SO-001")
    self.assertRaises(frappe.PermissionError, doc.submit)

Mocking (Use only if unavoidable)

Prefer real data using frappe.get_doc. If you must mock external APIs:

from unittest.mock import patch

@patch('frappe.make_get_request')
def test_api_integration(self, mock_get):
    mock_get.return_value = {"status": "success"}
    # ... call your method ...

Anti-Patterns

AvoidWhyInstead
Test AfterFails to prove the test actually works.Test First.
Manual CleanupError-prone and slow.Use frappe.db.rollback() in tearDown.
Huge SetupsMakes tests hard to read.Use frappe.get_test_records().
Mocking FrappeMocks don't test the DB state.Use the real frappe.db in FrappeTestCase.

Checklist

  • Every new function has a test.
  • Watched each test fail for the right reason.
  • Minimal code written to pass.
  • tearDown performs rollback or cleanup.
  • Edge cases (None, Empty, Invalid) covered.

More Sales & marketing skills

revops logo
Sales & marketing

revops

Design and optimize systems connecting marketing, sales, and customer success into a unified revenue engine.

53.3K 104.9K
View
lead-magnets logo
Sales & marketing

lead-magnets

Strategic planning and optimization for email capture offers and gated content.

53.3K 103.4K
View
directory-submissions logo
Sales & marketing

directory-submissions

Directory submissions: the foundation layer of distribution that passes backlinks and discovery surface area into your marketing pages.

53.3K 76.3K
View
gsap-timeline logo
Sales & marketing

gsap-timeline

Sequence and choreograph multi-step animations with precise timing control and nested timelines.

15.9K 58.4K
View
launch-strategy logo
Sales & marketing

launch-strategy

Structured framework for planning product launches, feature announcements, and go-to-market strategies.

53.3K 56.8K
View
gws-drive-upload logo
Sales & marketing

gws-drive-upload

Upload files to Google Drive with automatic MIME type detection and metadata.

31.2K 47.2K
View
ci-cd-and-automation logo
Sales & marketing

ci-cd-and-automation

Automates CI/CD pipeline setup. Use when setting up or modifying build and deployment pipelines. Use when you need to automate quality gates, configure test runners in CI, or establish deployment strategies.

101.4K 43.2K
View
persona-team-lead logo
Sales & marketing

persona-team-lead

Team leadership persona with standup coordination, meeting prep, task delegation, and team communication.

31.2K 29.3K
View
recipe-find-large-files logo
Sales & marketing

recipe-find-large-files

Identify large Google Drive files consuming storage quota.

31.2K 29.2K
View

Sales & marketing AI tools

KreadoAI logo
Sales & marketing

KreadoAI

Revolutionize Video Creation with AI-Generated Avatars and Voices

Paid
View
Followr logo
Sales & marketing

Followr

Social media on autopilot

Paid
View
Logodiffusion logo
Sales & marketing

Logodiffusion

Design custom logos instantly with AI.

Paid
View
I
Sales & marketing

Ideogram

Text-heavy marketing and design images

Freemium
View
Prompt Silo logo
Sales & marketing

Prompt Silo

Boost Your SEO with Content and SEO Silos

Free
View
Marketing Blocks logo
Sales & marketing

Marketing Blocks

Revolutionize Your Marketing with AI-Powered MarketingBlocks!

Paid
View
Scrip AI logo
Sales & marketing

Scrip AI

10X faster & free AI content creation for 100+ use cases

Free
View
T
Sales & marketing

Tribescaler

Tribescaler | Make Your Tweets Go Viral

Paid
View