<commit_before>---
---

[Ref](https://semaphoreci.com/community/tutorials/getting-started-with-mocking-in-python)

#### calc.py
```python
import time

class Calculator:
    def sum(self, a, b):
        time.sleep(10)
        return a + b
```

#### tests.py
```python
from unittest import TestCase, mock
from calc import Calculator

def mock_sum(a, b):
      return a + b

class TestCalc(TestCase):
    def setUp(self):
        self.calc = Calculator

    @mock.patch('calc.requests')
    @mock.patch('calc.Calculator.sum')
    def test_sum_return_value(self, sum, mock_requests):
        sum.return_value = 9

        mock_requests.get.return_value.content = 'Content of the request'
        # Mocks requests.get().content

        answer = self.calc.sum(2, 4)
        self.assertEqual(answer, 9)

    @mock.patch('calc.Calculator.sum')
    def test_sum_side_effect(self, sum):
        sum.side_effect = mock_sum

        answer = self.calc.sum(2, 4)
        self.assertEqual(answer, 6)

    @mock.patch('calc.Calculator.sum', side_effect=Exception('Test'))
    def test_sum_side_effect(self, sum):
        answer = self.calc.sum(2, 4)  # raises exception
        self.assertEqual(answer, 6)

    @mock.patch('calc.Calculator.sum')
    def test_call_count(self, sum):
        sum.assert_called_with(1)  # Asserts the last call's parameters
        sum.assert_any_call(1)     # Asserts any call's parameters
        sum.assert_any_call(2)     # Asserts any call's parameters

        assert sum.call_count == 3
```<commit_msg>Improve python's mock notes<commit_after>---
doc: https://docs.python.org/3/library/unittest.mock.html
---

## Usage

`calc.py`

```python
import time

class Calculator:
    def sum(self, a, b):
        time.sleep(10)
        return a + b
```

`tests.py`

```python
from unittest import TestCase, mock
from calc import Calculator

def mock_sum(a, b):
      return a + b

class TestCalc(TestCase):
    def setUp(self):
        self.calc = Calculator

    @mock.patch('calc.requests')
    @mock.patch('calc.Calculator.sum')
    def test_sum_return_value(self, sum, mock_requests):
        sum.return_value = 9

        mock_requests.get.return_value.content = 'Content of the request'
        # Mocks requests.get().content

        answer = self.calc.sum(2, 4)
        self.assertEqual(answer, 9)

    @mock.patch('calc.Calculator.sum')
    def test_sum_side_effect(self, sum):
        sum.side_effect = mock_sum

        answer = self.calc.sum(2, 4)
        self.assertEqual(answer, 6)

    @mock.patch('calc.Calculator.sum', side_effect=Exception('Test'))
    def test_sum_side_effect(self, sum):
        answer = self.calc.sum(2, 4)  # raises exception
        self.assertEqual(answer, 6)

    @mock.patch('calc.Calculator.sum')
    def test_call_count(self, sum):
        sum.assert_called_with(1)  # Asserts the last call's parameters
        sum.assert_any_call(1)     # Asserts any call's parameters
        sum.assert_any_call(2)     # Asserts any call's parameters

        assert sum.call_count == 3
```

## Mock module

```python
import sys
import mock

sys.modules['some_module'] = mock.MagicMock()
```
{'fraction_non_alphanumeric': 0.12906309751434034, 'fraction_numerical': 0.010516252390057362, 'mean_word_length': 2.92375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3503860', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1017, 'n_words': 224}