Page 1 of 1

Unit Test

Posted: Mon Jul 13, 2020 10:39 am
by Mohammedaln
[*]Content ::
1. test_records.json
2. validate exception
3. unit test parts
4. Commends in bench frappe commend line

***********************************************************************************************************
1. test_records.json
-----------------
demo dictionary for inserting data to test codes
- It has standard way of naming data :: like _Test Doctype Name"
Ex:

Code: Select all

[
 {
  "activity_type": "_Test Activity Type"
 }, 
 {
  "activity_type": "_Test Activity Type 1"
 }, 
 {
  "activity_type": "_Test Activity Type 2"
 }
]

# in test_...py

# read the file test_records.json
test_records = frappe.get_test_records('Activity Type')
#for reading data from test_records.json 
---------
#call it from other file::
from erpnext.setup.doctype.currency_exchange.test_currency_exchange import test_records as \
			currency_exchange_records
			
			

***********************************************************************************************************
2. validate exception
-----------------------

for important code in validate or on_update events to check if it is implementing well by ...

Ex :

Code: Select all

#.py

class DuplicationError(frappe.ValidationError): pass

frappe.throw(_("Activity Cost exists for Employee {0} against Activity Type - {1}")
						.format(self.employee, self.activity_type), DuplicationError)

#test.py
from erpnext.projects.doctype.activity_cost.activity_cost import DuplicationError

self.assertRaises(DuplicationError, activity_cost2.insert )

from erpnext.projects.doctype.task.task import CircularReferenceError
from erpnext.projects.doctype.timesheet.timesheet import OverlapError



***********************************************************************************************************
3. unit test parts
------------

1. Setup
2. Execute
3. Check
-------------------------
1. Setup ::
to clear if there are any thing might course problem in execution
or adding data for execution

Ex ::

Code: Select all

Setup

frappe.db.sql("delete from `tabActivity Cost`")
self.assertRaises(DuplicationError, activity_cost2.insert )
frappe.db.sql("delete from `tabActivity Cost`")


def setUp(self):
		frappe.db.sql(''' delete from `tabCompensatory Leave Request`''')
		frappe.db.sql(''' delete from `tabLeave Ledger Entry`''')
		frappe.db.sql(''' delete from `tabLeave Allocation`''')
		frappe.db.sql(''' delete from `tabAttendance` where attendance_date in {0} '''.format((today(), add_days(today(), -1)))) #nosec
		create_leave_period(add_months(today(), -3), add_months(today(), 3), "_Test Company")
		create_holiday_list()



===========================================================
2. Execute::

write the code for testing production code

=========================================================
3. Check:

check if the code execute successfully and get the target output or result

Ex::

Code: Select all

self.assertEqual(task1.subject, 'Task 1')

self.assertRaises(DuplicationError, activity_cost2.insert )
==============================================================
4. Commends ::

Code: Select all


bench --verbose --site erp-test set-config allow_tests true

bench --verbose --site erp-test run-tests --app erpnext --doctype 'Holiday List' 

bench --verbose --site erp-test run-tests --app project_app --module projects
bench --verbose --site erp-test run-tests --module "erpnext.stock.doctype.stock_entry.test_stock_entry"

bench --verbose --site erp-test run-tests --app erpnext


# Run a specific case in User:
bench run-tests --doctype User --test test_get_value