2016-08-23 50 views
2

我正在構建一個包,該包提供(1)帶有(2)可插拔驅動程序的固定接口。所需的驅動程序可由包的用戶選擇。我的軟件包至少會包含一個驅動程序,但我希望其他開發人員能夠實現和驗證符合軟件包界面的驅動程序。因此,我希望這些開發人員能夠運行我的測試針對他們的驅動程序。從本地代碼運行另一個pip安裝包的py.test測試

目前,我使用py.test的參數化夾具,我的驅動程序()注入到我的測試:

# conftest.py 
import my_pkg 
import pytest 

@pytest.fixture(params=[my_pkg.MyDriver]) 
def driver(request): 
    return request.param 
# my_pkg/tests/conftest.py 
import my_pkg 
import pytest 

@pytest.fixture 
def my_interface(driver): 
    return my_pkg.MyInterface(driver) 
# my_pkg/tests/test_my_interface.py 
def test_that_it_does_the_right_thing(my_interface): 
    assert my_interface.some_method() == "some return value" 

我構建它希望這樣,有人會能夠針對其driver夾具版本收集並運行我的測試。換句話說,他們會是這個樣子:

# setup.py 
from setuptools import setup 

setup(
    # ... 
    install_requires=["my-pkg"]) 
# conftest.py 
import their_pkg 
import pytest 

@pytest.fixture(params=[their_pkg.TheirDriver]) 
def driver(request): 
    return request.param 

這顯然是不夠的,得到它的工作,因爲py.test似乎沒有提供一個選項,以注入測試來自外部軟件包。 但是,如果可能的話,這是可能的嗎?

This question似乎是概念上類似,但作者似乎是一個代碼庫中的全部工作。我想一個完全獨立 PIP安裝包能夠引用納入 pip-測試安裝包)

回答

1

I solved this人:

  1. 實施a custom py.test collector是移植試驗的必要套件從我的包,
  2. 需要消費者來實例化自定義類礦,my_pkg.MyDriverSuite的,在他們的測試文件中的一個,那麼,
  3. a custom py.test plugin使用該pytest_pycollect_makeitem鉤子攔截自定義實例,並注入我的收藏家。

(注:此方法可以避免使用參數化夾具)


定製收集是迄今爲止最棘手的部分搞清楚,因爲它需要大量通過py.test挖掘內核,並最終重新實現一個精簡版的核心py.test收集器,Session,其定製會話Configrootdir被設置爲我的包。