zoukankan      html  css  js  c++  java
  • 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.');
        });
    
    });
  • 相关阅读:
    Palindrome Partitioning
    triangle
    Populating Next Right Pointers in Each Node(I and II)
    分苹果(网易)
    Flatten Binary Tree to Linked List
    Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)
    iOS系统navigationBar背景色,文字颜色处理
    登录,注销
    ios 文字上下滚动效果Demo
    经常崩溃就是数组字典引起的
  • 原文地址:https://www.cnblogs.com/kfx2007/p/4942649.html
Copyright © 2011-2022 走看看