diff --git a/tests/conftest.py b/tests/conftest.py index 5641392d5..8ba0f4880 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -202,3 +202,27 @@ def pytest_namespace(): 'requires_eigen_and_scipy': skipif(not have_eigen or not scipy, reason="eigen and/or scipy are not installed"), } + + +def _test_import_pybind11(): + """Early diagnostic for test module initialization errors + + When there is an error during initialization, the first import will report the + real error while all subsequent imports will report nonsense. This import test + is done early (in the pytest configuration file, before any tests) in order to + avoid the noise of having all tests fail with identical error messages. + + Any possible exception is caught here and reported manually *without* the stack + trace. This further reduces noise since the trace would only show pytest internals + which are not useful for debugging pybind11 module issues. + """ + # noinspection PyBroadException + try: + import pybind11_tests + except Exception as e: + print("Failed to import pybind11_tests from pytest:") + print(" {}: {}".format(type(e).__name__, e)) + sys.exit(1) + + +_test_import_pybind11() diff --git a/tests/test_eigen.py b/tests/test_eigen.py index 43cc2fb71..2a58f8c40 100644 --- a/tests/test_eigen.py +++ b/tests/test_eigen.py @@ -3,12 +3,11 @@ import pytest with pytest.suppress(ImportError): import numpy as np - -ref = np.array([[ 0, 3, 0, 0, 0, 11], - [22, 0, 0, 0, 17, 11], - [ 7, 5, 0, 1, 0, 11], - [ 0, 0, 0, 0, 0, 11], - [ 0, 0, 14, 0, 8, 11]]) + ref = np.array([[ 0, 3, 0, 0, 0, 11], + [22, 0, 0, 0, 17, 11], + [ 7, 5, 0, 1, 0, 11], + [ 0, 0, 0, 0, 0, 11], + [ 0, 0, 14, 0, 8, 11]]) def assert_equal_ref(mat): diff --git a/tests/test_numpy_dtypes.cpp b/tests/test_numpy_dtypes.cpp index b4266d0f3..7133a8047 100644 --- a/tests/test_numpy_dtypes.cpp +++ b/tests/test_numpy_dtypes.cpp @@ -268,6 +268,12 @@ py::list test_dtype_methods() { } void init_ex_numpy_dtypes(py::module &m) { + try { + py::module::import("numpy"); + } catch (...) { + return; + } + PYBIND11_NUMPY_DTYPE(SimpleStruct, x, y, z); PYBIND11_NUMPY_DTYPE(PackedStruct, x, y, z); PYBIND11_NUMPY_DTYPE(NestedStruct, a, b); diff --git a/tests/test_numpy_dtypes.py b/tests/test_numpy_dtypes.py index 73da34644..2f4cab0f0 100644 --- a/tests/test_numpy_dtypes.py +++ b/tests/test_numpy_dtypes.py @@ -2,15 +2,15 @@ import pytest with pytest.suppress(ImportError): import numpy as np + simple_dtype = np.dtype({'names': ['x', 'y', 'z'], + 'formats': ['?', 'u4', 'f4'], + 'offsets': [0, 4, 8]}) + packed_dtype = np.dtype([('x', '?'), ('y', 'u4'), ('z', 'f4')]) + def assert_equal(actual, expected_data, expected_dtype): np.testing.assert_equal(actual, np.array(expected_data, dtype=expected_dtype)) -simple_dtype = np.dtype({'names': ['x', 'y', 'z'], - 'formats': ['?', 'u4', 'f4'], - 'offsets': [0, 4, 8]}) -packed_dtype = np.dtype([('x', '?'), ('y', 'u4'), ('z', 'f4')]) - @pytest.requires_numpy def test_format_descriptors():