2014-08-29 39 views
1

我做了一個基本包,以方便在金字塔應用程序中使用SQLAlchemy:pyramid_sqlalchemy。當你使用這個軟件包爲應用程序編寫測試時,你將需要一些固件來提供一個工作的數據庫環境。這些固定裝置是完全通用的,不需要爲應用程序定製。此時這些燈具列在testing chapter of the documentation中,並要求人們複製&將它們粘貼到其代碼中。毋庸置疑,這並不理想。創建可重用的py.text燈具

是否有pyramid_sqlalchemy一個簡單的方法來公開這些燈具在某種程度上其它軟件包可以直接使用它們?我嘗試了一種天真的方法,創建一個包含所有燈具的pyramid_sqlalchemy.fixtures模塊,並在應用測試的conftest.py中執行from pyramid_sqlalchemy.fixtures import *,但這並未導致燈具被拾取。

回答

2

看一看約在http://pytest.org/latest/plugins.html#making-your-plugin-installable-by-others安裝外部插件的文檔。

基本上你創建一個Python包與setuptools的入口點:

# sample ./setup.py file 
from setuptools import setup 

setup(
    name="myproject", 
    packages = ['myproject'] 

    # the following makes a plugin available to pytest 
    entry_points = { 
     'pytest11': [ 
      'name_of_plugin = myproject.pluginmodule', 
      ] 
    }, 
) 

哪裏myproject.pluginmodule是包含您的燈具模塊。然後使用pip安裝該軟件包將使pytest在啓動時加載它。有關示例,請參見pytest-mock或任何其他pytest插件。

希望幫助,