• Odoo中的Javascript单元测试


    前端页面利用QUnit进行单元测试,本文参考官方文档:https://www.odoo.com/documentation/8.0/reference/javascript.html

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

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

    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.');
        });
    
    });
  • 相关阅读:
    超过5名学生的课
    大的国家
    shell启动时读取的配置文件
    shell中的环境变量
    chisel中pviews命令无法使用
    shell script中的$*和$@
    OS X EI Capitan 10.11.4中sudo无法起作用的解决方法
    如何判断CPU的位数
    block中无法使用C数组变量
    布尔代数
  • 原文地址:https://www.cnblogs.com/kfx2007/p/4942649.html
Copyright © 2020-2023  润新知