Created
June 24, 2013 11:44
-
-
Save florentx/5849493 to your computer and use it in GitHub Desktop.
Integration between Python unit-tests and OpenERP <assert/>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import unittest | |
from openerp.myaddon.util import osv_unittest | |
class MyTests(unittest.TestCase): | |
def test_upper(self): | |
self.assertEqual('dn_vn_nsn'.upper(), 'DN_VN_NSN') | |
def test_something_else(self): | |
self.assertFalse([]) | |
class Test(osv_unittest): | |
# This is boilerplate | |
_name = 'myaddon.test' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<openerp> | |
<data> | |
<assert model="myaddon.test" search="[]"> | |
<test expr="successful"/> | |
</assert> | |
</data> | |
</openerp> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""Generic tools for Python and OpenERP.""" | |
import sys | |
import unittest | |
from osv import osv, fields | |
__all__ = ['osv_unittest'] | |
def run_unittest(module, verbose=1): | |
suite = unittest.TestSuite() | |
suite.addTest(unittest.findTestCases(module)) | |
runner = unittest.TextTestRunner(stream=sys.stderr, verbosity=verbose) | |
result = runner.run(suite) | |
return result | |
class osv_unittest(osv.osv_memory): | |
"""Base class to integrate 'unittest' with OpenERP.""" | |
_register = False | |
_name = 'osv.unittest' | |
_columns = { | |
'successful': fields.boolean("Successful"), | |
'failures': fields.integer("Failures"), | |
'errors': fields.integer("Errors"), | |
} | |
# openerp.SUPERUSER_ID | |
UID = 1 | |
@property | |
def cr(self): | |
"""Grab a cursor.""" | |
return self.pool.db.cursor() | |
@property | |
def pool_cr_uid(self): | |
"""Helper to retrieve (pool, cr, uid).""" | |
return (self.pool, self.cr, self.UID) | |
def search(self, cr, uid, args, offset=0, limit=None, order=None, | |
context=None, count=False): | |
"""Run the tests.""" | |
tids = super(osv_unittest, self).search(cr, uid, args, offset, limit, | |
order, context, count) | |
if count: | |
return tids | |
if tids: | |
# Tests already run | |
return [max(tids)] | |
# Run the tests once | |
test_id = self._run_unittest(cr, uid) | |
return [test_id] | |
def _run_unittest(self, cr, uid): | |
result = run_unittest(sys.modules[self.__module__], verbose=2) | |
values = { | |
'successful': result.wasSuccessful(), | |
'failures': len(result.failures), | |
'errors': len(result.errors), | |
} | |
return self.create(cr, uid, values) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment