Skip to content

Instantly share code, notes, and snippets.

@elena-roff
Created March 12, 2019 13:33
Show Gist options
  • Save elena-roff/d427061dc47828e698e7831141ca5567 to your computer and use it in GitHub Desktop.
Save elena-roff/d427061dc47828e698e7831141ca5567 to your computer and use it in GitHub Desktop.
Pytest unit test examples
def test_format_to_dict():
""" Tests formatting to a dictionary. """
data_to_transform = defaultdict(lambda: defaultdict(lambda: {}))
assert isinstance(format_to_dict(data_to_transform), dict)
def test_valid():
valid_data = {'valid_key': 1}
assert function_to_test(valid_data) == 1
def test_raise_keyerror():
non_valid_data = {'non_valid_key': 1}
with pytest.raises(KeyError):
function_to_test(non_valid_data)
def test_valid_return_value(mocker):
""" Tests return."""
from module import some_function
path = 'module.some_function'
with mocker.patch(path, return_value=[]):
assert some_function() == {}
def test_with_reset_global_var(mocker):
# reset global variable _data
mocker.patch('module._data', None)
<funcitonality that needs emplty global variable>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment