运行 py.test 根据本地代码从另一个 pip 安装包进行测试

Running py.test tests from another pip-installed package against local code

我正在构建一个包,它提供 (1) 固定接口和 (2) 可插入驱动程序。所需的驱动程序将由软件包的用户选择。我的包将包含至少一个驱动程序,但我希望其他开发人员能够实现和验证符合包接口的驱动程序。因此,我希望这些开发人员能够运行 我的 测试他们的 驱动程序。

目前,我正在使用 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 安装包来能够参考 my pip 安装包中包含的测试。)

I solved this 来自:

  1. 实施 a custom py.test collector 从我的包中移植必要的测试套件,
  2. 要求消费者在他们的一个测试文件中实例化我的自定义 class,my_pkg.MyDriverSuite,然后,
  3. 编写 a custom py.test plugin 使用 pytest_pycollect_makeitem 挂钩拦截自定义实例并注入我的收集器。

(注意:这种方法避免了参数化夹具的使用。)


自定义收集器是目前为止最难弄清楚的部分,因为它需要大量挖掘 py.test 内部结构,并最终重新实现核心的精简版本 py.test 收集器,Session,具有自定义会话 Config,其 rootdir 设置为我的包裹的会话。