Introduction
Welcome to this comprehensive guide for developers, focusing on automated testing in Empress. Empress is a powerful open-source framework that allows you to create business applications. This guide will guide you through the process of creating automated tests for your applications to ensure they function as expected, mirroring the clarity and sophistication of Apple’s guides.
Introduction
Automated testing in Empress is a crucial feature that aids in consistently maintaining the high quality of your applications. It ensures that every component of your software functions correctly and that the entire application works in harmony. As a developer, understanding and implementing this feature is fundamental to the design, development, and maintenance of applications.
How Automated Testing Works in Empress
To put it simply, automated testing in Empress is a set of rules that help you quickly create tests for your applications.
- The test can be anywhere in your repository, but it must begin with
test_
and should be a.py
file. - Tests must run on a site that starts with
test_
to prevent accidental data loss. - Test stubs are automatically generated for new DocTypes.
- The Empress test runner will automatically build test records for dependent DocTypes identified by the
Link
type field (Foreign Key) - Tests can be executed using
bench run-tests
- For non-DocType tests, you can write simple unittests and prefix your file names with
test_
.
Writing Tests for DocTypes
When writing tests for DocTypes, follow these rules:
- Test cases should be in a file named
test_[doctype].py
. - You must create all dependencies in the test file.
- Create a Python module structure to create fixtures/dependencies.
Here is an example of a test for the DocType Event
:
# Copyright (c) 2015, Empress Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
import frappe
import frappe.defaults
from frappe.tests.utils import EmpressTestCase
def create_events():
if frappe.flags.test_events_created:
return
frappe.set_user("Administrator")
doc = frappe.get_doc({
"doctype": "Event",
"subject":"_Test Event 1",
"starts_on": "2014-01-01",
"event_type": "Public"
}).insert()
# ... more code ...
class TestEvent(EmpressTestCase):
def setUp(self):
create_events()
# ... more code ...
In the example above, we first define a function create_events
that creates some test events. Then, we define a class TestEvent
that extends EmpressTestCase
and uses the create_events
function to set up some events for testing. This class also defines several tests for the Event
DocType.
Running Tests
To run your tests, navigate to your “frappe_bench” folder and execute bench run-tests
. Without options, all tests will be run. If you need more information about the test execution, use verbose log level for the bench with bench --verbose run-tests
.
You can specify options to run tests for a specific app, DocType, test, or module. The --profile
option runs a Python profiler on the test, and the --junit-xml-output
option provides test results in the standard XUnit XML format.
Here are some examples of running tests:
# Run tests for the 'erpnext' app
bench run-tests --app erpnext
# Run tests for the 'Activity Cost' DocType
bench run-tests --doctype "Activity Cost"
# Run a specific test case in 'User'
bench run-tests --doctype User --test test_get_value
# Run tests in a specific module
bench run-tests --module "erpnext.stock.doctype.stock_entry.test_stock_entry"
Conclusion
Automated testing in Empress is an indispensable feature that helps maintain the quality and reliability of your applications. By learning how to write and run tests, you will ensure that your applications function correctly and meet the needs of your users.