Thursday, January 26, 2017

UnitTest odoo 8 modules with coverage

At the time of this writing, help is a bit sparse on testing with odoo, let alone testing inside eclipse. Although I've been able to achieve it, it's quite the journey so I've decided to write this article.

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 pytest
Then install pytest-odoo plugin:
sudo pip install pytest-odoo
Then you need to export a variable that points to an odoo config file with some database information:
export OPENERP_SERVER=/etc/odoo/openerp-server.conf
If 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 = testjerther
And last, go to the root of your module and run:
pytest -v
Bingo!


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
sudo pip install pytest-cov
You can then add coverage to your tests with:
pytest -v --cov
But 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!

1 comment:

Anonymous said...

I love it, could you integrate things like @pytest.fixture to run and dont deppend of the set up of odoo?