Odoo pytest pycharm

Viewed 602

I'm trying to run pytest for odoo 13 in pycharm without success. from the console the test runs ok with this command (python odoo-bin -c odoo.conf -i test_module --test-enable). How do you run unit tests in odoo? Do you have some documentation how I can set up pycharm to run pytest? Thank you!

2 Answers

I was able to run tests for custom addons using pycharm but this only applies to custom addons in my case because I use docker to set up my odoo instance, this means odoo is a binary instead of copying the source code.

By the way, this response uses the package pytest-odoo please take a look at their docs first.

To do this with docker + pycharm + pytest-odoo, you need to specify a Dockerfile like this:

USER root
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        python3-pip
RUN pip3 install pytest-odoo coverage pytest-html
USER odoo

And a regular docker-compose like this:


services:
  web:
    container_name: plusteam-odoo-web
    build:
      context: .
      dockerfile: Dockerfile
    image: odoo:14
    depends_on:
      - db
    ports:
      - "8069:8069"
    volumes:
      - odoo-web-data:/var/lib/odoo
      - ./config:/etc/odoo
      - ./addons:/mnt/extra-addons
    command: --dev all
  db:
    container_name: plusteam-odoo-db
    image: postgres:13
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_PASSWORD=odoo
      - POSTGRES_USER=odoo
      - PGDATA=/var/lib/postgresql/data/pgdata
    volumes:
      - odoo-db-data:/var/lib/postgresql/data/pgdata
volumes:
  odoo-web-data:
  odoo-db-data:

Once you have this, you can run docker-compose up -d to get your odoo instance running, the odoo container will have pytest-odoo installed, the next part is to tell pycharm to use a remote interpreter with the odoo modified image including the pyest-odoo package:

enter image description here

Now everytime you run a script in pycharm they will launch a new container based on the image you provided.

After examining the containers launched by pycharm I realized they bind the project's directory to the /opt/project/ directory inside the container, this is useful because you will need to modify the odoo.conf when you run your tests.

enter image description here

You can customize the database connection for a custom testing db which you should do, and the important part is that you need to map the addons_path option to /opt/project/addons or the final path inside the containers launched by pycharm where your custom addons are available.

With this you can create a pycharm script for pytest like this: enter image description here

Notice how we provided the path for the odoo config with modifications for testing, this way the odoo available in the container launched by pycharm will know where your custom addon's code is located.

Now we can run the script and even debug it and everything will work as expected.

enter image description here

I go further in this matter in a medium article, I even wrote a repository with a working demo so you can try it out, hope this helps:

https://medium.com/plusteam/how-to-run-odoo-tests-with-pycharm-51e4823bdc59 https://github.com/JSilversun/odoo-testing-example

You can find the structure here Testing Test Module Structure

Also please check that the naming on the folder and file in which you wrote the code,

for example - tests/test_todo.py Also, don't forget to update the import in tests/init.py to from. import test_todo

This is because Odoo expects the test module names to start with test_ when it searches for tests belonging to a module Code Reference

Command to Run the testcases:

python ./odoo.py -i module_to_test --log-level=test -d your_database --db-filter=your_database --test-enable --stop-after-init

Note: Test cases run only and only with the demo data.

Related