Mocking a whole class



examples/classy/app.py
import json

class Thing(object):
    def data_file():
        return "/corporate/fixed/path/data.json"

    def get_sum(self):
        data_file = self.data_file()
        with open(data_file) as fh:
            data = json.load(fh)
            # ...
            result = data['x'] + data['y']
            return result

examples/classy/data.json
{
    "x": 19,
    "y": 23
}

examples/classy/test_app.py
import app

def test_sum():
    app.Thing.data_file = lambda self: 'data.json'
    t = app.Thing()
    res = t.get_sum()
    assert True
    assert res == 42