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.
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 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.
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)
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) but when I run:
$ py.test -v -s test_person.py I get the following result:
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 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.
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?)
So, what am I doing wrong?
Any help would be really appreciated.
Thank you in advance.