The front-end page uses Qunit for unit testing, this article refers to the official documentation: https://www.odoo.com/documentation/8.0/reference/javascript.html
Access the/web/tests page to see all current unit test lists that contain test modules.
The custom unit test method is as follows:
1. Add a reference to the JS file in the __openerp__ file:
(in the unofficial document to write the JS file in the ' test ', but reference the view file, in the view file to add a reference to JS, after 8.0 valid)
<TemplateID= "Assets_backend"name= "Qweb_test"inherit_id= "Web.qunit_suite"> <XPathExpr= "//head"position= "Inside"> <Scripttype= "Text/javascript"src= "/px_qweb/static/test/demo.js"></Script> </XPath></Template>
2. Create JS file, write unit test
Unit tests take openerp.testing as namespaces, and tests are written in the section function.
Openerp.testing.section (' My.test ', function (test) { Test (' My first Test ', function () { OK} (False, "this test has Run ");});
3. Assertions
Ok (State[,messsage]): Check if state is true
Strictequal (Actual,expected[,message]): Strictly Test for equality
Notstrictequal (Actual,expected[,message]): rigorously tested for unequal
Deepequal (Actual,expected[,message]): For object and array types, ensure that the key values for each element are equal
Notdeepequal (Actual,expected[,message]): Contrary to deepequal
Throws (Block[,expected][,messsage]): Block throws an exception to verify that the thrown exception is the same as expected (expected).
Equal (Actual,expected[,message]): Test for equality
NotEqual (Actual,expected[,message]): Contrary to equal
Example:
Custom data.js Files:
(function () { Openerp.px_qweb = { value_true:true, SomeType:openerp.web.Class.extend ({ init: function (value) { this.value = value; }} ) };} ());
Unit tests:
Openerp.testing.section (' px QWeb ', function (test) { test (' px test ', function (instance) { OK (instance.px_ Qweb.value_true, ' should has a true value '); var type_instance = new Instance.px_qweb. SomeType (+); Strictequal (type_instance.value,50, ' should has provided value. '); });
The JavaScript unit test in Odoo