How to enable pytest full trace on import error?

Viewed 198

We are testing our c++ library over SWIG in python using pytest. However, occasionally when having problem in the SWIG interpretation we are not getting the proper traceback information when importing our library in conftest.py When we import our library directly in python we get the full exception trace

How can we get the full traceback in pytest as could be achieved when importing the lib directly in python?

Below is a snippet from the SWIG auto-generated with additional manual injected exception for import test purposes

from sys import version_info as _swig_python_version_info
if _swig_python_version_info >= (2, 7, 0):
    def swig_import_helper():
        import importlib
        pkg = __name__.rpartition('.')[0]
        mname = '.'.join((pkg, '_sdk')).lstrip('.')
        try:
            # Added - Generate exception use case
            raise ImportError("**** Import Error Use Case ****")

            return importlib.import_module(mname)
        except ImportError:
            return importlib.import_module('_sdk')

When we import the library directly in python we get the full trace including the first exception occurrence:

Import in library directly in python

>>> from leaba import sdk
Traceback (most recent call last):
  File ".../out/opt3-debug/pylib/leaba/sdk.py", line 17, in swig_import_helper
    raise ImportError("**** Import Error Use Case ****")
ImportError: **** Import Error Use Case ****

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../out/opt3-debug/pylib/leaba/sdk.py", line 21, in <module>
    _sdk = swig_import_helper()
  File ".../out/opt3-debug/pylib/leaba/sdk.py", line 20, in swig_import_helper
    return importlib.import_module('_sdk')
  File "/common/pkgs/python/3.6.0/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named '_sdk'

Import in pytest conftest.py, missing the output of first traceback

ImportError while loading conftest '.../fishnet/tests/conftest.py'.
fishnet/tests/conftest.py:60: in <module>
    from init import device_init
fishnet/init/device_init.py:28: in <module>
    from leaba import sdk
.../out/opt3-debug/pylib/leaba/sdk.py:21: in <module>
    _sdk = swig_import_helper()
.../out/opt3-debug/pylib/leaba/sdk.py:20: in swig_import_helper
    return importlib.import_module('_sdk')
/common/pkgs/python/3.6.0/lib/python3.6/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
E   ModuleNotFoundError: No module named '_sdk'

How can we tell pytest to provide the full traceback and not to truncate and provide only the last one?

0 Answers
Related