简体   繁体   English

如何运行 Odoo 测试 unittest2?

[英]How to run Odoo tests unittest2?

I tried running odoo tests using --test-enable, but it won't work.我尝试使用 --test-enable 运行 odoo 测试,但它不起作用。 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 ?是否可以像 Pycharm 这样的 IDE 运行测试?

This useful For Run odoo test case:这个有用的 For Run odoo 测试用例:

./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. @aftab 您需要添加log-level请参见下文。

./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.关于您的问题,如果您正在对已安装的模块进行更改并需要重新测试所有测试用例,那么您需要使用上述命令使用-u <module_name>-u all (对于所有模块)简单地重新启动服务器。

Here is a REALLY nice plugin to run unit odoo tests directly with pytest:这是一个非常好的插件,可以直接使用 pytest 运行单元 odoo 测试:

https://github.com/camptocamp/pytest-odoo 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).我能够使用 pycharm 运行 odoo 的测试,为此我使用了 docker + pytest-odoo + pycharm(使用远程解释器)。

First you setup a Dockerfile like this:首先,您设置一个 Dockerfile,如下所示:

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:还有一个像这样的 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:

So we extend an odoo image with packages to generate coverage reports and pytest-odoo因此,我们使用包扩展 odoo 图像以生成覆盖率报告和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:一旦你有了这个,你可以运行 docker docker-compose up -d来让你的 odoo 实例运行,odoo 容器将安装pytest-odoo ,下一部分是告诉 pycharm 使用远程解释器和 odoo 修改后的图像,包括pyest-odoo包:

在此处输入图片说明

Now every time you run a script in pycharm they will launch a new container based on the image you provided.现在,每次您在 pycharm 中运行脚本时,它们都会根据您提供的图像启动一个新容器。

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.在检查了 pycharm 启动的容器后,我意识到它们将项目的目录绑定到容器内的/opt/project/目录,这很有用,因为您在运行测试时需要修改odoo.conf文件。 在此处输入图片说明

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.您可以为您应该做的自定义测试数据库自定义数据库连接,重要的部分是您需要将addons_path选项映射到/opt/project/addons或 pycharm 启动的容器内的最终路径,您的自定义插件可用。

With this you can create a pycharm script for pytest like this:有了这个,您可以为 pytest 创建一个 pycharm 脚本,如下所示:

在此处输入图片说明

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.请注意我们如何为 odoo 配置提供路径并进行修改以进行测试,这样 pycharm 启动的容器中可用的 odoo 将知道您的自定义插件代码所在的位置。

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 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.请注意,使用远程解释器,您只需要确保 odoo 二进制文件可以正确找到 addons 文件夹,您将一切就绪:) 除了使用 Dockerfile 扩展图像有助于加快开发速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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