Odoo 16 Development Book

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

odoo Development

While writing tests:

  • 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.

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/.

odoo Development

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();
});
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message