Odoo中的Javascript单元测试

发表于:2015-11-12 10:37

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Kevin-Kong    来源:51Testing软件测试网采编

  前端页面利用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.');
  });
  });
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号