How to run odoo tests with Pycharm

Julia Suarez
plusteam
Published in
6 min readMay 19, 2021

--

There is an example repository with an addon and all the scripts mentioned here: https://github.com/JSilversun/odoo-testing-example

If you are an odoo developer, you probably would like to write some unit tests like regular pytest/unit test suites so you can debug, get coverage reports and enhance your own developer experience.

So, how can we achieve it?

Well, let’s start by using a docker-compose file to get our odoo and database running:

Docker compose file to set up odoo 14

Note: The --dev all args will allow your odoo instance to restart once it detects changes on your addons or related volumes code.

And a Dockerfile like this:

In this file we see how we extend an odoo image, we install some packages like pytest-odoo coverage and pytest-html to generate coverage reports and run odoo’s tests with Pycharm. Also, a coverage directory is created with proper permissions so the coverage files can be copied from the docker container to the host machine later.

Now we run:

docker-compose up -d

And we will see the following containers:

Now we can go to http://localhost:8069 and we will see something like this:

Feel free to change the connection details, just make sure you remember them.

At this point you will have your odoo instance running, you were able to use a docker compose to start an odoo instance with a Postgres database, it’s easy to change the odoo version you need with the image tag.

Now the question will be, if we have the odoo instance running in a docker, how can we run the tests?

I created a Makefile to teach you how to generate coverage reports or set up a database only for testing purposes:

First, we need to set up a database only to run the test so we don’t use the main database we use for our manual development, you can run make init_test_db to create a test database or reset it.

Start by connecting to the container using:

docker exec -it plusteam-odoo-web bash

Now you can run:

odoo --help

This is the odoo binary, if you take a look at all the options it provides, you will be able to see a test section with all the flags available.

Run the tests with:

odoo --test-enable -d db_test -p 8001

Note: Since the docker container for odoo is running the server already, you can specify a different port with the -p flag so you won’t get an error Address already in use.

This is the “default” way to run odoo’s test, however, since this runs in a docker container, it can have a terrible developer experience since you can’t use your IDE to debug them.

So, how can we run our tests using Pycharm?

Go to Settings > Project > Python Interpreter

Add a new python interpreter, select docker, and specify the image name of the service. If you can’t find the image name for your customized odoo image you can generate an image with a name like this:

docker build -t <image_name> .

Now you will have a local image that will appear in the dialog mentioned previously. You can rename it if for some reason you need to invalidate the cached image.

A summary of how remote interpreters work in Pycharm: They will use the image name to launch a docker container anytime you run something through your IDE, for example:

When you open the Python console of your IDE like this:

Pycharm will start a docker container based on the image you provided:

This is why the image used needs to have available pytest-odoo so we can run our tests later.

This container was started automatically by Pycharm as soon as you open the python console, the same will happen if you having any python script using a remote interpreter.

You can connect to the docker container as you would do with any kind of container like this:

docker exec -it <container_name|container_id> bash

Then you will realize that your container looks a bit different than you might expect:

As you can see all the project code is available in the docker container by default.

Now to we need to add a config to run our tests with Pycharm:

It’s important to note that Pycharm won’t use the volume bindings specified in the docker-compose file, this means the odoo.conf file from the config folder won’t be copied to the folder odoo expects and this leads to problems when we run our tests because odoo won’t know where the addons are located, to solve this, we specified an odoo.conf file used only for tests which main difference is mapping the addons path to /opt/project/addons since this is the directory where Pycharm will copy the entire project’s code by default.

Note: Review your database connection to make sure the odoo.test.conf has proper credentials for the test database.

With these settings now our tests work as expected!

We can even debug our tests:

Coverage

Coverage for custom addons

Since we use pytest to run our odoo tests, we can use it to generate html reports as well, I created a command to make this process easier. Please note that this will only consider the tests inside your custom addons, since I don’t know yet how to tell pytest to run all the tests inside odoo’s binary.

How it looks like when you run the script

After the script finishes, you will get the report in the coverage/local folder

This is how the pytest report looks like.

Coverage for all tests

I also read about how can I can generate a coverage report for all tests even the ones inside odoo’s binary.

The process is similar:

However this process will take more time, once it’s done, you can see the html report in the coverage/all folder.

This is how the report looks like.

Conclusion

Hope this article was useful to enhance your developer experience with odoo, docker definitely changes the way developers work since we can have open source projects running with few lines of code in a docker-compose file and even better, we can customize images to add things missing like libraries to allow us running tests.

Pycharm’s remote interpreters is an amazing feature that lets us take full advantage of docker images to debug or run scripts.

--

--