Bench run-tests removes all custom fields

I’m coming from this thread - At times, the custom field is not getting created through fixtures. Why would it happen? - #4 by kirthi

The fields are created, but they are deleted when I run tests. I traced the issue to this before_tests hook.

def before_tests():
frappe.db.sql(“delete from tabCustom Field”)
frappe.db.sql(“delete from tabEvent”)
frappe.db.commit()
frappe.clear_cache()

Here, all the custom fields are deleted prior to test execution. This means that all our custom fields are deleted, and the tests fail. (For Eg. - I have a custom field added to BOM, and have a hook to check that field on submit of BOM. When I run tests, the custom fields are deleted, and the on_submit event calls my custom function to check for the custom field, and test execution fails since the field is already deleted.

Since I’m very new to run-tests, I dont know the purpose behind removing all custom fields.
Can the experts in running tests in ERPNext , help me out. My basic qn is -

  1. If all custom fields are removed before tests, how do I test my custom fields?

How do I go about resolving this issue, and run tests successfully that includes my custom fields also.

In your app or via custom test case sync custom fields using sync_fixtures()

Thanks @saurabh6790. I’ll try that. But, how do I make existing ERPNext test cases run successfully? I’ll need the custom fields to be synced before those test cases are run. Rather, I’m wondering why to delete the custom fields in the first place? Anyway, the hooks are called during tests which need custom fields. Why not leave the custom fields in there and run the tests? What is the rationale behind deleting the custom fields before tests? (I’m sure I’m missing something to understand)