You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

343 regels
9.5 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals, print_function
  4. import frappe
  5. import unittest, json, sys
  6. import xmlrunner
  7. import importlib
  8. from frappe.modules import load_doctype_module, get_module_name
  9. from frappe.utils import cstr
  10. import frappe.utils.scheduler
  11. import cProfile, StringIO, pstats
  12. unittest_runner = unittest.TextTestRunner
  13. def xmlrunner_wrapper(output):
  14. """Convenience wrapper to keep method signature unchanged for XMLTestRunner and TextTestRunner"""
  15. def _runner(*args, **kwargs):
  16. kwargs['output'] = output
  17. return xmlrunner.XMLTestRunner(*args, **kwargs)
  18. return _runner
  19. def main(app=None, module=None, doctype=None, verbose=False, tests=(),
  20. force=False, profile=False, junit_xml_output=None, ui_tests=False):
  21. global unittest_runner
  22. xmloutput_fh = None
  23. if junit_xml_output:
  24. xmloutput_fh = open(junit_xml_output, 'w')
  25. unittest_runner = xmlrunner_wrapper(xmloutput_fh)
  26. else:
  27. unittest_runner = unittest.TextTestRunner
  28. try:
  29. frappe.flags.print_messages = verbose
  30. frappe.flags.in_test = True
  31. if not frappe.db:
  32. frappe.connect()
  33. # if not frappe.conf.get("db_name").startswith("test_"):
  34. # raise Exception, 'db_name must start with "test_"'
  35. # workaround! since there is no separate test db
  36. frappe.clear_cache()
  37. frappe.utils.scheduler.disable_scheduler()
  38. set_test_email_config()
  39. if verbose:
  40. print('Running "before_tests" hooks')
  41. for fn in frappe.get_hooks("before_tests", app_name=app):
  42. frappe.get_attr(fn)()
  43. if doctype:
  44. ret = run_tests_for_doctype(doctype, verbose, tests, force, profile)
  45. elif module:
  46. ret = run_tests_for_module(module, verbose, tests, profile)
  47. else:
  48. ret = run_all_tests(app, verbose, profile, ui_tests)
  49. frappe.db.commit()
  50. # workaround! since there is no separate test db
  51. frappe.clear_cache()
  52. return ret
  53. finally:
  54. if xmloutput_fh:
  55. xmloutput_fh.flush()
  56. xmloutput_fh.close()
  57. def set_test_email_config():
  58. frappe.conf.update({
  59. "auto_email_id": "test@example.com",
  60. "mail_server": "smtp.example.com",
  61. "mail_login": "test@example.com",
  62. "mail_password": "test",
  63. "admin_password": "admin"
  64. })
  65. def run_all_tests(app=None, verbose=False, profile=False, ui_tests=False):
  66. import os
  67. apps = [app] if app else frappe.get_installed_apps()
  68. test_suite = unittest.TestSuite()
  69. for app in apps:
  70. for path, folders, files in os.walk(frappe.get_pymodule_path(app)):
  71. for dontwalk in ('locals', '.git', 'public'):
  72. if dontwalk in folders:
  73. folders.remove(dontwalk)
  74. # print path
  75. for filename in files:
  76. filename = cstr(filename)
  77. if filename.startswith("test_") and filename.endswith(".py")\
  78. and filename != 'test_runner.py':
  79. # print filename[:-3]
  80. _add_test(app, path, filename, verbose,
  81. test_suite, ui_tests)
  82. if profile:
  83. pr = cProfile.Profile()
  84. pr.enable()
  85. out = unittest_runner(verbosity=1+(verbose and 1 or 0)).run(test_suite)
  86. if profile:
  87. pr.disable()
  88. s = StringIO.StringIO()
  89. ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
  90. ps.print_stats()
  91. print(s.getvalue())
  92. return out
  93. def run_tests_for_doctype(doctype, verbose=False, tests=(), force=False, profile=False):
  94. module = frappe.db.get_value("DocType", doctype, "module")
  95. if not module:
  96. print('Invalid doctype {0}'.format(doctype))
  97. sys.exit(1)
  98. test_module = get_module_name(doctype, module, "test_")
  99. if force:
  100. for name in frappe.db.sql_list("select name from `tab%s`" % doctype):
  101. frappe.delete_doc(doctype, name, force=True)
  102. make_test_records(doctype, verbose=verbose, force=force)
  103. module = importlib.import_module(test_module)
  104. return _run_unittest(module, verbose=verbose, tests=tests, profile=profile)
  105. def run_tests_for_module(module, verbose=False, tests=(), profile=False):
  106. module = importlib.import_module(module)
  107. if hasattr(module, "test_dependencies"):
  108. for doctype in module.test_dependencies:
  109. make_test_records(doctype, verbose=verbose)
  110. return _run_unittest(module=module, verbose=verbose, tests=tests, profile=profile)
  111. def run_ui_tests(app=None, test=None, verbose=False, profile=False):
  112. '''Run a single unit test for UI using test_test_runner'''
  113. module = importlib.import_module('frappe.tests.ui.test_test_runner')
  114. frappe.flags.ui_test_app = app
  115. frappe.flags.ui_test_path = test
  116. return _run_unittest(module=module, verbose=verbose, tests=(), profile=profile)
  117. def _run_unittest(module, verbose=False, tests=(), profile=False):
  118. test_suite = unittest.TestSuite()
  119. module_test_cases = unittest.TestLoader().loadTestsFromModule(module)
  120. if tests:
  121. for each in module_test_cases:
  122. for test_case in each.__dict__["_tests"]:
  123. if test_case.__dict__["_testMethodName"] in tests:
  124. test_suite.addTest(test_case)
  125. else:
  126. test_suite.addTest(module_test_cases)
  127. if profile:
  128. pr = cProfile.Profile()
  129. pr.enable()
  130. frappe.flags.tests_verbose = verbose
  131. out = unittest_runner(verbosity=1+(verbose and 1 or 0)).run(test_suite)
  132. if profile:
  133. pr.disable()
  134. s = StringIO.StringIO()
  135. ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
  136. ps.print_stats()
  137. print(s.getvalue())
  138. return out
  139. def _add_test(app, path, filename, verbose, test_suite=None, ui_tests=False):
  140. import os
  141. if os.path.sep.join(["doctype", "doctype", "boilerplate"]) in path:
  142. # in /doctype/doctype/boilerplate/
  143. return
  144. app_path = frappe.get_pymodule_path(app)
  145. relative_path = os.path.relpath(path, app_path)
  146. if relative_path=='.':
  147. module_name = app
  148. else:
  149. module_name = '{app}.{relative_path}.{module_name}'.format(app=app,
  150. relative_path=relative_path.replace('/', '.'), module_name=filename[:-3])
  151. module = importlib.import_module(module_name)
  152. is_ui_test = True if hasattr(module, 'TestDriver') else False
  153. if is_ui_test != ui_tests:
  154. return
  155. if not test_suite:
  156. test_suite = unittest.TestSuite()
  157. if os.path.basename(os.path.dirname(path))=="doctype":
  158. txt_file = os.path.join(path, filename[5:].replace(".py", ".json"))
  159. with open(txt_file, 'r') as f:
  160. doc = json.loads(f.read())
  161. doctype = doc["name"]
  162. make_test_records(doctype, verbose)
  163. test_suite.addTest(unittest.TestLoader().loadTestsFromModule(module))
  164. def make_test_records(doctype, verbose=0, force=False):
  165. if not frappe.db:
  166. frappe.connect()
  167. for options in get_dependencies(doctype):
  168. if options == "[Select]":
  169. continue
  170. if not options in frappe.local.test_objects:
  171. if options in frappe.local.test_objects:
  172. print("No test records or circular reference for {0}".format(options))
  173. frappe.local.test_objects[options] = []
  174. make_test_records(options, verbose, force)
  175. make_test_records_for_doctype(options, verbose, force)
  176. def get_modules(doctype):
  177. module = frappe.db.get_value("DocType", doctype, "module")
  178. try:
  179. test_module = load_doctype_module(doctype, module, "test_")
  180. if test_module:
  181. reload(test_module)
  182. except ImportError:
  183. test_module = None
  184. return module, test_module
  185. def get_dependencies(doctype):
  186. module, test_module = get_modules(doctype)
  187. meta = frappe.get_meta(doctype)
  188. link_fields = meta.get_link_fields()
  189. for df in meta.get_table_fields():
  190. link_fields.extend(frappe.get_meta(df.options).get_link_fields())
  191. options_list = [df.options for df in link_fields] + [doctype]
  192. if hasattr(test_module, "test_dependencies"):
  193. options_list += test_module.test_dependencies
  194. options_list = list(set(options_list))
  195. if hasattr(test_module, "test_ignore"):
  196. for doctype_name in test_module.test_ignore:
  197. if doctype_name in options_list:
  198. options_list.remove(doctype_name)
  199. return options_list
  200. def make_test_records_for_doctype(doctype, verbose=0, force=False):
  201. module, test_module = get_modules(doctype)
  202. if verbose:
  203. print("Making for " + doctype)
  204. if hasattr(test_module, "_make_test_records"):
  205. frappe.local.test_objects[doctype] += test_module._make_test_records(verbose)
  206. elif hasattr(test_module, "test_records"):
  207. frappe.local.test_objects[doctype] += make_test_objects(doctype, test_module.test_records, verbose, force)
  208. else:
  209. test_records = frappe.get_test_records(doctype)
  210. if test_records:
  211. frappe.local.test_objects[doctype] += make_test_objects(doctype, test_records, verbose, force)
  212. elif verbose:
  213. print_mandatory_fields(doctype)
  214. def make_test_objects(doctype, test_records=None, verbose=None, reset=False):
  215. '''Make test objects from given list of `test_records` or from `test_records.json`'''
  216. records = []
  217. if test_records is None:
  218. test_records = frappe.get_test_records(doctype)
  219. for doc in test_records:
  220. if not doc.get("doctype"):
  221. doc["doctype"] = doctype
  222. d = frappe.copy_doc(doc)
  223. if doc.get('name'):
  224. d.name = doc.get('name')
  225. if frappe.local.test_objects.get(d.doctype) and not reset:
  226. # do not create test records, if already exists
  227. return []
  228. if d.meta.get_field("naming_series"):
  229. if not d.naming_series:
  230. d.naming_series = "_T-" + d.doctype + "-"
  231. # submit if docstatus is set to 1 for test record
  232. docstatus = d.docstatus
  233. d.docstatus = 0
  234. try:
  235. d.run_method("before_test_insert")
  236. d.insert()
  237. if docstatus == 1:
  238. d.submit()
  239. except frappe.NameError:
  240. pass
  241. except Exception as e:
  242. if d.flags.ignore_these_exceptions_in_test and e.__class__ in d.flags.ignore_these_exceptions_in_test:
  243. pass
  244. else:
  245. raise
  246. records.append(d.name)
  247. frappe.db.commit()
  248. return records
  249. def print_mandatory_fields(doctype):
  250. print("Please setup make_test_records for: " + doctype)
  251. print("-" * 60)
  252. meta = frappe.get_meta(doctype)
  253. print("Autoname: " + (meta.autoname or ""))
  254. print("Mandatory Fields: ")
  255. for d in meta.get("fields", {"reqd":1}):
  256. print(d.parent + ":" + d.fieldname + " | " + d.fieldtype + " | " + (d.options or ""))
  257. print()