是否可以从夹具请求中检索其他请求的夹具?

Is it possible to retrieve other requested fixtures from fixture request?

例如:

@pytest.fixture()
def connection():
    return make_connection()


@pytest.fixture()
def database(connection):
    connection = request.fixtures['connection']
    return create_database(connection)


@pytest.fixture()
def table(request):
    database = request.fixtures['database']
    return create_table(database)


@pytest.mark.usefixtures('database')
def test_whatever(connection, table):
    insert_some_data(table)
    connection.execute(...)
    ...

我可以使用当前版本的 Pytest 做到这一点吗 - 以非分层方式根据其他固定装置制作固定装置?

你可以这样做:

@pytest.fixture()
def fixture1():
    return 'fixture1'


@pytest.fixture()
def fixture2(request):
    if 'fixture1' in request._funcargs:
        # fixture1 was requested earlier
        # but most likely you don't need that check because getfuncargvalue always works
        fixture1_instance = request.getfuncargvalue('fixture1')
    return do_stuff(fixture1_instance)


def test_whatever(fixture1, fixture2):
    result = do_some_stuff(fixture1)
    assert result.supermethod(fixture2)