Search code examples
pythonvisual-studio-codepytest

Pytest not working in VSCODE, after installing the package in venv


I'm trying to run pytest which worked in pycharm, but can't manage to run it on vscode.

First I'm doing:

python -m venv env
env\Scripts\activate
python -m pip install --upgrade pip
pip install selenium webdriver-manager pytest pytest-html
Package            Version
------------------ -----------
pip                23.3.2
pytest             7.4.4
pytest-html        4.1.1
pytest-metadata    3.0.0
pytest-xdist       3.5.0
selenium           4.16.0
setuptools         69.0.3
webdriver-manager  4.0.1

The packages appear in the venv yet getting the following error:

(env) PS C:\Users\User\Desktop\automation-projects\Python\paloAlto> pytest
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "C:\Users\User\Desktop\automation-projects\Python\paloAlto\env\Lib\site-packages\_pytest\main.py", line 267, in wrap_session
INTERNALERROR>     config._do_configure()
INTERNALERROR>   File "C:\Users\User\Desktop\automation-projects\Python\paloAlto\env\Lib\site-packages\_pytest\config\__init__.py", line 1053, in _do_configure
INTERNALERROR>     self.hook.pytest_configure.call_historic(kwargs=dict(config=self))
INTERNALERROR>   File "C:\Users\User\Desktop\automation-projects\Python\paloAlto\env\Lib\site-packages\pluggy\_hooks.py", line 514, in call_historic
INTERNALERROR>     res = self._hookexec(self.name, self._hookimpls, kwargs, False)
INTERNALERROR>           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\User\Desktop\automation-projects\Python\paloAlto\env\Lib\site-packages\pluggy\_manager.py", line 115, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\User\Desktop\automation-projects\Python\paloAlto\env\Lib\site-packages\pluggy\_callers.py", line 113, in _multicall
INTERNALERROR>     raise exception.with_traceback(exception.__traceback__)
INTERNALERROR>   File "C:\Users\User\Desktop\automation-projects\Python\paloAlto\env\Lib\site-packages\pluggy\_callers.py", line 77, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>           ^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File "C:\Users\User\Desktop\automation-projects\Python\paloAlto\testCases\conftest.py", line 49, in pytest_configure
INTERNALERROR>     config._metadata['Project Name'] = 'Palo Alto'
INTERNALERROR>     ^^^^^^^^^^^^^^^^
INTERNALERROR> AttributeError: 'Config' object has no attribute '_metadata'

Solution

  • INTERNALERROR>   File "C:\Users\User\Desktop\automation-projects\Python\paloAlto\testCases\conftest.py", line 49, in pytest_configure
    INTERNALERROR>     config._metadata['Project Name'] = 'Palo Alto'
    

    In pytest, the Config object does not have a _metadata attribute by default.

    It should be changed to:

    def pytest_addoption(parser):
        parser.addini("metadata", type="args", help="add metadata", default=[])
    
    def pytest_configure(config):
        for name in config.getini("metadata"):
            config._metadata[name] = 'Palo Alto'
    

    This should resolve the AttributeError you're seeing.