Skip to content

Instantly share code, notes, and snippets.

@hjwp
Created August 13, 2014 18:22
Show Gist options
  • Save hjwp/44210e3e164bbb1fed18 to your computer and use it in GitHub Desktop.
Save hjwp/44210e3e164bbb1fed18 to your computer and use it in GitHub Desktop.
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