Pytest: Mocking multiple random numbers



examples/pytest/random-more/app.py
import random

def random_sum(n):
    total = 0
    for _ in range(n):
        current = random.randint(0, 10)
        #print(current)
        total += current
    return total

examples/pytest/random-more/use_app.py
import app

result = app.random_sum(3)
print(result)

examples/pytest/random-more/test_app.py
import app

def test_random_sum(monkeypatch):
    values = [2, 3, 4]
    monkeypatch.setattr(app.random, 'randint', lambda x, y: values.pop(0))
    result = app.random_sum(3)
    assert result == 9