py.test 由于缺少模块而失败
py.test fails due to missing module
我想写一个测试,但是这个测试只有安装了nlopt
模块才能通过。由于此模块是 可选的 ,我想知道是否有一种方法可以编写不会阻止 py.test 在模块不存在时完全失败的测试。此时,py.test 停止,因为它找不到 nlopt
模块:
$ make test
py.test --exitfirst tests/
============================================================= test session starts =============================================================
platform darwin -- Python 3.4.2 -- py-1.4.26 -- pytest-2.6.4
collecting 0 items / 1 errors
=================================================================== ERRORS ====================================================================
_____________________________________________ ERROR collecting tests/unit/fem/test_simulation.py ______________________________________________
tests/unit/fem/test_simulation.py:5: in <module>
from hybrida.fem import Simulation, Step, Eigenvalue
src/hybrida/__init__.py:4: in <module>
from . import geometry
src/hybrida/geometry/__init__.py:3: in <module>
from . import distance
src/hybrida/geometry/distance.py:9: in <module>
import nlopt
E ImportError: No module named 'nlopt'
--------------------------------------------------------------- Captured stdout ---------------------------------------------------------------
nlopt does not seem to be installed.
=========================================================== short test summary info ===========================================================
ERROR tests/unit/fem/test_simulation.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================== 1 error in 0.67 seconds ===========================================================
make: *** [test] Error 2
我试图在测试文件的开头添加一个 try-except
块,但这没有帮助:
try:
import nlopt
import numpy as np
except ImportError:
print("""nlopt does not seem to be installed""")
nlopt
模块在我正在为其编写测试的库中使用。目前,如果找不到模块,库会引发异常。在使用模块的文件的顶层:
try:
import nlopt
except ImportError:
print("""3[91m nlopt does not seem to be installed. Please install it by downloading nlopt, and installing it using
$ ./configure --enable-shared
$ make
$ make install
and adding /usr/local/lib/Python3.4/site-packages to the PYTHONPATH
(or wherever nlopt has been installed):
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/Python3.4/site-packages
Note: although Homebrew provides nlopt, it does not install the Python interface.3[0m""")
raise
使用conditional import machinerypytest提供:
nlopt = pytest.importorskip('nlopt')
将该行放在使用 nlopt
的特定测试函数中(或在一组函数的设置方法中),它只会在无法导入时跳过那些。
我想写一个测试,但是这个测试只有安装了nlopt
模块才能通过。由于此模块是 可选的 ,我想知道是否有一种方法可以编写不会阻止 py.test 在模块不存在时完全失败的测试。此时,py.test 停止,因为它找不到 nlopt
模块:
$ make test
py.test --exitfirst tests/
============================================================= test session starts =============================================================
platform darwin -- Python 3.4.2 -- py-1.4.26 -- pytest-2.6.4
collecting 0 items / 1 errors
=================================================================== ERRORS ====================================================================
_____________________________________________ ERROR collecting tests/unit/fem/test_simulation.py ______________________________________________
tests/unit/fem/test_simulation.py:5: in <module>
from hybrida.fem import Simulation, Step, Eigenvalue
src/hybrida/__init__.py:4: in <module>
from . import geometry
src/hybrida/geometry/__init__.py:3: in <module>
from . import distance
src/hybrida/geometry/distance.py:9: in <module>
import nlopt
E ImportError: No module named 'nlopt'
--------------------------------------------------------------- Captured stdout ---------------------------------------------------------------
nlopt does not seem to be installed.
=========================================================== short test summary info ===========================================================
ERROR tests/unit/fem/test_simulation.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================== 1 error in 0.67 seconds ===========================================================
make: *** [test] Error 2
我试图在测试文件的开头添加一个 try-except
块,但这没有帮助:
try:
import nlopt
import numpy as np
except ImportError:
print("""nlopt does not seem to be installed""")
nlopt
模块在我正在为其编写测试的库中使用。目前,如果找不到模块,库会引发异常。在使用模块的文件的顶层:
try:
import nlopt
except ImportError:
print("""3[91m nlopt does not seem to be installed. Please install it by downloading nlopt, and installing it using
$ ./configure --enable-shared
$ make
$ make install
and adding /usr/local/lib/Python3.4/site-packages to the PYTHONPATH
(or wherever nlopt has been installed):
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/Python3.4/site-packages
Note: although Homebrew provides nlopt, it does not install the Python interface.3[0m""")
raise
使用conditional import machinerypytest提供:
nlopt = pytest.importorskip('nlopt')
将该行放在使用 nlopt
的特定测试函数中(或在一组函数的设置方法中),它只会在无法导入时跳过那些。