为什么我可以从其他文件中修补一个单元测试的类别的类?

发布于 2025-01-22 23:27:34 字数 839 浏览 0 评论 0原文

我有一个名为some.py的文件:

from dataclasses import dataclass
from some_folder.some_reader import SomeReader

class SomeClass:
    def __init__(self, url):
        self.reader = SomeReader(url)

some_reader.py

class SomeReader:
    def read_path(self, url):
        return "read_path"

some_test.py

from unittest.mock importpatch
from some_folder.some import SomeClass

@patch("some_folder.some_reader.SomeReader")
def test_some_class(mock_some_reader):
    SomeClass("")

当我运行some_test ,看来somereader实例化不是模拟对象,而是:

SomeReader
<class 'some_folder.some_reader.SomeReader'>

如果我将somereader放入some.py 。

I have a file called some.py:

from dataclasses import dataclass
from some_folder.some_reader import SomeReader

class SomeClass:
    def __init__(self, url):
        self.reader = SomeReader(url)

and some_reader.py:

class SomeReader:
    def read_path(self, url):
        return "read_path"

and some_test.py:

from unittest.mock importpatch
from some_folder.some import SomeClass

@patch("some_folder.some_reader.SomeReader")
def test_some_class(mock_some_reader):
    SomeClass("")

When I run some_test, it seems that the SomeReader instantiated is not a mock object, instead it is:

SomeReader
<class 'some_folder.some_reader.SomeReader'>

Everything seems to work fine if I put SomeReader in some.py.

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

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

发布评论

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

评论(1

甩你一脸翔 2025-01-29 23:27:34

参见在哪里进行修补

from some_folder.some_reader import SomeReader


class SomeClass:
    def __init__(self, url):
        self.reader = SomeReader(url)

class SomeReader:
    def read_path(self, url):
        return "read_path"

import unittest
from unittest.mock import patch
from some import SomeClass


class TestSome(unittest.TestCase):
    @patch("some.SomeReader")
    def test_some_class(self, mock_some_reader):
        some = SomeClass("")
        print(some.reader)

if __name__ == '__main__':
    unittest.main()

<MagicMock name='SomeReader()' id='4520784368'>
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

See Where to patch

some.py:

from some_folder.some_reader import SomeReader


class SomeClass:
    def __init__(self, url):
        self.reader = SomeReader(url)

some_folder/some_reader.py:

class SomeReader:
    def read_path(self, url):
        return "read_path"

test_some.py:

import unittest
from unittest.mock import patch
from some import SomeClass


class TestSome(unittest.TestCase):
    @patch("some.SomeReader")
    def test_some_class(self, mock_some_reader):
        some = SomeClass("")
        print(some.reader)

if __name__ == '__main__':
    unittest.main()

Test result:

<MagicMock name='SomeReader()' id='4520784368'>
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

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