Before we start, if you've not done so already, take a look at Python UnitTest Framework doc and then Testing Odoo Modules . I now assume you know how to write and organize unit tests for odoo. Also, in the latter link, you'll read that testing outside odoo is not supported. Which might be true for the odoo team, but lately I came accross a really neat plugin. But enough of this, let's dig in!
We're gonna be using pytest and the plugin won't work with older versions so make sure it is up to date:
sudo pip install -U pytestThen install pytest-odoo plugin:
sudo pip install pytest-odooThen you need to export a variable that points to an odoo config file with some database information:
export OPENERP_SERVER=/etc/odoo/openerp-server.confIf you're already running odoo, this file should already be configured but could miss this important parameter so make sure it is there:
db_name = testjertherAnd last, go to the root of your module and run:
pytest -vBingo!
I'd like to run this inside Eclipse with its pytest runner, but I gave up after (too) many hours.
Now for coverage, install pytest-cov
Now for coverage, install pytest-cov
sudo pip install pytest-covYou can then add coverage to your tests with:
pytest -v --covBut that gives a LOT, and I found that using --cov=moduleName does not work, but we can apply a filter in a config file. So go ahead and create one in the root folder of your project and put these lines in:
[run]
include = */moduleName/*
There are many other parameters that can go in there. Now you can run:
pytest -v --cov --cov-config=coverage.config
You can also export the coverage in html by adding --cov-report html:cov_html where cov_html is a folder. check out the doc for more options!