From 5a9d38f767b91a7e4ccf9ffaf8d09b30d10d8b67 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Fri, 1 Dec 2017 12:05:06 +0530 Subject: [PATCH] [enhance] bench run-tests --doctype-list app/path/to/test.txt (#4561) * [enhance] bench run-tests --doctype-list app/path/to/test.txt * [fix] tests for doctype-list-path * [fix] tests for doctype-list-path --- frappe/commands/utils.py | 5 +- frappe/desk/page/setup_wizard/setup_wizard.js | 2 +- frappe/test_runner.py | 68 ++++++++++++------- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/frappe/commands/utils.py b/frappe/commands/utils.py index c97e11672d..1b042ed88c 100644 --- a/frappe/commands/utils.py +++ b/frappe/commands/utils.py @@ -300,6 +300,7 @@ def console(context): @click.command('run-tests') @click.option('--app', help="For App") @click.option('--doctype', help="For DocType") +@click.option('--doctype-list-path', help="Path to .txt file for list of doctypes. Example erpnext/tests/server/agriculture.txt") @click.option('--test', multiple=True, help="Specific test") @click.option('--driver', help="For Travis") @click.option('--ui-tests', is_flag=True, default=False, help="Run UI Tests") @@ -308,7 +309,7 @@ def console(context): @click.option('--junit-xml-output', help="Destination file path for junit xml report") @pass_context def run_tests(context, app=None, module=None, doctype=None, test=(), - driver=None, profile=False, junit_xml_output=False, ui_tests = False): + driver=None, profile=False, junit_xml_output=False, ui_tests = False, doctype_list_path=None): "Run tests" import frappe.test_runner tests = test @@ -318,7 +319,7 @@ def run_tests(context, app=None, module=None, doctype=None, test=(), ret = frappe.test_runner.main(app, module, doctype, context.verbose, tests=tests, force=context.force, profile=profile, junit_xml_output=junit_xml_output, - ui_tests = ui_tests) + ui_tests = ui_tests, doctype_list_path = doctype_list_path) if len(ret.failures) == 0 and len(ret.errors) == 0: ret = 0 diff --git a/frappe/desk/page/setup_wizard/setup_wizard.js b/frappe/desk/page/setup_wizard/setup_wizard.js index 12ccc69cc3..f55b2296d7 100644 --- a/frappe/desk/page/setup_wizard/setup_wizard.js +++ b/frappe/desk/page/setup_wizard/setup_wizard.js @@ -244,7 +244,7 @@ frappe.setup.SetupWizard = class SetupWizard extends frappe.ui.Slides { this.current_slide = null; this.completed_state_message = this.get_message( __("Setup Complete"), - __("You're all set!") + __("Refreshing...") ); } diff --git a/frappe/test_runner.py b/frappe/test_runner.py index 197fb09147..8d5507b488 100644 --- a/frappe/test_runner.py +++ b/frappe/test_runner.py @@ -4,7 +4,7 @@ from __future__ import unicode_literals, print_function import frappe -import unittest, json, sys +import unittest, json, sys, os import xmlrunner import importlib from frappe.modules import load_doctype_module, get_module_name @@ -25,9 +25,14 @@ def xmlrunner_wrapper(output): return _runner def main(app=None, module=None, doctype=None, verbose=False, tests=(), - force=False, profile=False, junit_xml_output=None, ui_tests=False): + force=False, profile=False, junit_xml_output=None, ui_tests=False, doctype_list_path=None): global unittest_runner + if doctype_list_path: + app, doctype_list_path = doctype_list_path.split(os.path.sep, 1) + with open(frappe.get_app_path(app, doctype_list_path), 'r') as f: + doctype = f.read().strip().splitlines() + xmloutput_fh = None if junit_xml_output: xmloutput_fh = open(junit_xml_output, 'w') @@ -119,19 +124,25 @@ def run_all_tests(app=None, verbose=False, profile=False, ui_tests=False): return out -def run_tests_for_doctype(doctype, verbose=False, tests=(), force=False, profile=False): - module = frappe.db.get_value("DocType", doctype, "module") - if not module: - print('Invalid doctype {0}'.format(doctype)) - sys.exit(1) - - test_module = get_module_name(doctype, module, "test_") - if force: - for name in frappe.db.sql_list("select name from `tab%s`" % doctype): - frappe.delete_doc(doctype, name, force=True) - make_test_records(doctype, verbose=verbose, force=force) - module = importlib.import_module(test_module) - return _run_unittest(module, verbose=verbose, tests=tests, profile=profile) +def run_tests_for_doctype(doctypes, verbose=False, tests=(), force=False, profile=False): + modules = [] + if not isinstance(doctypes, (list, tuple)): + doctypes = [doctypes] + + for doctype in doctypes: + module = frappe.db.get_value("DocType", doctype, "module") + if not module: + print('Invalid doctype {0}'.format(doctype)) + sys.exit(1) + + test_module = get_module_name(doctype, module, "test_") + if force: + for name in frappe.db.sql_list("select name from `tab%s`" % doctype): + frappe.delete_doc(doctype, name, force=True) + make_test_records(doctype, verbose=verbose, force=force) + modules.append(importlib.import_module(test_module)) + + return _run_unittest(modules, verbose=verbose, tests=tests, profile=profile) def run_tests_for_module(module, verbose=False, tests=(), profile=False): module = importlib.import_module(module) @@ -139,7 +150,7 @@ def run_tests_for_module(module, verbose=False, tests=(), profile=False): for doctype in module.test_dependencies: make_test_records(doctype, verbose=verbose) - return _run_unittest(module=module, verbose=verbose, tests=tests, profile=profile) + return _run_unittest(module, verbose=verbose, tests=tests, profile=profile) def run_setup_wizard_ui_test(app=None, verbose=False, profile=False): '''Run setup wizard UI test using test_test_runner''' @@ -154,18 +165,23 @@ def run_ui_tests(app=None, test=None, test_list=None, verbose=False, profile=Fal frappe.flags.ui_test_list = test_list else: frappe.flags.ui_test_path = test - return _run_unittest(module=module, verbose=verbose, tests=(), profile=profile) + return _run_unittest(module, verbose=verbose, tests=(), profile=profile) -def _run_unittest(module, verbose=False, tests=(), profile=False): +def _run_unittest(modules, verbose=False, tests=(), profile=False): test_suite = unittest.TestSuite() - module_test_cases = unittest.TestLoader().loadTestsFromModule(module) - if tests: - for each in module_test_cases: - for test_case in each.__dict__["_tests"]: - if test_case.__dict__["_testMethodName"] in tests: - test_suite.addTest(test_case) - else: - test_suite.addTest(module_test_cases) + + if not isinstance(modules, (list, tuple)): + modules = [modules] + + for module in modules: + module_test_cases = unittest.TestLoader().loadTestsFromModule(module) + if tests: + for each in module_test_cases: + for test_case in each.__dict__["_tests"]: + if test_case.__dict__["_testMethodName"] in tests: + test_suite.addTest(test_case) + else: + test_suite.addTest(module_test_cases) if profile: pr = cProfile.Profile()