How to do it...

We have to add our test file and make it known to the test mechanism in the appropriate template:

  1. Add the static/test/r4_tests.js file:
odoo.define('r4.tests', function (require) {
  1. Require what we need:
var test_utils = require('web.test_utils'), 
FormView = require('web.FormView');
  1. Provide some test data:
    QUnit.module( 
'r4',
{
beforeEach: function()
{
this.data = {
'res.partner': {
fields: {
user_id: {
string: 'User id',
type: 'many2one',
relation: 'res.users',
},
},
records: [
{
id: 1,
user_id: 1,
},
],
},
'res.users': {
fields: {
display_name: {
string: 'Display name',
type: 'char',
},
},
records: [
{
id: 1,
display_name: 'Administrator',
},
{
id: 4,
display_name: 'Demo user',
},
],
},
};
},
  1. Run the actual test:
function() 
{
QUnit.test('Test many2one_buttons widget', function(assert) {
assert.expect(2);
var view = test_utils.createView({
View: FormView,
model: 'res.partner',
data: this.data,
arch: '<form><field name="user_id"
widget="many2one_buttons" /></form>',
});
assert.strictEqual(2, view.$('.btn').length);
view.on('field_changed', view, function(e)
{
assert.strictEqual(1, e.data.changes.user_id.id);
});
view.$('.btn[data-id=1]').click();
});
}
  1. Make the test file known to the test mechanism in views/templates.xml:
    <template id="qunit_suite" inherit_id="web.qunit_suite"> 
<xpath expr="//t[@t-set='head']" position="inside">
<script src="/r4_tests/static/test/r4_tests.js"
type="text/javascript" />
</xpath>
</template>

When you navigate to /web/tests now, the web client will run all tests available. You should find our test in this list, too, hopefully with a positive result. Given that a lot of tests will run there, it can be simpler to also pass the module you want to test. In our case, select r4 in the upper-right drop-down box.

Note that what you need to select as the module in the test page is not necessarily the name of your addon, but whatever section name you defined in your QUnit.module call earlier.