# Testing ## File location Tests live alongside the code they test: ``` apps////doctype//test_.py ``` For feature-wise tests, place in the tests directory: ``` apps///tests/test_.py ``` ## Writing tests ```python import frappe from frappe.tests import IntegrationTestCase class TestExpense(IntegrationTestCase): def test_expense_creation(self): doc = frappe.get_doc(doctype="Expense", title="Test", amount=100) doc.insert() self.assertEqual(doc.amount, 100) def test_validation(self): doc = frappe.get_doc(doctype="Expense", title="Test", amount=-1) self.assertRaises(frappe.ValidationError, doc.insert) ``` Key patterns: - Inherit from `frappe.tests.IntegrationTestCase` (not `unittest.TestCase`) - Tests run inside a transaction that rolls back — no manual cleanup needed ## Unit tests (no database) For pure logic that doesn't need Frappe context or database: ```python from frappe.tests import UnitTestCase class TestExpenseUtils(UnitTestCase): def test_calculate_tax(self): self.assertEqual(calculate_tax(100, 0.1), 10) ``` `UnitTestCase` is faster — no DB setup/teardown. Use for utility functions, calculations, parsing logic. ## Test fixtures For test data that multiple tests need, create `test_records` or use `setUp`: ```python class TestExpense(IntegrationTestCase): def setUp(self): self.category = frappe.get_doc(doctype="Expense Category", category_name="Travel").insert() ``` ## Test site Run tests on a **separate site** from the one the user is actively working on. Tests create, modify, and delete data — running them on the development site will pollute it. Convention: if the dev site is `expense.localhost`, create `expense-test.localhost` for tests: ```bash bench new-site expense-test.localhost --admin-password admin bench --site expense-test.localhost install-app ``` Always run tests against the test site: ```bash bench --site expense-test.localhost run-tests --app ``` ## Running tests ```bash # All tests for an app bench --site run-tests --app # Specific DocType bench --site run-tests --doctype "Expense" # Specific test file bench --site run-tests --module ..doctype..test_ # Specific test method bench --site run-tests --module ..doctype..test_ --test test_expense_creation ``` ## Common pitfalls - If tests fail with "DocType not found", run `bench --site migrate` first. - Test files must be named `test_*.py` to be discovered.