- 上传者: Administrator
- 上传时间:2024年02月24日 06时58分01秒
Qunit Test** The Odoo framework uses the QUnit library test system as a test splinter. QUnit characterizes the concept of tests and modules (a set of ......
- Define a tests subpackage in your module's static directory.
- Create a js test file in the tests directory.
- After defining the test suite, add test cases.
- Add the file to the main test assets.
- Visit /web/tests/ to make sure the test is executed.
Qunit Test
The Odoo framework uses the QUnit library test system as a test splinter. QUnit characterizes the concept of tests and modules (a set of related tests) and provides a web-based interaction point for running tests.
web.test_utils provides the test programs needed to create JavaScript test cases.
Adding Qunit Test Cases
While writing tests:
Let's look at an example qunit test for JavaScript code (e.g., helper function myFunction in test_addon.utils). The process to add a new test case is as follows.
First, create our test file inside static/tests/utils_tests.js
odoo.define('student.arithmetic', function (require) {
"use strict";
var pyUtils = require('web.py_utils');
var testUtils = require("web.test_utils");
QUnit.module(student,{}, function (){
});
});
Define the test case inside the test file.
odoo.define('student.arithmetic', function (require) {
"use strict";
var pyUtils = require('web.py_utils');
var testUtils = require("web.test_utils");
QUnit.module('student',{}, function (){
QUnit.test('simpletest', function (assert) {
assert.expect(2);
var result = pyUtils.py_eval("4 + 2");
assert.strictEqual(result, 6, "should properly evaluate sum");
result = pyUtils.py_eval("6 * 6");
assert.strictEqual(result, 36, "should properly evaluate multiplication operator");
});
});
});
Add our tests inside the asset (manifest file).
'assets': {
'web.qunit_suite_tests': [
'student/static/tests/test_file.js'
],
},
Running test by visiting /web/tests/.
Testing by using Helper Function:
It's very difficult to test some parts of Odoo without help. In particular, views can communicate with the server and perform many RPCs that need to be mocked. For this reason, we have developed some special helper functions in test_utils.js.
Mock test functions: These features help you set up your test environment. The main use case is to mock answers from Odoo servers. These functions use the mock server. This is a JavaScript class that simulates responses to the most common model methods (read, search_read, name_get, etc.).
DOM helpers: Useful for simulating events/actions on specific targets. For example, testUtils.dom.click performs a click on the target. Note that this is safer than doing it manually, as it also checks that the target exists and is visible.
create helpers: These are probably the most important functions exported by test_utils.js. These helpers help you create widgets with simulated environments and lots of details to simulate real-world conditions as much as possible. The most important is indeed the createView.
qunit assertions: QUnit can be reached out with particular assertions. For Odoo, we often test some DOM properties. To this end, we made a few assertions to assist with that. For instance, the containsOnce assertion takes a widget/jQuery/HtmlElement and a selector and afterward checks in the event that the objective contains precisely one counterpart for the CSS selector.
For example, with these helpers, a simple form test might look like this:
QUnit.test('Simple Form Test ', async function (assert) {
assert.expect(1);
var form = await testUtils.createView({
View: FormView,
model: 'student.student',
data: this.data,
arch: '' +
'' +
'',
res_id: 1,
});
var line = form.$('[name="f_name"]').length
assert.strictEqual(line, 1,
"The field exist");
form.destroy();
});
form.destroy();
});
- 微信扫一扫,一分也是爱:
服务原则及地区范围
宜兴通达网络科技有限公司,地处中国宜兴环科园内,是一家高新技术企业。公司在企业网络维护和企业信息化建设与咨询方面,有10多年经验。
我公司愿与客户一道,力求彻底解决客户问题!
我们不是在给企业提供“头痛医头、脚痛医脚”的暂时解决方案,而是在部署根本性安全与稳定服务!!
我们愿携手客户,建立企业IT规划;杜绝随意安装系统、软件等操作;力求共同维护有序、安全、稳定的网络办公环境!!!
IT服务,服务是根本,客户是上帝;我们提供快速响应、快速上门、快速排查,提供优质高效的服务!!!!
通达科技提供全国范围内的服务,服务形式包括远程协助、电话咨询、电子邮件咨询、传真咨询、问答平台的问题解决等。
宜兴地区提供上门服务:
- 市区服务:宜城街道、城北街道(屺亭街道)、新街街道、新庄街道、环科园、渚桥开发区
- 市郊服务:张渚镇、西渚镇、太华镇、徐舍镇、官林镇、杨巷镇、新建镇、和桥镇、高塍镇、万石镇、周铁镇、芳桥镇、丁蜀镇、湖父镇。