Skip to content

Instantly share code, notes, and snippets.

@hjwp hjwp/family.py
Created Aug 13, 2014

Embed
What would you like to do?
example of testing super calls
x = None
class Parent:
def foo(self):
return 'parent'
class Child(Parent):
def foo(self):
if x:
return x
else:
return super().foo()
import unittest
from unittest.mock import patch
from family import Child
class Tester(unittest.TestCase):
@patch('family.x', False)
@patch('family.Parent.foo')
def test_calls_super_if_not_x(self, mock_parent_foo):
c = Child()
r = c.foo()
self.assertEqual(r, mock_parent_foo.return_value)
@patch('family.x', 3)
@patch('family.Parent.foo')
def test_returns_x_if_x_truthy(self, mock_parent_foo):
c = Child()
r = c.foo()
self.assertEqual(r, 3)
self.assertFalse(mock_parent_foo.called)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.