10/17/15

Python Mock

Mock is a library for testing in Python. It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.

Example:
import os
from mock import patch

def file_extension(file_path):
    if os.path.isfile(file_path):
        return os.path.splitext(file_path)[1]

def is_date(val):
try:
    datetimeobject = datetime.datetime.strptime(val, '%Y%m%d')
    return isinstance(datetimeobject, datetime.date)
 except:
    return False

if __name__ == '__main__':
    with patch('is_date'as mock_is_date:
        mock_is_date.return_value = True

 with patch('os.path.isfile'as mock_isfile:
        mock_isfile.return_value = True

No comments:

Post a Comment