Created
November 17, 2019 22:45
-
-
Save MGough/8894e0f7cdcb21b11714ed4be46e15e2 to your computer and use it in GitHub Desktop.
Mocking using autospeccing.
This file contains 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
from unittest.mock import patch, Mock, create_autospec | |
from pytest import fixture | |
from service.example import SomeObjectToTest | |
from service.imported_class import SomeClassOutOfOurControl | |
class TestSomeObjectToTest: | |
@fixture | |
def expected_result(self): | |
return 400 | |
def test_do_something(self, expected_result): | |
object_under_test = SomeObjectToTest() | |
mocked_calculator = create_autospec(SomeClassOutOfOurControl) | |
mocked_calculator.add.return_value = expected_result | |
result = object_under_test.do_something(mocked_calculator) | |
assert result == expected_result | |
mocked_calculator.add.assert_called_once_with(5, 10) | |
@patch("service.example.SomeClassOutOfOurControl", autospec=True) | |
def test_do_something_else(self, mocked_class, expected_result): | |
mocked_class.return_value.add.return_value = expected_result | |
object_under_test = SomeObjectToTest() | |
result = object_under_test.do_something_else() | |
assert result == expected_result | |
object_under_test.calculator.add.assert_called_once_with(5, 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment