Odoo的模块测试是基于unittest。
简要介绍编写测试程序,仅需要在模块里定义tests 子模块,在测试的时候会自动识别的。测试的py文件需要以test_ 开头而且需要导入到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 导入的测试模块将不会运行
测试模块的写法和一般的unittest文档里一样。但Odoo给模块测试提供了一些基类和函数:
默认是模块在安装完毕后测试会运行。测试也可以在设置为在所有模块安装后执行:
openerp.tests.common.at_install(flag)
- 设置at-install测试状态,用于是否运行在模块安装的时候。默认测试会在该模块安装后,下一个模块安装前运行。
openerp.tests.common.post_install(flag)
- 设置post-install测试状态,用于是否运行在一系列模块安装后。默认测试不会在当前安装的模块集后运行。
最常见的案例就是使用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...
|
运行测试如果在启动Odoo服务器时设置了--test-enable ,测试会在安装和更新模块后自动运行。
|