[question] for python test case : initial steps


I wish to write python test case for my feature. I am looking for basic initial environment steps.
Ex. I looked at existing test cases they use test data ex Test Customer so (a)how to have this test data attached when i run the test. (b) are there any pre requisite to run test case (c) any useful links for writing test cases.


Hi jignesh_shah,

One option to bootstrap your learning is to find an existing test that is close to what you wish to test. That will point the way for test setup ideas too - no sense reinventing the wheel.

To view example tests you can inspect and run, open a console on your ERPNext instance and run this on the command line:

frappe@erpnext:~/frappe-bench$ find . -name ‘test_*.py’

Here’s a primer on how to run the tests Unit Testing

How does one attach test data and run test case.??..There are demo test data which test cases are using . Thanks

@nabinhait, during your demo at conference for employee advance feature …You had shown how to use test data and run it. Can you please give that step? It will help thanks

Have a look at the test_records.json file in the doctype folder for inspiration. The test runner uses it to create records. You can also manually create using frappe.get_doc. Another option is to make use of the record that have already been created.


@tundebabzy thanks…When i run existing test case with test customer and test product…Such data is not in my site…So test case fails…My question is how to load test data?
“Another option is to make use of the record that have already been created.”— by default this data is not present in db

Have you added it to test_records.json?

On running bench run-tests all test data gets created on default site.
The confusion happens as automated link mentions “Tests must run on a site that starts with test_. This is to prevent accidental loss of data” → which is not mandatory.
Thanks @tundebabzy and @clarkej for your time and help.