Skip to content

This is a simple demonstration of how to mock functions in python.

Notifications You must be signed in to change notification settings

kalsmic/python_mock_intro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Simple introduction to Mocking in Python

Added for comparison

mock return_value using patch in with context

def test_add_two_mocked_return_value(self):
    with mock.patch('builtins.input', return_value=10):
        result = add_two()
        expected = 20
        self.assertEqual(expected, result)

mock return_value using patch as decorator

@mock.patch('builtins.input')
def test_add_two_mocked_return_value(self, mock_input):
    mock_input.return_value=10
    result = add_two()
    expected = 20
    self.assertEqual(expected, result)

side_effect using patch in with context

def test_add_two_mocked_side_effect(self):
    with mock.patch('builtins.input', side_effect=[7, 10]):
        result = add_two()
        expected = 17
        self.assertEqual(expected, result)

side_effect using patch as decorator

@mock.patch('builtins.input')
def test_add_two_mocked_side_effect(self,mock_input):
    mock_input.side_effect = [7, 10]
    result = add_two()
    expected = 17
    self.assertEqual(expected, result)

About

This is a simple demonstration of how to mock functions in python.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages