在 Python 中模拟一个类以调用其方法之一 [英] Mock a class in Python in order to call one of its methods

查看:58
本文介绍了在 Python 中模拟一个类以调用其方法之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习 Python,与此同时,我尝试学习如何为我的代码编写测试.为此,我决定使用 py.testmock.我得到了一个相当大且复杂的类,要为其编写测试,因此一开始我决定自己编写一个更简单的示例.

I'm starting to learn Python, and along with that, I try learn how to write tests for my code. I've decided to use py.test and mock for that. I'm given a quite big and complex class, to write tests for, so for a start I decided to work on a simpler example of my own.

因此,我编写了一个非常简单的类(person.py 在一个名为 src_pkg 的包中)

So, I've written a very simple class (person.py in a package called src_pkg)

class Person():


    def __init__(self, name, age):
        self.name = name
        self.age = age

    def can_do_it(self, x):
        result = True if x > 5 else False
        print "result: ", result
        return result

我想要做的是模拟 Person 类,并创建模拟类的实例,以便能够调用 can_do_it() 方法.

What I want to do, is mock the Person class, and create an instance of the mocked class, in order to be able to call the can_do_it() method.

我想这样做的原因是因为我正在处理的真实类有一个非常复杂的构造函数,而且我不想通过编写类似 foo = 的东西来创建类的实例Foo(x, y, z)

The reason I want to do that, is because the real class I'm working on, has a really complex constructor, and I don't want to make an instance of the class by writing something like foo = Foo(x, y, z)

所以,我已经编写了我的测试代码(test_person.py 在一个名为 test_pkg 的包中),如下所示:

So, I've written my test code (test_person.py in a package called test_pkg), which is the following:

from mock import patch

class TestPerson():

    def test_can_do_it(self):
        with patch('src_pck.person.Person') as Person:
            person = Person.return_value
            print "can he do it? :", person.can_do_it(4)

但是当我跑步时:

$ py.test -v -s test_person.py

我得到以下结果:

platform linux2 -- Python 2.7.5 -- py-1.4.20 -- pytest-2.5.2 -- /home/kostas/.virtualenvs/excite/bin/python
collected 1 items 

test_person.py:5: TestPerson.test_can_do_it Can he do it? : <MagicMock name='Person().can_do_it()' id='37709904'>
PASSED

我希望表达式 print "can he do it? :", person.can_do_it(4) 会导致 他能做到吗?: 错误.因此,断言任何事情都没有意义.

I would expect that the expression print "can he do it? :", person.can_do_it(4) would result to can he do it? : False. As a result, there is no point of asserting anything.

我认为当我运行测试时,它根本不会调用 can_do_it() 方法!(否则方法的print语句会被打印出来,对吗?)

I think that when I run the test, it does not call the can_do_it() method at all! (otherwise the print statement of the method would be printed, right?)

那么,我做错了什么?

任何帮助将不胜感激.

提前致谢.

推荐答案

使用 mock.patch.object 修补 __init__ 方法:

Patch the __init__ method using mock.patch.object:

from mock import patch
import src_pkg.person as p

class TestPerson():
    def test_can_do_it(self):
        with patch.object(p.Person, '__init__', lambda self: None):
            person = p.Person()
            print "can he do it? :", person.can_do_it(4)

这篇关于在 Python 中模拟一个类以调用其方法之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆