What is the significant of the before_test method?

def before_tests():
frappe.clear_cache()
# complete setup if missing
from frappe.desk.page.setup_wizard.setup_wizard import setup_complete
if not frappe.get_list("Company"):
	setup_complete({
		"currency"			:"USD",
		"full_name"			:"Test User",
		"company_name"		:"Wind Power LLC",
		"timezone"			:"America/New_York",
		"company_abbr"		:"WP",
		"industry"			:"Manufacturing",
		"country"			:"United States",
		"fy_start_date"		:"2011-01-01",
		"fy_end_date"		:"2011-12-31",
		"language"			:"english",
		"company_tagline"	:"Testing",
		"email"				:"test@erpnext.com",
		"password"			:"test",
		"chart_of_accounts" : "Standard",
		"domains"			: ["Manufacturing"],
	})

frappe.db.sql("delete from `tabLeave Allocation`")
frappe.db.sql("delete from `tabLeave Application`")
frappe.db.sql("delete from `tabSalary Slip`")
frappe.db.sql("delete from `tabItem Price`")

frappe.db.set_value("Stock Settings", None, "auto_insert_price_list_rate_if_missing", 0)
enable_all_roles_and_domains()

frappe.db.commit()

Hi guys, When i ran my test file, all of a sudden my data gone missing. i found out that running the test in frappe is quite dangerous especially if the test is related to those tables. I am just curious, if any significant to the code above? what is the purpose. can we make it default not to touch any of the tables unless we do it intentionally?

i realized that there a way that we can skip the before test method by specifying --skip-before-tests, but it is not default.

It is used to create a test environment that has just the right amount of data to run the tests. Its best if you create a separate test instance ( a separate site just for tests). Running tests on your active site will mess up your data.
The tests scripts create data anyway, which are to be used in your tests. These tests should run on any instance irrespective, this requires uniform data. Hence the before_tests method

1 Like

Thank you @marination for the explanation. I never thought of that. I will try to separate the test using site option.