简体   繁体   中英

How to run Odoo tests unittest2?

I tried running odoo tests using --test-enable, but it won't work. I have a couple of questions.

According to the documentation Tests can only be run during module installation, what happens when we add functionality and then want to run tests?

Is it possible to run tests from IDE like Pycharm ?

This useful For Run odoo test case:

./odoo.py -i/-u module_being_tested -d being_used_to_test --test-enable

Common options:

 -i INIT, --init=INIT
       install one or more modules (comma-separated list, use "all" for all modules), requires -d

-u UPDATE, --update=UPDATE
       update one or more modules (comma-separated list, use "all" for all modules). Requires -d.

Database related options:

-d DB_NAME, --database=DB_NAME
       specify the database name

Testing Configuration:

 --test-enable:  Enable YAML and unit tests.

@aftab You need add log-level please see below.

./odoo.py -d <dbname> --test-enable --log-level=test

and regarding you question, If you are making changes to installed modules and need to re test all test cases then you need to simple restart you server with -u <module_name> or -u all (for all modules) with the above command.

Here is a REALLY nice plugin to run unit odoo tests directly with pytest:

https://github.com/camptocamp/pytest-odoo

Here's a result example:

在此处输入图片说明

I was able to run odoo's tests using pycharm, to achieve this I used docker + pytest-odoo + pycharm (using remote interpreters).

First you setup a Dockerfile like this:

FROM odoo:14
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 docker-compose.yml like this:

version: '2'

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:

So we extend an odoo image with packages to generate coverage reports and pytest-odoo

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:

在此处输入图片说明

Now every time 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 file when you run your tests. 在此处输入图片说明

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:

在此处输入图片说明

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.

在此处输入图片说明

I go further in this matter (my particular solution) 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

Be aware that using remote interpreters you just need to make sure the odoo binary can find the addons folder properly and you will be all set :) besides using a Dockerfile to extend an image helps to speed up development.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM