访问/web/tests页面可以看到当前所有包含测试模块的单元测试列表。

Odoo自定义单元测试方法如下:

1. __openerp__文件中添加对js文件的引用:

(非官方文档中将js文件写到'test'中,而是引用view文件,在view文件中添加对js的引用,8.0以后有效)

 

<template id="assets_backend" name="qweb_test" inherit_id="web.qunit_suite">
            <xpath expr="//head" position="inside">
                <script type="text/javascript" src="/px_qweb/static/test/demo.js"></script>
            </xpath>
</template>

 

 

 

2. 创建js文件,写单元测试

单元测试以openerp.testing为命名空间,测试写在section函数中。

 

openerp.testing.section('my.test',function(test){
    test('my first test',function(){
        ok(false,"this test has run");
    });
});

 

 

3.断言

  ok(state[,messsage]):检查state是否为真

  strictEqual(actual,expected[,message]):严格测试相等

  notStrictEqual(actual,expected[,message]):严格测试是否不等

    deepEqual(actual,expected[,message]):对于对象和数组类型,确保其每个元素的键值都相等

    notDeepEqual(actual,expected[,message]):与deepEqual相反

    throws(block[,expected][,messsage]):block抛出异常,验证抛出异常是否与预期(expected)一致。

    equal(actual,expected[,message]):测试是否相等

    notEqual(actual,expected[,message]):与equal相反

 

例子:

自定义data.js文件:

 

 
(function(){
    openerp.px_qweb =  {
            value_true:true,
            SomeType: openerp.web.Class.extend({
                init:function(value){
                    this.value = value;
                }
            })
        };
}());
 

 

单元测试:

 
openerp.testing.section('PX QWeb',function(test){

    test('PX Test',function(instance){
        ok(instance.px_qweb.value_true,'should have a true value');
        var type_instance = new instance.px_qweb.SomeType(50);
        strictEqual(type_instance.value,50,'should have provided value.');
    });

});