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

Support odoo>=18.0

Open petrus-v opened this issue 1 year ago • 2 comments

Avoid logging error/warning on: fix 2/3 topic of #79 (you needs this PR to make is works https://github.com/odoo/odoo/pull/184409)

  • In odoo 18 if we load odoo.tests.common before setting test_enable=True we get a logging error which is very noisy while developing. ie :
pytest --odoo-database db-test odoo/addons/ --ff --sw
Importing test framework, avoid importing from business modules and when not running in test mode
Stack (most recent call last):
  File "~/.venv/bin/pytest", line 8, in <module>
    sys.exit(console_main())
  File "~/.venv/lib/python3.12/site-packages/_pytest/config/__init__.py", line 201, in console_main
    code = main()
  File "~/.venv/lib/python3.12/site-packages/_pytest/config/__init__.py", line 156, in main
    config = _prepareconfig(args, plugins)
  File "~/.venv/lib/python3.12/site-packages/_pytest/config/__init__.py", line 341, in _prepareconfig
    config = pluginmanager.hook.pytest_cmdline_parse(
  File "~/.venv/lib/python3.12/site-packages/pluggy/_hooks.py", line 513, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  File "~/.venv/lib/python3.12/site-packages/pluggy/_manager.py", line 120, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "~/.venv/lib/python3.12/site-packages/pluggy/_callers.py", line 103, in _multicall
    res = hook_impl.function(*args)
  File "~/.venv/lib/python3.12/site-packages/_pytest/config/__init__.py", line 1140, in pytest_cmdline_parse
    self.parse(args)
  File "~/.venv/lib/python3.12/site-packages/_pytest/config/__init__.py", line 1494, in parse
    self._preparse(args, addopts=addopts)
  File "~/.venv/lib/python3.12/site-packages/_pytest/config/__init__.py", line 1381, in _preparse
    self.pluginmanager.load_setuptools_entrypoints("pytest11")
  File "~/.venv/lib/python3.12/site-packages/pluggy/_manager.py", line 421, in load_setuptools_entrypoints
    plugin = ep.load()
  File "/home/pverkest/.local/share/uv/python/cpython-3.12.6-linux-x86_64-gnu/lib/python3.12/importlib/metadata/__init__.py", line 205, in load
    module = import_module(match.group('module'))
  File "/home/pverkest/.local/share/uv/python/cpython-3.12.6-linux-x86_64-gnu/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "~/src/pytest-odoo/pytest_odoo.py", line 22, in <module>
    import odoo.tests
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "~/src/ocb/odoo/tests/__init__.py", line 8, in <module>
    from . import common
  File "<frozen importlib._bootstrap>", line 1415, in _handle_fromlist
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "~/src/ocb/odoo/tests/common.py", line 98, in <module>
    _logger.error(
================================================================================================================================== test session starts ===================================================================================================================================
platform linux -- Python 3.12.6, pytest-8.3.3, pluggy-1.5.0
rootdir: ~
configfile: pyproject.toml
plugins: odoo-2.1.1
collected 2 items                                                                                                                                                                                                                                                                        
run-last-failure: rerun previous 1 failure first
stepwise: skipping 0 already passed items.

bacause of this code https://github.com/odoo/odoo/blob/18.0/odoo/tests/common.py#L93-L101

_logger = logging.getLogger(__name__)
if config['test_enable'] or config['test_file']:
    _logger.info("Importing test framework", stack_info=_logger.isEnabledFor(logging.DEBUG))
else:
    _logger.error(
        "Importing test framework"
        ", avoid importing from business modules and when not running in test mode",
        stack_info=True,
    )
  • second point is a warning displayed while opening the registry
 /.venv/lib/python3.12/site-packages/pytest_odoo.py:167: DeprecationWarning: Use directly odoo.modules.registry.Registry
    odoo.registry(db_name)

because this registry method becomes deprecated https://github.com/odoo/odoo/blob/18.0/odoo/init.py#L37 we can safely use the Registry as this method do because we are setting the database and this code hasn't change from odoo v10.0

petrus-v avatar Oct 19 '24 13:10 petrus-v

:warning: Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Please upload report for BASE (master@f602202). Learn more about missing BASE report.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff            @@
##             master      #80   +/-   ##
=========================================
  Coverage          ?   33.33%           
=========================================
  Files             ?        1           
  Lines             ?      117           
  Branches          ?        0           
=========================================
  Hits              ?       39           
  Misses            ?       78           
  Partials          ?        0           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov-commenter avatar Oct 19 '24 14:10 codecov-commenter

tested against odoo 18.0 and 14.0 real projects

petrus-v avatar Oct 19 '24 14:10 petrus-v