From 5577442251454e0233e634d7009fc91777f1a25c Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Wed, 19 Jul 2017 18:09:06 +0530 Subject: [PATCH] [tests] refactor tests (#3743) * [tests] refactored client side tests * [tests] refactored client side tests * [tests] refactored client side tests --- frappe/contacts/doctype/address/address.js | 15 ++- ..._test_controller.js => test_controller.js} | 0 frappe/core/doctype/doctype/doctype.py | 4 +- .../doctype/test_runner/_test_test_runner.js | 23 ++++ .../core/doctype/test_runner/test_runner.js | 33 +++-- .../core/doctype/test_runner/test_runner.json | 32 ++++- .../core/doctype/test_runner/test_runner.py | 26 +--- .../guides/automated-testing/qunit-testing.md | 11 +- frappe/modules/utils.py | 8 +- frappe/public/build.json | 1 + frappe/public/js/frappe/dom.js | 9 +- .../public/js/frappe/form/footer/timeline.js | 6 +- frappe/public/js/frappe/misc/test_utils.js | 41 +++++++ frappe/tests/ui/_test_desktop.js | 64 ---------- frappe/tests/ui/_test_gantt_view.js | 60 --------- frappe/tests/ui/_test_print_format_builder.js | 56 --------- frappe/tests/ui/data/test_data_for_views.js | 16 --- frappe/tests/ui/data/test_lib.js | 115 ++++-------------- .../ui/global_search/_test_list_document.js | 44 ------- frappe/tests/ui/global_search/_test_math.js | 64 ---------- .../ui/global_search/_test_new_record.js | 55 --------- .../ui/global_search/_test_open_module.js | 42 ------- .../ui/global_search/_test_search_document.js | 88 -------------- frappe/tests/ui/test_calendar_view.js | 6 +- .../tests/ui/test_list/_test_list_delete.js | 62 ---------- .../tests/ui/test_list/_test_list_values.js | 16 --- .../tests/ui/test_list/_test_quick_entry.js | 27 ---- frappe/tests/ui/test_list/test_list_filter.js | 38 +++--- frappe/tests/ui/test_list/test_list_paging.js | 21 ++-- .../tests/ui/test_module/test_module_menu.js | 26 ---- frappe/tests/ui/test_module_view.js | 41 +++++++ .../tests => tests/ui}/test_number_format.js | 0 frappe/tests/ui/test_test_runner.py | 28 ++--- frappe/tests/ui/tests.txt | 5 + 34 files changed, 262 insertions(+), 821 deletions(-) rename frappe/core/doctype/doctype/boilerplate/{_test_controller.js => test_controller.js} (100%) create mode 100644 frappe/core/doctype/test_runner/_test_test_runner.js create mode 100644 frappe/public/js/frappe/misc/test_utils.js delete mode 100644 frappe/tests/ui/_test_desktop.js delete mode 100644 frappe/tests/ui/_test_gantt_view.js delete mode 100644 frappe/tests/ui/_test_print_format_builder.js delete mode 100644 frappe/tests/ui/data/test_data_for_views.js delete mode 100644 frappe/tests/ui/global_search/_test_list_document.js delete mode 100644 frappe/tests/ui/global_search/_test_math.js delete mode 100644 frappe/tests/ui/global_search/_test_new_record.js delete mode 100644 frappe/tests/ui/global_search/_test_open_module.js delete mode 100644 frappe/tests/ui/global_search/_test_search_document.js delete mode 100644 frappe/tests/ui/test_list/_test_list_delete.js delete mode 100644 frappe/tests/ui/test_list/_test_list_values.js delete mode 100644 frappe/tests/ui/test_list/_test_quick_entry.js delete mode 100644 frappe/tests/ui/test_module/test_module_menu.js create mode 100644 frappe/tests/ui/test_module_view.js rename frappe/{public/js/frappe/misc/tests => tests/ui}/test_number_format.js (100%) create mode 100644 frappe/tests/ui/tests.txt diff --git a/frappe/contacts/doctype/address/address.js b/frappe/contacts/doctype/address/address.js index 7809f426ea..a8d4860117 100644 --- a/frappe/contacts/doctype/address/address.js +++ b/frappe/contacts/doctype/address/address.js @@ -32,10 +32,15 @@ frappe.ui.form.on("Address", { } }, after_save: function() { - var last_route = frappe.route_history.slice(-2, -1)[0]; - if(frappe.dynamic_link && frappe.dynamic_link.doc - && frappe.dynamic_link.doc.name == last_route[2]){ - frappe.set_route(last_route[0], last_route[1], last_route[2]); - } + frappe.run_serially([ + () => frappe.timeout(1), + () => { + var last_route = frappe.route_history.slice(-2, -1)[0]; + if(frappe.dynamic_link && frappe.dynamic_link.doc + && frappe.dynamic_link.doc.name == last_route[2]){ + frappe.set_route(last_route[0], last_route[1], last_route[2]); + } + } + ]); } }); diff --git a/frappe/core/doctype/doctype/boilerplate/_test_controller.js b/frappe/core/doctype/doctype/boilerplate/test_controller.js similarity index 100% rename from frappe/core/doctype/doctype/boilerplate/_test_controller.js rename to frappe/core/doctype/doctype/boilerplate/test_controller.js diff --git a/frappe/core/doctype/doctype/doctype.py b/frappe/core/doctype/doctype/doctype.py index 3593b0b73c..2e13e64f57 100644 --- a/frappe/core/doctype/doctype/doctype.py +++ b/frappe/core/doctype/doctype/doctype.py @@ -338,7 +338,9 @@ class DocType(Document): if not self.istable: make_boilerplate("controller.js", self.as_dict()) #make_boilerplate("controller_list.js", self.as_dict()) - make_boilerplate("_test_controller.js", self.as_dict()) + if not os.path.exists(frappe.get_module_path(frappe.scrub(self.module), + 'doctype', frappe.scrub(self.name), 'tests')): + make_boilerplate("test_controller.js", self.as_dict()) if self.has_web_view: templates_path = frappe.get_module_path(frappe.scrub(self.module), 'doctype', frappe.scrub(self.name), 'templates') diff --git a/frappe/core/doctype/test_runner/_test_test_runner.js b/frappe/core/doctype/test_runner/_test_test_runner.js new file mode 100644 index 0000000000..0b0bd9a98b --- /dev/null +++ b/frappe/core/doctype/test_runner/_test_test_runner.js @@ -0,0 +1,23 @@ +/* eslint-disable */ +// rename this file from _test_[name] to test_[name] to activate +// and remove above this line + +QUnit.test("test: Test Runner", function (assert) { + let done = assert.async(); + + // number of asserts + assert.expect(1); + + frappe.run_serially('Test Runner', [ + // insert a new Test Runner + () => frappe.tests.make([ + // values to be set + {key: 'value'} + ]), + () => { + assert.equal(cur_frm.doc.key, 'value'); + }, + () => done() + ]); + +}); diff --git a/frappe/core/doctype/test_runner/test_runner.js b/frappe/core/doctype/test_runner/test_runner.js index 0c305e7014..d5cac7f8a5 100644 --- a/frappe/core/doctype/test_runner/test_runner.js +++ b/frappe/core/doctype/test_runner/test_runner.js @@ -32,31 +32,28 @@ frappe.ui.form.on('Test Runner', { frappe.dom.eval(f.script); }); - // if(frm.doc.module_name) { - // QUnit.module.only(frm.doc.module_name); - // } - QUnit.testDone(function(details) { - var result = { - "Module name": details.module, - "Test name": details.name, - "Assertions": { - "Total": details.total, - "Passed": details.passed, - "Failed": details.failed - }, - "Skipped": details.skipped, - "Todo": details.todo, - "Runtime": details.runtime - }; + // var result = { + // "Module name": details.module, + // "Test name": details.name, + // "Assertions": { + // "Total": details.total, + // "Passed": details.passed, + // "Failed": details.failed + // }, + // "Skipped": details.skipped, + // "Todo": details.todo, + // "Runtime": details.runtime + // }; + + // eslint-disable-next-line + // console.log(JSON.stringify(result, null, 2)); details.assertions.map(a => { // eslint-disable-next-line console.log(`${a.result ? '✔' : '✗'} ${a.message}`); }); - // eslint-disable-next-line - console.log(JSON.stringify(result, null, 2)); }); QUnit.load(); diff --git a/frappe/core/doctype/test_runner/test_runner.json b/frappe/core/doctype/test_runner/test_runner.json index 8396d5df43..ccc1361dc9 100644 --- a/frappe/core/doctype/test_runner/test_runner.json +++ b/frappe/core/doctype/test_runner/test_runner.json @@ -42,6 +42,36 @@ "set_only_once": 0, "unique": 0 }, + { + "allow_bulk_edit": 0, + "allow_on_submit": 0, + "bold": 0, + "collapsible": 0, + "columns": 0, + "fieldname": "app", + "fieldtype": "Data", + "hidden": 0, + "ignore_user_permissions": 0, + "ignore_xss_filter": 0, + "in_filter": 0, + "in_global_search": 0, + "in_list_view": 0, + "in_standard_filter": 0, + "label": "App", + "length": 0, + "no_copy": 0, + "permlevel": 0, + "precision": "", + "print_hide": 0, + "print_hide_if_no_value": 0, + "read_only": 0, + "remember_last_selected_value": 0, + "report_hide": 0, + "reqd": 0, + "search_index": 0, + "set_only_once": 0, + "unique": 0 + }, { "allow_bulk_edit": 0, "allow_on_submit": 0, @@ -83,7 +113,7 @@ "issingle": 1, "istable": 0, "max_attachments": 0, - "modified": "2017-07-12 23:16:15.910891", + "modified": "2017-07-19 03:22:33.221169", "modified_by": "Administrator", "module": "Core", "name": "Test Runner", diff --git a/frappe/core/doctype/test_runner/test_runner.py b/frappe/core/doctype/test_runner/test_runner.py index a59ddc69a5..c09d75ae4f 100644 --- a/frappe/core/doctype/test_runner/test_runner.py +++ b/frappe/core/doctype/test_runner/test_runner.py @@ -13,37 +13,23 @@ class TestRunner(Document): def get_test_js(): '''Get test + data for app, example: app/tests/ui/test_name.js''' test_path = frappe.db.get_single_value('Test Runner', 'module_path') + test_js = [] # split app, test_path = test_path.split(os.path.sep, 1) - test_js = get_test_data(app) - # full path + # now full path test_path = frappe.get_app_path(app, test_path) - with open(test_path, 'r') as fileobj: - test_js.append(dict( - script = fileobj.read() - )) - return test_js - -def get_test_data(app): - '''Get the test fixtures from all js files in app/tests/ui/data''' - test_js = [] - def add_file(path): with open(path, 'r') as fileobj: test_js.append(dict( script = fileobj.read() )) - data_path = frappe.get_app_path(app, 'tests', 'ui', 'data') - if os.path.exists(data_path): - for fname in os.listdir(data_path): - if fname.endswith('.js'): - add_file(os.path.join(data_path, fname)) - - if app != 'frappe': - add_file(frappe.get_app_path('frappe', 'tests', 'ui', 'data', 'test_lib.js')) + # add test_lib.js + add_file(frappe.get_app_path('frappe', 'tests', 'ui', 'data', 'test_lib.js')) + add_file(test_path) return test_js + diff --git a/frappe/docs/user/en/guides/automated-testing/qunit-testing.md b/frappe/docs/user/en/guides/automated-testing/qunit-testing.md index d66dde2782..ac7a1f3ccf 100644 --- a/frappe/docs/user/en/guides/automated-testing/qunit-testing.md +++ b/frappe/docs/user/en/guides/automated-testing/qunit-testing.md @@ -22,11 +22,16 @@ To run a Test Runner based test, use the `run-ui-tests` bench command by passing This will pass the filename to `test_test_runner.py` that will load the required JS in the browser and execute the tests -### Adding Fixtures / Test Data +### Debugging Tests -You can also add data that you require for all tests in the `tests/ui/data` folder of your app. All the files in this folder will be loaded in the browser before running the test. +To debug a test, you can open it in the **Test Runner** from your UI and run it manually to see where it is exactly failing. -The file `frappe/tests/ui/data/test_lib.js`, which contains library functions for testing is always loaded. +### Test Sequence + +In Frappé UI tests are run in a fixed sequence to ensure dependencies. + +The sequence in which the tests will be run will be in `tests/ui/tests.txt` +file. ### Running All UI Tests diff --git a/frappe/modules/utils.py b/frappe/modules/utils.py index ab353950bf..2acb5b5db6 100644 --- a/frappe/modules/utils.py +++ b/frappe/modules/utils.py @@ -208,17 +208,11 @@ def make_boilerplate(template, doc, opts=None): template_name = template.replace("controller", scrub(doc.name)) target_file_path = os.path.join(target_path, template_name) - # allow alternate file paths beginning with _ (e.g. for _test_controller.js) - if template_name.startswith('_'): - alt_target_file_path = os.path.join(target_path, template_name[1:]) - else: - alt_target_file_path = target_file_path - if not doc: doc = {} app_publisher = get_app_publisher(doc.module) - if not os.path.exists(target_file_path) and not os.path.exists(alt_target_file_path): + if not os.path.exists(target_file_path): if not opts: opts = {} diff --git a/frappe/public/build.json b/frappe/public/build.json index 3b58de727b..3fafb6c5f2 100755 --- a/frappe/public/build.json +++ b/frappe/public/build.json @@ -125,6 +125,7 @@ "public/js/frappe/misc/common.js", "public/js/frappe/misc/pretty_date.js", "public/js/frappe/misc/utils.js", + "public/js/frappe/misc/test_utils.js", "public/js/frappe/misc/tools.js", "public/js/frappe/misc/datetime.js", "public/js/frappe/misc/number_format.js", diff --git a/frappe/public/js/frappe/dom.js b/frappe/public/js/frappe/dom.js index c66841ceee..f3727ae16f 100644 --- a/frappe/public/js/frappe/dom.js +++ b/frappe/public/js/frappe/dom.js @@ -219,13 +219,6 @@ frappe.get_modal = function(title, content) { return $(frappe.render_template("modal", {title:title, content:content})).appendTo(document.body); }; -frappe._in = function(source, target) { - // returns true if source is in target and both are not empty / falsy - if(!source) return false; - if(!target) return false; - return (target.indexOf(source) !== -1); -}; - // add