为什么我可以从其他文件中修补一个单元测试的类别的类?
我有一个名为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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
参见在哪里进行修补
。
See Where to patch
some.py
:some_folder/some_reader.py
:test_some.py
:Test result: