Odoo - Testing Modules

Odoo 通過對 Python 的 unittest 封裝提供了對模塊的測試功能
如果要自定義一些測試,只需要簡單的在你的模塊目錄下創建一個 tests 文件夾,它將會自動的檢測你的模塊。
這寫module的名字應該是以test_ 開頭,並且應該在tests/__init__.py 文件中明確的 import

your_module
|-- ...
`-- tests
    |-- __init__.py
    |-- test_bar.py
    `-- test_foo.py

中在這個 __init__.py 中應該包含

from . import test_foo, test_bar

注意
在這個文件中沒有import的,將不會被調用測試

在8.0之前,只會測試在tests/__init__.py 文件中的 在fast_suitechecks 兩個列表中的指明的 test module,
8.0及以後,會測試所有在 tests/__init__.py 文件中 import 的 test module.

test runner 像 unittest 中定義的普通的一樣,跑所有符合 unittest 定義要求的測試函數,Odoo同時也提供了一系列 utilities 來幫助測試 Odoo 相關的數據:

class odoo.tests.common.TransactionCase(methodName='runTest')

這中情況下,每一個 test method 都是自己單獨的一個 transaction,自己單獨的一個 db cursor。在每一個test method 結束之後,這個 transaction 會roll back, cursor 關閉。

  • browse_ref(xid)
    返回對應的 external id 的 record object

    - -
    Parameters: xid - 完整的 external id。包含 ‘.’
    Raise: ValueError if not found
    Returns: BaseModel
  • ref(xid)
    返回對應的 external id 的 database id。

    - -
    Parameters: xid - 完整的 external id。包含 ‘.’
    Raise: ValueError if not found
    Returns: registered id

class odoo.tests.common.SingleTransactionCase(methodName='runTest')

這情況下,所有的 test method 都是同一個 transaction,同一個 db cursor。在最後一個 test method 結束之後,這個 transaction 會roll back, cursor 關閉。

  • browse_ref(xid) 同上
  • ref(xid) 同上

odoo.tests.common.at_install(flag)

設置一個test的 at-install state,flag是一個 boolean,用來指定在 odoo module 安裝的時候,是否應該測試。
通常,tests 是在這個Odoo模塊安裝完成之後,下一個模塊安裝之前,進行測試。

odoo.tests.common.post_install(flag)

設置一個test的 post-install state,flag是一個 boolean,用來指定在 odoo 一系列 module 安裝之後,是否應該測試。
By default, tests are not run after installation of all modules in the current installation set.


最常用的是 TransactionCase,在每一個method中只測試一個property

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...

Running tests

當你設置了 --test-enable 參數,啓動 Odoo server 並且要安裝或者更新 modules 時,就會自動的進行測試。
As of Odoo 8, running tests outside of the install/update cycle is not supported.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章