jeudi 5 novembre 2015

Operations on mock.sentinel objects

I really like mock's sentinel values. It is a good way not to use random meaningless numbers in cases where you just want to write a minimal unit tests that covers one line.

However, the following

from mock import sentinel, patch

def test_multiply_stuff():
    with patch('module.data_source1',return_value=sentinel.source1):
        with patch('module.data_source1',return_value=sentinel.source1):
            assert function(module.data_source1,
                            module_data2) == sentinel.source1 * sentinel.source2

does not work. You'll get

TypeError: unsupported operand type(s) for *: '_SentinelObject' and '_SentinelObject'

I understand why: It makes sense that operations on sentinel objects can't evaluate to an expression.

Is there some technique that does it (preferably within mock) ?

Is there some hack I can use? Or is the best thing you can do just to use exemplary numbers?

Aucun commentaire:

Enregistrer un commentaire