File: test_patch.py

package info (click to toggle)
python-testbook 0.4.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 408 kB
  • sloc: python: 1,045; makefile: 11
file content (41 lines) | stat: -rw-r--r-- 1,348 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from testbook.testbook import testbook
from testbook.exceptions import TestbookRuntimeError

import pytest


@pytest.fixture(scope='module')
def tb():
    with testbook('tests/resources/patch.ipynb', execute=True) as tb:
        yield tb


class TestPatch:
    @pytest.mark.parametrize(
        "target, func", [("os.listdir", "listdir"), ("os.popen", "get_branch")]
    )
    def test_patch_basic(self, target, func, tb):
        with tb.patch(target) as mock_obj:
            tb.ref(func)()
            mock_obj.assert_called_once()

    @pytest.mark.parametrize(
        "target, func", [("os.listdir", "listdir"), ("os.popen", "get_branch")]
    )
    def test_patch_raises_error(self, target, func, tb):
        with pytest.raises(TestbookRuntimeError), tb.patch(target) as mock_obj:
            mock_obj.assert_called_once()

    def test_patch_return_value(self, tb):
        with tb.patch("os.listdir", return_value=['file1', 'file2']) as mock_listdir:
            assert tb.ref("listdir")() == ['file1', 'file2']
            mock_listdir.assert_called_once()


class TestPatchDict:
    @pytest.mark.parametrize(
        "in_dict, values", [("os.environ", {"PATH": "/usr/bin"})],
    )
    def test_patch_dict(self, in_dict, values, tb):
        with tb.patch_dict(in_dict, values, clear=True):
            assert tb.ref(in_dict) == values