Skip to main content

py.test plugin to run Odoo tests

Project description

PyPI - Version PyPI - Python Version PyPI - Downloads codecov

pytest-oduit

A pytest plugin for running Odoo tests with automatic Odoo setup and oduit integration.

Features

  • Automatic Odoo configuration: Integrates with .oduit.toml configuration files using oduit-core
  • Automatic module installation: Automatically detects and installs addon modules based on test paths
  • Module path resolution: Automatically resolves Odoo addon module paths for proper test discovery
  • Test retry management: Disables Odoo's built-in test retry mechanism to work seamlessly with pytest
  • Distributed testing support: Works with pytest-xdist for parallel test execution
  • HTTP server support: Explicit Odoo HTTP server launch for HttpCase and integration tests
  • Actionable config errors: Surfaces clearer messages when generated Odoo options are invalid

Installation

pip install pytest-oduit

Note: pytest-odoo must not be installed. Also, do not run Odoo module tests from inside the pytest-oduit source directory (it contains Odoo mocks used for this project's own tests).

Requirements

  • Python >= 3.9
  • pytest >= 8
  • oduit
  • Odoo >= 15.0

Usage

Basic Usage

Simply run pytest in your Odoo addon directory:

pytest

The plugin will automatically detect which addon modules contain your tests and initialize them in Odoo. For example:

pytest addons/sale                    # Automatically adds --init=sale
pytest addons/sale addons/purchase    # Automatically adds --init=purchase,sale
pytest addons/sale/tests/test_sale.py # Automatically adds --init=sale

This eliminates the need to manually specify --odoo-install for each test run.

Other pytest plugins

This plugin works also together pytest-subtests and pytest-xdist.

Command Line Options

  • --odoo-log-level: Set the log level for Odoo processes during tests (default: 'critical')
  • --odoo-install: Control module installation behavior:
    • Not specified (default): Automatically detect and install modules based on test paths
    • --odoo-install=module1,module2: Manually specify modules to install (disables auto-detection)
    • --odoo-install="": Disable all module installation
  • --oduit-env: Path to an oduit config file (if omitted, uses local .oduit.toml in current directory)
  • --odoo-http: Enables the Odoo HTTP server for HttpCase and integration tests

Automatic Module Installation

By default, the plugin automatically detects which addon modules are being tested and initializes them in Odoo. This eliminates the need to manually specify modules for each test run.

How it works:

  1. Analyzes the test paths provided to pytest
  2. Extracts addon names by locating __manifest__.py files
  3. Automatically appends --init=<detected_modules> to the Odoo configuration

Examples:

# Auto-detect and install modules
pytest addons/sale                    # Installs: sale
pytest addons/sale addons/purchase    # Installs: purchase, sale
pytest addons/sale/tests/test_sale.py # Installs: sale

# Manually specify modules (overrides auto-detection)
pytest --odoo-install=sale,purchase addons/crm  # Installs: sale, purchase (NOT crm)

# Disable all module installation
pytest --odoo-install="" addons/sale  # Installs: nothing

When automatic detection activates:

  • A .oduit.toml configuration file is present or --oduit-env is specified
  • --odoo-install is not provided (no manual override)

Supported path types:

  • Addon directories: addons/my_module
  • Test files: addons/my_module/tests/test_something.py
  • Subdirectories: addons/my_module/tests/
  • Multiple addons: addons/module_a addons/module_b

Configuration

The plugin loads configuration from one of these sources:

  1. --oduit-env=/path/to/config.toml (explicit path)
  2. Local .oduit.toml in the current working directory

This config is converted into Odoo CLI options and passed to odoo.tools.config.parse_config(). Use valid Odoo option keys (for example, db_maxconn, not db-maxconn).

Example .oduit.toml:

python_bin = "/path/to/.venv/bin/python"
odoo_bin = "/path/to/odoo/odoo-bin"
db_name = "test_db"
db_user = "odoo"
db_password = "odoo"
db_host = "127.0.0.1"
db_port = 5432
db_maxconn = 64
http_port = 8069
addons_path = ["./addons", "./custom_addons"]

Sectioned format is also supported:

[binaries]
python_bin = "/path/to/.venv/bin/python"
odoo_bin = "/path/to/odoo/odoo-bin"

[odoo_params]
db_name = "test_db"
db_maxconn = 64
addons_path = ["./addons", "./custom_addons"]

Module Path Resolution

The plugin automatically resolves Odoo addon module paths, ensuring that:

  • Test modules in addon_name/tests/ are properly recognized as odoo.addons.addon_name.tests.test_module
  • Only installable addons (with installable: True in __manifest__.py) are collected for testing
  • Namespace packages are handled correctly

Distributed Testing

Works seamlessly with pytest-xdist for parallel test execution:

pytest -n auto  # Run tests in parallel using all available CPUs

The plugin automatically creates isolated database copies for each worker to prevent conflicts.

Integration testing

Real-Odoo integration coverage lives under tests_integration/odoo and uses oduit config files instead of pytest-odoo-only flags.

Prerequisites:

  • a reachable PostgreSQL server (for example PGHOST=127.0.0.1, PGUSER=odoo)
  • an Odoo 18 or 19 environment installable in the active Python environment
  • the repo addon path rendered as a comma-separated addons_path string in .oduit-18.toml / .oduit-19.toml

Useful commands:

tox -e py313-unit
pytest --oduit-env tests_integration/odoo/.oduit-18.toml --odoo-install=pytest_oduit_test_module --odoo-http tests_integration/odoo/addons/pytest_oduit_test_module
pytest --oduit-env tests_integration/odoo/.oduit-18.toml --odoo-install=pytest_oduit_test_module tests_integration/odoo/addons/pytest_oduit_test_module

The first integration command must pass the model, subtest, and HTTP coverage. The second command must still pass, but skip only the HttpCase test with a clear rerun hint.

Troubleshooting

If startup fails with an error like:

pytest: error: no such option: --db-maxconn

check your oduit config key names. Some Odoo DB options use underscores in their CLI names.

  • Correct: db_maxconn -> --db_maxconn
  • Incorrect: db-maxconn -> --db-maxconn

pytest-oduit now raises a clearer pytest.UsageError for invalid generated Odoo options and includes a hint for common DB option naming mistakes.

Usage in Harnesses

Codex

Local network access needs to be activated, otherwise pytest is not able to communicate with the local postgres server.

codex -c sandbox_policy.network_access=enabled
# Enable network access for the workspace
[sandbox_workspace_write]
network_access = true

Development

Running Tests

cd pytest-oduit
PYTHONPATH=tests/mock/odoo:$PYTHONPATH pytest
tox -e py313-unit

Test Structure

The plugin includes comprehensive tests that use mock Odoo modules to verify functionality without requiring a full Odoo installation.

License

AGPLv3 - see LICENSE file for details.

Authors

  • Holger Nahrstaedt
  • Based on original work by Pierre Verkest and Camptocamp SA

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_oduit-0.4.5.tar.gz (42.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_oduit-0.4.5-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file pytest_oduit-0.4.5.tar.gz.

File metadata

  • Download URL: pytest_oduit-0.4.5.tar.gz
  • Upload date:
  • Size: 42.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_oduit-0.4.5.tar.gz
Algorithm Hash digest
SHA256 15d3d59211b873a3fbcf218fc8cda6f59c7e9007fc5273d31e4271efad6aa6f0
MD5 48f9bb5fca73f016a80389b37cecb965
BLAKE2b-256 19b613008d0ced4be1143757996b1e8ad1d2ef9c0a41bf363382c9af2d60223a

See more details on using hashes here.

File details

Details for the file pytest_oduit-0.4.5-py3-none-any.whl.

File metadata

  • Download URL: pytest_oduit-0.4.5-py3-none-any.whl
  • Upload date:
  • Size: 23.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_oduit-0.4.5-py3-none-any.whl
Algorithm Hash digest
SHA256 95ee6376201d70fa3ed8a3cc8a9b28dc7c6bfb56dcf57d62ac2254d1ff3f8e80
MD5 af53fa2b9d2d912a6c5ca6b4430e38dc
BLAKE2b-256 7a2929b4f40ae026daa71772813f6402f72ed445310e7671e9631ac082d9d64d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page