camptocamp / pytest-odoo

pytest plugin to run Odoo tests
GNU Affero General Public License v3.0
68 stars 45 forks source link

`ModuleNotFoundError: No module named 'odoo'` error on trying to launch #68

Closed mathisgauthey closed 4 months ago

mathisgauthey commented 4 months ago

Hey there. Just like #56 and running my odoo-dev-env.

I try the command : pytest -s --odoo-database=odoo-db and get the following error :

pytest -s --odoo-database=odoo-db
Traceback (most recent call last):
  File "/home/mat/documents/odoo-dev-env/venv/bin/pytest", line 8, in <module>
    sys.exit(console_main())
  File "/home/mat/documents/odoo-dev-env/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 206, in console_main
    code = main()
  File "/home/mat/documents/odoo-dev-env/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 159, in main
    config = _prepareconfig(args, plugins)
  File "/home/mat/documents/odoo-dev-env/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 346, in _prepareconfig
    config = pluginmanager.hook.pytest_cmdline_parse(
  File "/home/mat/documents/odoo-dev-env/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 513, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  File "/home/mat/documents/odoo-dev-env/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 120, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/home/mat/documents/odoo-dev-env/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 139, in _multicall
    raise exception.with_traceback(exception.__traceback__)
  File "/home/mat/documents/odoo-dev-env/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 122, in _multicall
    teardown.throw(exception)  # type: ignore[union-attr]
  File "/home/mat/documents/odoo-dev-env/venv/lib/python3.10/site-packages/_pytest/helpconfig.py", line 106, in pytest_cmdline_parse
    config = yield
  File "/home/mat/documents/odoo-dev-env/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 103, in _multicall
    res = hook_impl.function(*args)
  File "/home/mat/documents/odoo-dev-env/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1152, in pytest_cmdline_parse
    self.parse(args)
  File "/home/mat/documents/odoo-dev-env/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1501, in parse
    self._preparse(args, addopts=addopts)
  File "/home/mat/documents/odoo-dev-env/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1388, in _preparse
    self.pluginmanager.load_setuptools_entrypoints("pytest11")
  File "/home/mat/documents/odoo-dev-env/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 421, in load_setuptools_entrypoints
    plugin = ep.load()
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 171, in load
    module = import_module(match.group('module'))
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "/home/mat/documents/odoo-dev-env/venv/lib/python3.10/site-packages/_pytest/assertion/rewrite.py", line 178, in exec_module
    exec(co, module.__dict__)
  File "/home/mat/documents/odoo-dev-env/venv/lib/python3.10/site-packages/pytest_odoo.py", line 18, in <module>
    import odoo
ModuleNotFoundError: No module named 'odoo'

Thansk in advance !

mathisgauthey commented 4 months ago

I did a pip install -e . inside my Odoo source directory.

I also downgraded pytest to pip install pytest==7.4.4 because there's an issue with pytest > 8 apparently.

But it can't find my custom addons correctly. Is there any way to help him find it ?

Here is my workspace organization : image

mathisgauthey commented 4 months ago

Alright, I managed to make the commandline work by adding pytest -s PATH_TO_TEST_FILE --odoo-database=odoo-db --odoo-config=config/odoo.conf

And this is my odoo.conf :

[options]
addons_path = src/odoo/odoo/addons,src/odoo/addons,addons/odoo-api-rest,addons/odoo-firebase-push-notification,addons/ouktuva-odoo-addons
db_host = localhost
db_port = 5432
dbfilter = ^odoo-db$
db_name = odoo-db
db_user = odoo
db_password = odoo
admin_passwd = admin
dev = all
debug_mode = True

But I can't seem to make the VS Code testing extension work.

image

My settings.json :

"python.testing.pytestArgs": [
        "PATH_TO_TEST_FOLDER",
    ],
    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true,
    "terminal.integrated.env.windows": {
        "PYTHONPATH": "${workspaceFolder}/src/odoo"
    },
    "terminal.integrated.env.linux": {
        "PYTHONPATH": "${workspaceFolder}/src/odoo"
    }

Error :

2024-05-17 14:05:48.347 [error] pytest test discovery error for workspace:  /home/mat/documents/odoo-dev-env 
 ModuleNotFoundError: No module named 'odoo.addons.ADDON_NAME'

Is there any way to provide pytest with the addon_path for Odoo to take ? I'm this close to get a fully fledged Odoo WSL dev env that works well enough for any use case I'm facing.

mathisgauthey commented 4 months ago

Alright, guess I found out because it now works flawlessly using these settings.json for VSCODE :

"python.testing.pytestArgs": [
        "addons/ADDON_NAME/tests",
        "--odoo-database",
        "odoo-db",
        "--odoo-config",
        "config/odoo.conf"
    ],
    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true,
    "terminal.integrated.env.windows": {
        "PYTHONPATH": "${workspaceFolder}/src/odoo",
        "ODOO_RC": "${workspaceFolder}/config/odoo.conf",
        "OPENERP_SERVER": "${workspaceFolder}/config/odoo.conf"
    },
    "terminal.integrated.env.linux": {
        "PYTHONPATH": "${workspaceFolder}/src/odoo",
        "ODOO_RC": "${workspaceFolder}/config/odoo.conf",
        "OPENERP_SERVER": "${workspaceFolder}/config/odoo.conf"
    }

With odoo.conf being :

[options]
addons_path = COMMA_SEPARATED_ADDONS_PATH_LIST
db_host = localhost
db_port = 5432
dbfilter = ^odoo-db$
db_name = odoo-db
db_user = odoo
db_password = odoo
admin_passwd = admin
dev = all
debug_mode = True