我在myfile.py文件中有以下函数:
#myfile.py
import psutil
class RunnableObject:
def run(self):
parent = psutil.Process()
print(parent)
children = parent.children(recursive=True)
print(children)
然后我有一个单元测试,其中runnable_对象是RunnableObject类的一个实例,我使用pytest夹具设置该类
@patch("myfile.psutil")
def test_run_post_request(self, psutil_, runnable_object):
runnable_object.run()
assert psutil_.Process.call_count == 1
assert psutil_.Process.children.call_count == 1
但是,当我运行测试时,出现以下错误:
assert psutil_.Process.call_count == 1
> assert psutil_.Process.children.call_count == 1
E assert 0 == 1
E +0
E -1
-1
tests/unit/test_experiment.py:1651: AssertionError
我的标准:
<MagicMock name='psutil.Process()' id='3001903696'>
<MagicMock name='psutil.Process().children()' id='3000968624'>
我还尝试使用@patch.object(psutil.Process, "children")
以及@patch("myfile.psutil.Process")
和@patch("myfile.psutil.Process.children")
,但这给了我同样的问题
children
是psutil.Process()
的返回值的属性。不是Process
方法的属性因此,正确的断言是:
test_myfile.py
:测试结果:
相关问题 更多 >
编程相关推荐