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.

test_runner.py 5.5 KiB

12 years ago
12 years ago
11 years ago
12 years ago
11 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import webnotes
  5. import unittest, sys
  6. from webnotes.model.meta import has_field
  7. from webnotes.model.code import load_doctype_module, get_module_name
  8. from webnotes.model.doctype import get_link_fields
  9. from webnotes.utils import cstr
  10. def main(app=None, module=None, doctype=None, verbose=False):
  11. webnotes.flags.print_messages = verbose
  12. webnotes.flags.in_test = True
  13. if not webnotes.conn:
  14. webnotes.connect()
  15. if doctype:
  16. ret = run_unittest(doctype, verbose=verbose)
  17. elif module:
  18. import importlib
  19. test_suite = unittest.TestSuite()
  20. module = importlib.import_module(module)
  21. if hasattr(module, "test_dependencies"):
  22. for doctype in module.test_dependencies:
  23. make_test_records(doctype, verbose=verbose)
  24. test_suite.addTest(unittest.TestLoader().loadTestsFromModule(module))
  25. ret = unittest.TextTestRunner(verbosity=1+(verbose and 1 or 0)).run(test_suite)
  26. else:
  27. ret = run_all_tests(app, verbose)
  28. return ret
  29. def run_all_tests(app=None, verbose=False):
  30. import os
  31. apps = [app] if app else webnotes.get_installed_apps()
  32. test_suite = unittest.TestSuite()
  33. for app in apps:
  34. for path, folders, files in os.walk(webnotes.get_pymodule_path(app)):
  35. for dontwalk in ('locals', '.git', 'public'):
  36. if dontwalk in folders:
  37. folders.remove(dontwalk)
  38. # print path
  39. for filename in files:
  40. filename = cstr(filename)
  41. if filename.startswith("test_") and filename.endswith(".py"):
  42. # print filename[:-3]
  43. _run_test(path, filename, verbose, test_suite=test_suite, run=False)
  44. return unittest.TextTestRunner(verbosity=1+(verbose and 1 or 0)).run(test_suite)
  45. def _run_test(path, filename, verbose, test_suite=None, run=True):
  46. import os, imp
  47. from webnotes.modules.utils import peval_doclist
  48. if not test_suite:
  49. test_suite = unittest.TestSuite()
  50. if os.path.basename(os.path.dirname(path))=="doctype":
  51. txt_file = os.path.join(path, filename[5:].replace(".py", ".txt"))
  52. with open(txt_file, 'r') as f:
  53. doctype_doclist = peval_doclist(f.read())
  54. doctype = doctype_doclist[0]["name"]
  55. make_test_records(doctype, verbose)
  56. module = imp.load_source(filename[:-3], os.path.join(path, filename))
  57. test_suite.addTest(unittest.TestLoader().loadTestsFromModule(module))
  58. if run:
  59. unittest.TextTestRunner(verbosity=1+(verbose and 1 or 0)).run(test_suite)
  60. def make_test_records(doctype, verbose=0):
  61. webnotes.flags.mute_emails = True
  62. if not webnotes.conn:
  63. webnotes.connect()
  64. for options in get_dependencies(doctype):
  65. if options.startswith("link:"):
  66. options = options[5:]
  67. if options == "[Select]":
  68. continue
  69. if options not in webnotes.local.test_objects:
  70. webnotes.local.test_objects[options] = []
  71. make_test_records(options, verbose)
  72. make_test_records_for_doctype(options, verbose)
  73. def get_modules(doctype):
  74. module = webnotes.conn.get_value("DocType", doctype, "module")
  75. try:
  76. test_module = load_doctype_module(doctype, module, "test_")
  77. if test_module:
  78. reload(test_module)
  79. except ImportError, e:
  80. test_module = None
  81. return module, test_module
  82. def get_dependencies(doctype):
  83. module, test_module = get_modules(doctype)
  84. options_list = list(set([df.options for df in get_link_fields(doctype)] + [doctype]))
  85. if hasattr(test_module, "test_dependencies"):
  86. options_list += test_module.test_dependencies
  87. if hasattr(test_module, "test_ignore"):
  88. for doctype_name in test_module.test_ignore:
  89. if doctype_name in options_list:
  90. options_list.remove(doctype_name)
  91. return options_list
  92. def make_test_records_for_doctype(doctype, verbose=0):
  93. module, test_module = get_modules(doctype)
  94. if verbose:
  95. print "Making for " + doctype
  96. if hasattr(test_module, "_make_test_records"):
  97. webnotes.local.test_objects[doctype] += test_module._make_test_records(verbose)
  98. elif hasattr(test_module, "test_records"):
  99. webnotes.local.test_objects[doctype] += make_test_objects(doctype, test_module.test_records, verbose)
  100. elif verbose:
  101. print_mandatory_fields(doctype)
  102. def make_test_objects(doctype, test_records, verbose=None):
  103. records = []
  104. for doclist in test_records:
  105. if "doctype" not in doclist[0]:
  106. doclist[0]["doctype"] = doctype
  107. d = webnotes.bean(copy=doclist)
  108. if webnotes.local.test_objects.get(d.doc.doctype):
  109. # do not create test records, if already exists
  110. return []
  111. if has_field(d.doc.doctype, "naming_series"):
  112. if not d.doc.naming_series:
  113. d.doc.naming_series = "_T-" + d.doc.doctype + "-"
  114. # submit if docstatus is set to 1 for test record
  115. docstatus = d.doc.docstatus
  116. d.doc.docstatus = 0
  117. d.insert()
  118. if docstatus == 1:
  119. d.submit()
  120. records.append(d.doc.name)
  121. return records
  122. def print_mandatory_fields(doctype):
  123. print "Please setup make_test_records for: " + doctype
  124. print "-" * 60
  125. doctype_obj = webnotes.get_doctype(doctype)
  126. print "Autoname: " + (doctype_obj[0].autoname or "")
  127. print "Mandatory Fields: "
  128. for d in doctype_obj.get({"reqd":1}):
  129. print d.parent + ":" + d.fieldname + " | " + d.fieldtype + " | " + (d.options or "")
  130. print
  131. def run_unittest(doctype, verbose=False):
  132. module = webnotes.conn.get_value("DocType", doctype, "module")
  133. test_module = get_module_name(doctype, module, "test_")
  134. make_test_records(doctype, verbose=verbose)
  135. test_suite = unittest.TestSuite()
  136. module = webnotes.get_module(test_module)
  137. test_suite.addTest(unittest.TestLoader().loadTestsFromModule(module))
  138. return unittest.TextTestRunner(verbosity=1+(verbose and 1 or 0)).run(test_suite)