How to test app in frappe/erpnext

Hello. I’m a beginer in ERPnext.
I need to test an app in ERPnext. It’s have some addition fields on Employee table.
I’ve read this Not Found Unit Testing, and made a unit test.
But my fields are NULL in database.
Can you help me to solve this problem?
Sorry for my bad English writing, and thanks a lot.

1 Like

You should add the relevant Employees in your tests

(Fixed old bench commands on that page)

1 Like

Hi rmehta, in my test_records.json, I added my custom fields and values. but when i run bench run-tests --doctype “employee” then check db, they are still null.
Can you tell me how to fix this?
Thank you so much.

test_records.json will import only once.

Btw, our new thinking is to use test_records.json only static fixtures. Best to create all documents in the test itself.

Hi Rmehta.
Can you give me an example?

Can you share your test case, it will be easier to suggest then.

this is my function to insert a new Employee.

test_records = frappe.get_test_records(‘Employee’)

class TestEmployee(unittest.TestCase):

def create_test_employee(self):
    employees = []
    index = 0
    for record in test_records:
        emp = frappe.get_doc(record)
        emp.insert()
        tmp = frappe.get_last_doc("Employee")
        tmp["residency_status"]=record.get("residency_status")
        tmp.save()
        employees.append({
            'name': record.get("employee_name")
            })
    return employees

And this is my test records:

[
{
“company”: “_Test Company”,
“date_of_birth”: “1980-01-01”,
“date_of_joining”: “2010-01-01”,
“department”: “_Test Department”,
“doctype”: “Employee”,
“employee_name”: “_Test Employee”,
“gender”: “Female”,
“naming_series”: “_T-Employee-”,
“status”: “Active”,
“user_id”: “test@example.com”,
“residency_status”:“Singaporean”
},
{
“company”: “_Test Company”,
“date_of_birth”: “1980-01-05”,
“date_of_joining”: “2010-01-01”,
“department”: “_Test Department 1”,
“doctype”: “Employee”,
“employee_name”: “_Test Employee 1”,
“gender”: “Male”,
“naming_series”: “_T-Employee-”,
“status”: “Active”,
“user_id”: “test1@example.com”,
“residency_status”:“Permanent Resident”,
“date_of_obtaining_pr”:“2012-09-01”,
“pr_cpf_contribution_scheme”:“Graduated Employer & Employee (G/G)”
},
{
“company”: “_Test Company”,
“date_of_birth”: “1980-01-10”,
“date_of_joining”: “2010-01-01”,
“department”: “_Test Department 1”,
“doctype”: “Employee”,
“employee_name”: “_Test Employee 2”,
“gender”: “Male”,
“naming_series”: “_T-Employee-”,
“status”: “Active”,
“user_id”: “test2@example.com”,
“residency_status”:“Singaporean”
},
{
“company”: “_Test Company”,
“date_of_birth”: “1980-01-01”,
“date_of_joining”: “2010-01-01”,
“department”: “_Test Department 1”,
“doctype”: “Employee”,
“employee_name”: “_Test Employee 3”,
“gender”: “Male”,
“naming_series”: “_T-Employee-”,
“status”: “Active”,
“user_id”: “test2@example.com”,
“residency_status”:“Permanent Resident”,
“date_of_obtaining_pr”:“2012-09-01”,
“pr_cpf_contribution_scheme”:“Graduated Employer & Employee (G/G)”
},
{
“company”: “_Test Company”,
“date_of_birth”: “1980-01-01”,
“date_of_joining”: “2010-01-01”,
“department”: “_Test Department 1”,
“doctype”: “Employee”,
“employee_name”: “_Test Employee 4”,
“gender”: “Male”,
“naming_series”: “_T-Employee-”,
“status”: “Active”,
“user_id”: “test2@example.com”,
“residency_status”:“Permanent Resident”,
“date_of_obtaining_pr”:“2012-11-01”,
“pr_cpf_contribution_scheme”:“Graduated Employer & Employee (G/G)”
}
]

In my db, some fields are null: residency_status, date_of_obtaining_pr, pr_cpf_contribution_schema.

I tried to update records in db but they’re still NULL.

@Npvinhloc Firstly its not a good idea to change the Employee tests

If you want to test your custom fields, add them via the script.

def create_test_employee(self):
    employees = []
    index = 0
    for record in test_records:
        emp = frappe.get_doc(record)
        emp.residencey_status = "Permanent"
        emp.insert()

@rmehta they are still NULL.
And if I need to test my app then where should I put my test file and test value?
In erpnext original doctype folder or my app folder?
Thank you.

Create your record by code. Don’t use test_records.json

1 Like