有没有办法使用模拟来模拟 pytest 中的完整代码?

发布于 2025-01-18 05:25:59 字数 774 浏览 1 评论 0原文

例如,每当测试找到

database.db.session.using_bind("reader")

我要删除lust_bind(“读取器”))并与

database.db.session

使用模拟器

一起使用conftest.py.py尝试使用它来删除>

@pytest.fixture(scope='function')
def session(mocker):
    mocker.patch('store.database.db.session.using_bind', return_value=_db.db.session)

但是到目前为止什么都没有。

正在测试的代码:

from store import database     
results = database.db.session.using_bind("reader").query(database.Order.id).join(database.Shop).filter(database.Shop.deleted == False).all(), 

我得到

AttributeError: 'scoped_session' object has no attribute 'using_bind' as an error.

For instance, every time a test finds

database.db.session.using_bind("reader")

I want to remove the using_bind("reader")) and just work with

database.db.session

using mocker

Tried to use it like this in conftest.py

@pytest.fixture(scope='function')
def session(mocker):
    mocker.patch('store.database.db.session.using_bind', return_value=_db.db.session)

But nothing has worked so far.

Code under test:

from store import database     
results = database.db.session.using_bind("reader").query(database.Order.id).join(database.Shop).filter(database.Shop.deleted == False).all(), 

and I get

AttributeError: 'scoped_session' object has no attribute 'using_bind' as an error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〗斷ホ乔殘χμё〖 2025-01-25 05:25:59

让我们从一个MRE开始,其中正在测试的代码使用假数据库:

from unittest.mock import Mock, patch

class Session:
    def using_bind(self, bind):
        raise NotImplementedError(f"Can't bind {bind}")

    def query(self):
        return "success!"


database = Mock()
database.db.session = Session()


def code_under_test():
    return database.db.session.using_bind("reader").query()


def test():
    assert code_under_test() == "success!"

运行此测试失败:

E       NotImplementedError: Can't bind reader

因此,我们要模拟session.used_bind in code_under_teste_test,以便它返回<<代码>会话 - 这将使我们的测试通过。

我们使用patch这样做,例如:

@patch("test.database.db.session.using_bind")
def test(mock_bind):
    mock_bind.return_value = database.db.session
    assert code_under_test() == "success!"

请注意,我的代码位于名为test.py的文件中,因此我的patch> patch呼叫适用于测试模块 - 您需要对其进行调整,以指向您自己的代码中测试的模块。

还要注意,在调用正在测试的代码之前,我需要设置模拟

Let's start with an MRE where the code under test uses a fake database:

from unittest.mock import Mock, patch

class Session:
    def using_bind(self, bind):
        raise NotImplementedError(f"Can't bind {bind}")

    def query(self):
        return "success!"


database = Mock()
database.db.session = Session()


def code_under_test():
    return database.db.session.using_bind("reader").query()


def test():
    assert code_under_test() == "success!"

Running this test fails with:

E       NotImplementedError: Can't bind reader

So we want to mock session.using_bind in code_under_test so that it returns session -- that will make our test pass.

We do that using patch, like so:

@patch("test.database.db.session.using_bind")
def test(mock_bind):
    mock_bind.return_value = database.db.session
    assert code_under_test() == "success!"

Note that my code is in a file called test.py, so my patch call applies to the test module -- you will need to adjust this to point to the module under test in your own code.

Note also that I need to set up my mock before calling the code under test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文