Python way to replace real return values ​​and implement functions with breadboard

My flash drive + python application extracts json from its own binaries via a third-party module nativelib.

Essentially, my functions look like

def list_users():
    return nativelib.all_users()

However, the strong dependence on this third-party module and the huge internal component is a huge obstacle to rapid development.

What I would like to do is mock the return value of my function list_users. In addition, I should be able to return to the “real data” by simply switching some logical values. This boolean may be some attribute inside the code or a command line argument.

The solution I just developed looks something like this:

@mockable('users_list')
def list_users():
    return nativelib.all_users()

mockable :

class mockable:
    mock = False

    # A dictionary that contains a mapping between the api method
    # ..and the file which contains corresponding the mock json response
    __mock_json_resps = {'users_list':'/var/mock/list_user.json', 'user_by_id': '/var/mock/user_1.json'}

    def __init__(self, api_method):
        self.api_method = api_method

    def __call__(self, fn):
        @wraps(fn)
        def wrapper():
            if mock:
                # If mocking is enabled,read mock data from json file 
                mock_resp = __mock_json_resps[self.api_method]
                with open(mock_resp) as json_file:
                    return json.load(json_file)
            else:
                return self.fn()

        return wrapper

mockable.mock = True

, , , "" .

, ?

+4
2

mockable.mock , . . , .

nativelib.all_users, list_users, . , , nativelib :

if mock:
    class NativeLibMock:
        def all_users(self):
            return whatever # maybe configure the mock with init params
    nativelib = NativeLibMock()
else:
    import nativelib

, self .. , , NativeLibMock, , nativelib. , , .

, , , , PYTHONPATH ( sys.path) , , import mocklib as nativelib. , , , nativelib . , , , , nativelib, , ; -)

, , 'users_list' list_users list_users. , list_users , ​​- . - "" fn .

+2

mock, , . , unittests, . , , :

def list_users():
    return nativelib.all_users()

unittest, "":

def test_list_users(self):
    with mock.patch.object(nativelib, 
                           'all_users', 
                           return_value='json_data_here')):
        result = list_users()
        self.assertEqual(result, 'expected_result')

, , :

def mock_native_all_users(return_value):
    def _mock_native_all_users(func):
        def __mock_native_all_users(self, *args, **kwargs):
            with mock.patch.object(nativelib, 
                                   'all_users', 
                                   return_value=return_value):
                return func(self, *args, **kwargs)
        return __mock_native_all_users
    return _mock_native_all_users

:

@mock_native_all_users('json_data_to_return')
def test_list_users(self):
    result = list_users()
    self.assertEqual(result, 'expected_result')
+2

Source: https://habr.com/ru/post/1529257/


All Articles