11
.. i18n: Since version 4.2 of OpenERP, the XML api provides several features to test your modules. They allow to
14
Since version 4.2 of OpenERP, the XML api provides several features to test your modules. They allow to
16
.. i18n: * test the properties of your records, your class invariants etc.
17
.. i18n: * test your methods
18
.. i18n: * manipulate your objects to check your workflows and specific methods
21
* test the properties of your records, your class invariants etc.
23
* manipulate your objects to check your workflows and specific methods
25
.. i18n: This thus allows you to simulate user interaction and automatically test your modules.
28
This thus allows you to simulate user interaction and automatically test your modules.
33
.. i18n: As you will see in the next pages, unit testing through Open ERP's XML can be done using three main tags: <assert>, <workflow> and <function>. All these tags share some common optional attributes:
39
As you will see in the next pages, unit testing through Open ERP's XML can be done using three main tags: <assert>, <workflow> and <function>. All these tags share some common optional attributes:
46
.. i18n: allows to do the tag interpretation through a specific User ID (you must specify the XML id of that user, for example "base.user_demo")
49
allows to do the tag interpretation through a specific User ID (you must specify the XML id of that user, for example "base.user_demo")
56
.. i18n: allows to specify a context dictionary (given as a Python expression) to use when applicable (for <function> notice that not all objects methods take a context attribute so it wont be automatically transmitted to them, however it applies on <value>)
59
allows to specify a context dictionary (given as a Python expression) to use when applicable (for <function> notice that not all objects methods take a context attribute so it wont be automatically transmitted to them, however it applies on <value>)
61
.. i18n: These two attributes might be set on any of those tags (for <functions>, only the root <function> tag may accept it) or on the <data> tag itself. If you set a context attribute on both, they will be merged automatically.
64
These two attributes might be set on any of those tags (for <functions>, only the root <function> tag may accept it) or on the <data> tag itself. If you set a context attribute on both, they will be merged automatically.
66
.. i18n: Notice that Unit Testing tags will not be interpreted inside a <data> tag set in noupdate.
69
Notice that Unit Testing tags will not be interpreted inside a <data> tag set in noupdate.
71
.. i18n: Using unit tests
72
.. i18n: ================
78
.. i18n: You can declare unit tests in all your .XML files. We suggest you to name the files like this:
81
You can declare unit tests in all your .XML files. We suggest you to name the files like this:
83
.. i18n: * module_name_test.xml
86
* module_name_test.xml
88
.. i18n: If your tests are declared at demo data in the __terp__.py, they will be checked at the installation of the system with demo data. Example of usage, testing the the demo sale order produce a correct amount in the generated invoice.
91
If your tests are declared at demo data in the __terp__.py, they will be checked at the installation of the system with demo data. Example of usage, testing the the demo sale order produce a correct amount in the generated invoice.
93
.. i18n: If your tests are declared like init data, they will be checked at all installation of the softwares. Use it to test the consistency of the software after installation.
96
If your tests are declared like init data, they will be checked at all installation of the softwares. Use it to test the consistency of the software after installation.
98
.. i18n: If your tests are declared in update sections, the tests are checked at the installation and also at all updates. Use it to tests consistencies, invariants of the module. Example: The sum of the credit must be equals to the sum of the debit for all non draft entries in the accounting module. Putting tests in update sections is very usefull to check consistencies of migrations or new version upgrades.
101
If your tests are declared in update sections, the tests are checked at the installation and also at all updates. Use it to tests consistencies, invariants of the module. Example: The sum of the credit must be equals to the sum of the debit for all non draft entries in the accounting module. Putting tests in update sections is very usefull to check consistencies of migrations or new version upgrades.
110
.. i18n: The assert tag allows to define some assertion that have to be checked at boot time. Example :
113
The assert tag allows to define some assertion that have to be checked at boot time. Example :
115
.. i18n: .. code-block:: xml
117
.. i18n: <assert model="res.company" id="main_company" string="The main company name is Tiny SPRL">
118
.. i18n: <test expr="name">Tiny sprl</test>
124
<assert model="res.company" id="main_company" string="The main company name is Tiny SPRL">
125
<test expr="name">Tiny sprl</test>
128
.. i18n: This assert will check that the company with id main_company has a name equal to "Open sprl". The expr field specifies a python expression to evaluate. The expression can access any field of the specified model and any python built-in function (such as sum, reduce etc.). The ref function, which gives the database id corresponding to a specified XML id, is also available (in the case that "ref" is also the name of an attribute of the specified model, you can use _ref instead). The resulting value is then compared with the text contained in the test tag. If the assertion fails, it is logged as a message containing the value of the string attribute and the test tag that failed.
131
This assert will check that the company with id main_company has a name equal to "Open sprl". The expr field specifies a python expression to evaluate. The expression can access any field of the specified model and any python built-in function (such as sum, reduce etc.). The ref function, which gives the database id corresponding to a specified XML id, is also available (in the case that "ref" is also the name of an attribute of the specified model, you can use _ref instead). The resulting value is then compared with the text contained in the test tag. If the assertion fails, it is logged as a message containing the value of the string attribute and the test tag that failed.
133
.. i18n: For more complex tests it is not always sufficient to compare a result to a string. To do that you may instead omit the tag's content and just put an expression that must evaluate to True:
136
For more complex tests it is not always sufficient to compare a result to a string. To do that you may instead omit the tag's content and just put an expression that must evaluate to True:
138
.. i18n: .. code-block:: xml
140
.. i18n: <assert model="res.company"
141
.. i18n: id="main_company"
142
.. i18n: string="The main company's currency is €" : severity="warning">
143
.. i18n: <test expr="currency_id.code == 'eur'.upper()"/>
149
<assert model="res.company"
151
string="The main company's currency is €" : severity="warning">
152
<test expr="currency_id.code == 'eur'.upper()"/>
155
.. i18n: The severity attribute defines the level of the assertion: debug, info, warning, error or critical. If an assertion of too high severity fails, an exception is thrown and the parsing stops. If that appends during server initialization, the server will stop. Else the exception will be transmitted to the client. The level at which a failure will throw an exception is by default at warning, but can be specified at server launch trough the --assert-exit-level argument.
158
The severity attribute defines the level of the assertion: debug, info, warning, error or critical. If an assertion of too high severity fails, an exception is thrown and the parsing stops. If that appends during server initialization, the server will stop. Else the exception will be transmitted to the client. The level at which a failure will throw an exception is by default at warning, but can be specified at server launch trough the --assert-exit-level argument.
160
.. i18n: As sometimes you do not know the id a priori, that attribute can be substituted by a search. So we can define another example, which will be always true :
163
As sometimes you do not know the id a priori, that attribute can be substituted by a search. So we can define another example, which will be always true :
165
.. i18n: .. code-block:: xml
167
.. i18n: <assert model="res.partner"
168
.. i18n: search="[('name','=','Agrolait')]"
169
.. i18n: string="The name of Agrolait is :Agrolait">
170
.. i18n: <test expr="name">Agrolait</test>
176
<assert model="res.partner"
177
search="[('name','=','Agrolait')]"
178
string="The name of Agrolait is :Agrolait">
179
<test expr="name">Agrolait</test>
182
.. i18n: When you use the search, each resulting record is tested but the assertion is counted only once. Thus if an assertion fails, the remaining records wont be tested. In addition, if the search finds no record, nothing will be tested so the assertion will be considered successful. If you want to make sure that there are a certain number of results, you might use the count parameter:
185
When you use the search, each resulting record is tested but the assertion is counted only once. Thus if an assertion fails, the remaining records wont be tested. In addition, if the search finds no record, nothing will be tested so the assertion will be considered successful. If you want to make sure that there are a certain number of results, you might use the count parameter:
187
.. i18n: .. code-block:: xml
189
.. i18n: <assert model="res.partner"
190
.. i18n: search="[('name','=','Agrolait')]"
191
.. i18n: string="The name of Agrolait is :Agrolait" count="1">
192
.. i18n: <test expr="name">Agrolait</test>
198
<assert model="res.partner"
199
search="[('name','=','Agrolait')]"
200
string="The name of Agrolait is :Agrolait" count="1">
201
<test expr="name">Agrolait</test>
209
.. i18n: Require the version of a module.
212
Require the version of a module.
214
.. i18n: .. code-block:: xml
216
.. i18n: <!-- modules requirement -->
217
.. i18n: <assert model="ir.module.module"
218
.. i18n: search="[('name','=','common')]"
219
.. i18n: severity="critical" count="1">
220
.. i18n: <test expr="state == 'installed'" />
221
.. i18n: <!-- only check module version -->
222
.. i18n: <test expr="'.'.join(installed_version.split('.')[3:]) >= '2.4'" />
226
.. i18n: Workflow Tag
227
.. i18n: =============
232
<!-- modules requirement -->
233
<assert model="ir.module.module"
234
search="[('name','=','common')]"
235
severity="critical" count="1">
236
<test expr="state == 'installed'" />
237
<!-- only check module version -->
238
<test expr="'.'.join(installed_version.split('.')[3:]) >= '2.4'" />
245
.. i18n: The workflow tag allows you to call for a transition in a workflow by sending a signal to it. It is generally used to simulate an interaction with a user (clicking on a button…) for test purposes:
248
The workflow tag allows you to call for a transition in a workflow by sending a signal to it. It is generally used to simulate an interaction with a user (clicking on a button…) for test purposes:
250
.. i18n: .. code-block:: xml
252
.. i18n: <workflow model="sale.order" ref="test_order_1" action="order_confirm" />
257
<workflow model="sale.order" ref="test_order_1" action="order_confirm" />
259
.. i18n: This is the syntax to send the signal order_confirm to the sale order with id test_order_1.
262
This is the syntax to send the signal order_confirm to the sale order with id test_order_1.
264
.. i18n: Notice that workflow tags (as all other tags) are interpreted as root which might be a problem if the signals handling needs to use some particular property of the user (typically the user's company, while root does not belong to one). In that case you might specify a user to switch to before handling the signal, through the uid property:
267
Notice that workflow tags (as all other tags) are interpreted as root which might be a problem if the signals handling needs to use some particular property of the user (typically the user's company, while root does not belong to one). In that case you might specify a user to switch to before handling the signal, through the uid property:
269
.. i18n: .. code-block:: xml
271
.. i18n: <workflow model="sale.order" ref="test_order_1" action="manual_invoice" uid="base.user_admin" />
276
<workflow model="sale.order" ref="test_order_1" action="manual_invoice" uid="base.user_admin" />
278
.. i18n: (here we had to specify the module base - from which user_admin comes - because this tag is supposed to be placed in an xml file of the sale module)
281
(here we had to specify the module base - from which user_admin comes - because this tag is supposed to be placed in an xml file of the sale module)
283
.. i18n: In some particular cases, you do not know a priori the id of the object to manipulate through the workflow. It is thus allowed to replace the ref attribute by a value child tag:
286
In some particular cases, you do not know a priori the id of the object to manipulate through the workflow. It is thus allowed to replace the ref attribute by a value child tag:
288
.. i18n: .. code-block:: xml
290
.. i18n: <workflow model="account.invoice" action="invoice_open">
291
.. i18n: <value model="sale.order" eval="obj(ref('test_order_1')).invoice_ids[0].id" />
297
<workflow model="account.invoice" action="invoice_open">
298
<value model="sale.order" eval="obj(ref('test_order_1')).invoice_ids[0].id" />
301
.. i18n: (notice that it the eval part must evaluate to a valid database id)
304
(notice that it the eval part must evaluate to a valid database id)
306
.. i18n: Function Tag
307
.. i18n: ============
313
.. i18n: The function tag allows to call some method of an object. The called method must have the following signature:
316
The function tag allows to call some method of an object. The called method must have the following signature:
318
.. i18n: def mymethod(self, cr, uid [, …])
321
def mymethod(self, cr, uid [, …])
328
.. i18n: * cr is the database cursor
329
.. i18n: * uid is the user id
332
* cr is the database cursor
335
.. i18n: Most of the methods defined in Tiny respect that signature as cr and uid are required for a lot of operations, including database access.
338
Most of the methods defined in Tiny respect that signature as cr and uid are required for a lot of operations, including database access.
340
.. i18n: The function tag can then be used to call that method:
343
The function tag can then be used to call that method:
345
.. i18n: .. code-block:: xml
347
.. i18n: <function model="mypackage.myclass" name="mymethod" />
352
<function model="mypackage.myclass" name="mymethod" />
354
.. i18n: Most of the time you will want to call your method with additional arguments. Suppose the method has the following signature:
357
Most of the time you will want to call your method with additional arguments. Suppose the method has the following signature:
359
.. i18n: def mymethod(self, cr, uid, mynumber)
362
def mymethod(self, cr, uid, mynumber)
364
.. i18n: There are two ways to call that method:
367
There are two ways to call that method:
369
.. i18n: * either by using the eval attribute, which must be a python expression evaluating to the list of additional arguments:
372
* either by using the eval attribute, which must be a python expression evaluating to the list of additional arguments:
374
.. i18n: .. code-block:: xml
376
.. i18n: <function model="mypackage.myclass" name="mymethod" eval="[42]" />
381
<function model="mypackage.myclass" name="mymethod" eval="[42]" />
383
.. i18n: In that case you have access to all native python functions an to a function ref which takes as argument an XML id and returns the corresponding id.
386
In that case you have access to all native python functions an to a function ref which takes as argument an XML id and returns the corresponding id.
388
.. i18n: * or by putting a child node inside the function tag:
391
* or by putting a child node inside the function tag:
393
.. i18n: .. code-block:: xml
395
.. i18n: <function model="mypackage.myclass" name="mymethod">
396
.. i18n: <value eval="42" />
402
<function model="mypackage.myclass" name="mymethod">
406
.. i18n: Only value and function tags have a meaning as function child nodes (using other tags will give unspecified results). This means that you can use the returned result of a method call as an argument of another call. You can put as many child nodes as you want, each one being an argument of the method call (keeping them in order). You can also mix child nodes and the eval attribute. In that case it will be evaluated first and child nodes will be appended to the resulting list.
409
Only value and function tags have a meaning as function child nodes (using other tags will give unspecified results). This means that you can use the returned result of a method call as an argument of another call. You can put as many child nodes as you want, each one being an argument of the method call (keeping them in order). You can also mix child nodes and the eval attribute. In that case it will be evaluated first and child nodes will be appended to the resulting list.
411
.. i18n: ==================
412
.. i18n: Acceptance testing
413
.. i18n: ==================
420
.. i18n: This document describes all tests that are made each time someone install Open ERP on a computer. You can then assume that all these tests are valid as we must launch them before publishing a new module or a release of Open ERP.
423
This document describes all tests that are made each time someone install Open ERP on a computer. You can then assume that all these tests are valid as we must launch them before publishing a new module or a release of Open ERP.
425
.. i18n: Integrity tests on migrations
426
.. i18n: =============================
429
Integrity tests on migrations
430
=============================
432
.. i18n: * Sum credit = Sum debit
433
.. i18n: * Balanced account chart
436
* Sum credit = Sum debit
437
* Balanced account chart
439
.. i18n: ... Describe all integrity tests here
442
... Describe all integrity tests here
444
.. i18n: Workflow tests
445
.. i18n: ==============
451
.. i18n: ... Describe all processus tested here.
454
... Describe all processus tested here.
456
.. i18n: Record creation
457
.. i18n: ===============
463
.. i18n: More than 300 records are created, describe them here.
466
More than 300 records are created, describe them here.