pytest-odoo icon indicating copy to clipboard operation
pytest-odoo copied to clipboard

PyCharm support/usage

Open Carelvd opened this issue 5 years ago • 5 comments

I came across this module while trying to do development within PyCharm. I can't seem to get this working properly with their testing interface and I was hoping some one here might have some experience with this or a configuration they are willing to share. I find that PyCharm errors out around :

pypkgpath = self.fspath.pypkgpath()
pkgroot = pypkgpath.dirpath()

Claiming that pypkgpath is None. I suspect that the issue lies with the scope under which PyCharm is executing PyTest-Odoo but haven't resolved how to set this properly.

There is a related question on Stack Overflow that also requires an answer but is not getting any love over here

Carelvd avatar May 06 '19 09:05 Carelvd

Hi, i have successfully integrated pytest in PyCharm with this project, but I have to fix the method where the addon module name is created, here is the commit https://github.com/lck/pytest-odoo/commit/edbbf887ae2da48ffb6e0ada90da002f5bce8ac8

lck avatar May 06 '19 09:05 lck

So if I create a PyCharm configuration from the provided PyTest template (Run/Debug > Python tests > pytest) and populate it as follows :

pycharm_pytest

Then PyTest-Odoo errors out with the following :

Testing started at 12:21 PM ...
E:\env\odoo\Scripts\python.exe D:\applications\helpers\pycharm\_jb_pytest_runner.py -- -m pytest -s --odoo-config=..\odoo.conf
Launching pytest with arguments -m pytest -s --odoo-config=..\odoo.conf in E:\xlr8\odoo
============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-4.4.1, py-1.8.0, pluggy-0.9.0
rootdir: E:\xlr8\odoo
plugins: odoo-0.4.8.dev1+g985cb08.d20190503
addons/account_check_printing/tests/test_print_check.py:None (addons/account_check_printing/tests/test_print_check.py)
e:\python\pytest-odoo\pytest_odoo.py:136: in _importtestmodule
    pkgroot = pypkgpath.dirpath()
E   AttributeError: 'NoneType' object has no attribute 'dirpath

If you have gotten it working with some other configuration I'd be mighty interested to see what you used. Thanks for the quick response.

Carelvd avatar May 06 '19 10:05 Carelvd

Better create new template for pytest in "Run -> Edit Configurations" dialog, select "Templates -> Python tests -> pytest", add the "OPENERP_SERVER" variable to the "Environment variables"

image

The critical and only required configuration is the OPENERP_SERVER=/home/rlacko/Repos/odoo9/test.cfg environment variable - the test.cfg is regular odoo config file.

After this You can run the tests directly from code editor (via CTRL+F9) - the test configuration will be created from above test template.

lck avatar May 06 '19 10:05 lck

I have similar problem cant use a conftest.py file

Error when create any conftest.py file insite tests directory

../../../env/lib/python3.8/site-packages/pytest_odoo.py:116: in _importtestmodule
    pkgroot = pypkgpath.dirpath()
E   AttributeError: 'NoneType' object has no attribute 'dirpath'

samsagaz avatar Aug 19 '20 15:08 samsagaz

I was able to use pycharm with a python remote interpreter to run tests and debug them, I wrote an article and an example repository, you can read it here: https://medium.com/plusteam/how-to-run-odoo-tests-with-pycharm-51e4823bdc59

In summary, I used a Dockerfile to extend an odoo image so I don't have to deal with any kind of installation, just install the test packages you need:

// Dockerfile
FROM odoo:14
USER root
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        python3-pip
RUN mkdir -p /coverage/all && mkdir -p /coverage/local && chown -R odoo /coverage
RUN pip3 install pytest-odoo coverage pytest-html
USER odoo

Then in you need a docker-compose like this:

// docker-compose.yml
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:

Now when you run docker-compose up you will have a docker container with pytest-odoo installed, this will generate a build image, now in pycharm you go to Settings > Project > Python Interpreter You select docker and use the newly built image (The image of odoo modified with some tests modules installed).

This will tell pycharm that anytime you want to run a python script, launch a new docker container based on the image you provided which will have pytest-odoo and whatever you did in the Dockerfile.

However, for remote interpreters, pycharm will copy all the files available in your project's directory to the /opt/project directory in the docker container.

Knowing this and that regular volume bindings won't apply to any container launched by pycharm, you can create a custom odoo.test.conf or just a conf file that will be used only for testing, in this config file you need to specify the connection to the testing database and the addons path to /opt/project/addons.

Again, there is a repository with an example here: https://github.com/JSilversun/odoo-testing-example, there you can see the conf file used for testing and you will also see a command in the Makefile to create a clean database only for tests and other things like generating coverage reports either with the coverage package or the pytest-odoo package.

Then you create a new pycharm configuration for pytest: image

Notice how you specify the config file used for tests, with this you will be able to run your tests using pycharm: image

You can even debug them, hope this is useful to you, I did this because in our team we don't like having the odoo sourcecode available, we just use odoo compiled binaries.

julia-suarez-deel avatar May 18 '21 15:05 julia-suarez-deel