带有 monkeypatch 的 pytest 夹具参数

pytest fixture params with monkeypatch

我正在修补数据库连接 class 作为固定装置。我想要完成的是将一些参数传递给我的 monkey patched fixture,因为不同的测试都需要相同的 monkeypatch 但具有不同的 return 值。

@pytest.fixture
def data():
    now = datetime.datetime.now()
    data = Data(now)
    return data

@pytest.fixture
def patch_db(monkeypatch):
    class FakeDbConnection:
        def __init__(*args, **kwargs):
            pass

        def fetchall(self):
            return [1,2,3]

    monkeypatch.setattr(DbConnection, 'execute', FakeDbConnection)


def test_get_somevalue(patch_db, data):
    userids = data.get_userids()
    assert userids == [1,2,3]

def test_get_something_else(patch_db, data):
    userids = data.get_userids()
    assert userids == [6,7,8]

我遇到的问题是因为我的 fetchall 函数只有 returns [1,2,3]。我试图让 patch_db fixture 接受参数:

@pytest.fixture(scope='module', params=[[1, 2, 3], [4, 5, 6]])
def patch_db(monkeypatch, test_values):
    class FakeDbConnection:
        def __init__(*args, **kwargs):
            pass

        def fetchall(self):
            return test_values.param

    monkeypatch.setattr(DbConnection, 'execute', FakeDbConnection)

但我收到以下错误: ScopeMismatch: You tried to access the 'function' scoped fixture 'monkeypatch' with a 'module' scoped request object, involved factories

您不能使用 module 夹具中的 function 夹具,如消息所述。

如果您将 patch_db 的范围更改为 function,它将 运行,但这种方法不会如您所愿地工作,因为 test_get_somevalue 会收到两个patch_db 个固定装置,一个用 [1, 2, 3] 修补,另一个用 [4, 5, 6].

修补

我建议您向 FakeDbConnection 添加一个函数,让测试将他们想要的 fetchall 设置为 return。这是完整的示例:

@pytest.fixture
def data():
    now = datetime.datetime.now()
    data = Data(now)
    return data


@pytest.fixture
def patch_db(monkeypatch):
    class FakeDbConnection:
        def __init__(*args, **kwargs):
            pass

        def fetchall(self):
            return fetch_result.values

    monkeypatch.setattr(DbConnection, 'execute', FakeDbConnection)

    class FetchResult:
        pass

    fetch_result = FetchResult()
    fetch_result.values = None
    return fetch_result


def test_get_somevalue(patch_db, data):
    patch_db.values = [1,2,3]
    userids = data.get_userids()
    assert userids == [1,2,3]


def test_get_something_else(patch_db, data):
    patch_db.values = [6,7,8]
    userids = data.get_userids()
    assert userids == [6,7,8]