Created
August 13, 2014 18:22
-
-
Save hjwp/44210e3e164bbb1fed18 to your computer and use it in GitHub Desktop.
example of testing super calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = None | |
class Parent: | |
def foo(self): | |
return 'parent' | |
class Child(Parent): | |
def foo(self): | |
if x: | |
return x | |
else: | |
return super().foo() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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