Odoo的模塊測試是基于unittest。
簡要介紹編寫測試程序,僅需要在模塊里定義tests 子模塊,在測試的時候會自動識別的。測試的py文件需要以test_ 開頭而且需要導(dǎo)入到tests/__init__.py 。
your_module |-- ... `-- tests |-- __init__.py |-- test_bar.py `-- test_foo.py
|
__init__.py 包含
from . import test_foo, test_bar
|
沒有從tests/__init__.py 導(dǎo)入的測試模塊將不會運(yùn)行
測試模塊的寫法和一般的unittest文檔里一樣。但Odoo給模塊測試提供了一些基類和函數(shù):
默認(rèn)是模塊在安裝完畢后測試會運(yùn)行。測試也可以在設(shè)置為在所有模塊安裝后執(zhí)行:
openerp.tests.common.at_install(flag)
- 設(shè)置at-install測試狀態(tài),用于是否運(yùn)行在模塊安裝的時候。默認(rèn)測試會在該模塊安裝后,下一個模塊安裝前運(yùn)行。
openerp.tests.common.post_install(flag)
- 設(shè)置post-install測試狀態(tài),用于是否運(yùn)行在一系列模塊安裝后。默認(rèn)測試不會在當(dāng)前安裝的模塊集后運(yùn)行。
最常見的案例就是使用TransactionCase ,并在方法里測試模塊的一些屬性
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)行測試如果在啟動Odoo服務(wù)器時設(shè)置了--test-enable ,測試會在安裝和更新模塊后自動運(yùn)行。
|