Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 由于缺少模块,py.test失败_Python_Pytest - Fatal编程技术网

Python 由于缺少模块,py.test失败

Python 由于缺少模块,py.test失败,python,pytest,Python,Pytest,我想编写一个测试,但只有安装了nlopt模块,此测试才会通过。由于这个模块是可选的,我想知道是否有一种方法可以编写一个测试,如果模块不存在,它不会阻止py.test完全失败。此时,py.test停止,因为它找不到nlopt模块: $ make test py.test --exitfirst tests/ ============================================================= test session starts ==============

我想编写一个测试,但只有安装了
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
nlopt
模块在我正在编写测试的库中使用。当前,如果找不到模块,库将引发异常。在使用模块的文件的顶层:

try:
    import nlopt

except ImportError:
    print("""\033[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.\033[0m""")
    raise
使用pytest提供的:

nlopt = pytest.importorskip('nlopt')

将该行放在使用
nlopt
(或一组函数的设置方法)的特定测试函数中,它只会在无法进行导入时跳过这些函数。

我尝试过,但代码在我提供测试的库中仍然失败,与库内部一样,还有一个
import nlopt
,如果它找不到模块,就会引发异常。您可以将该
import
移动到实际使用它的函数内部,而不是使其处于顶级。您是指库端还是测试端?在库端。当未找到
nlopt
时,库代码当前做什么?我修改了我的帖子,向您展示了库当前做什么。这看起来很难看,但我不知道有什么更好的方法来实现这一点。理想情况下,我希望库能够工作,即使计算机中没有安装模块。
nlopt = pytest.importorskip('nlopt')