Odoo的模塊測(cè)試是基于unittest。
簡(jiǎn)要介紹編寫(xiě)測(cè)試程序,僅需要在模塊里定義tests 子模塊,在測(cè)試的時(shí)候會(huì)自動(dòng)識(shí)別的。測(cè)試的py文件需要以test_ 開(kāi)頭而且需要導(dǎo)入到tests/__init__.py 。
your_module |-- ... `-- tests |-- __init__.py |-- test_bar.py `-- test_foo.py
|
__init__.py 包含
from . import test_foo, test_bar
|
沒(méi)有從tests/__init__.py 導(dǎo)入的測(cè)試模塊將不會(huì)運(yùn)行
測(cè)試模塊的寫(xiě)法和一般的unittest文檔里一樣。但Odoo給模塊測(cè)試提供了一些基類(lèi)和函數(shù):
默認(rèn)是模塊在安裝完畢后測(cè)試會(huì)運(yùn)行。測(cè)試也可以在設(shè)置為在所有模塊安裝后執(zhí)行:
openerp.tests.common.at_install(flag)
- 設(shè)置at-install測(cè)試狀態(tài),用于是否運(yùn)行在模塊安裝的時(shí)候。默認(rèn)測(cè)試會(huì)在該模塊安裝后,下一個(gè)模塊安裝前運(yùn)行。
openerp.tests.common.post_install(flag)
- 設(shè)置post-install測(cè)試狀態(tài),用于是否運(yùn)行在一系列模塊安裝后。默認(rèn)測(cè)試不會(huì)在當(dāng)前安裝的模塊集后運(yùn)行。
最常見(jiàn)的案例就是使用TransactionCase ,并在方法里測(cè)試模塊的一些屬性
class TestModelA(common.TransactionCase): def test_some_action(self): record = self.env['model.a'].create({'field': 'value'}) record.some_action() self.assertEqual( record.field, expected_field_value)
# other tests...
|
運(yùn)行測(cè)試如果在啟動(dòng)Odoo服務(wù)器時(shí)設(shè)置了--test-enable ,測(cè)試會(huì)在安裝和更新模塊后自動(dòng)運(yùn)行。
|