Can I patch 'random' using unittest.mock.patch?
I'm interested in testing some code that uses the 'random' module, and I'd
like to be able to patch/insert my own fake version of random when my
tests are running, that returns a known value, and put it back to the
normal random module afterwards. From the documentation I can only see
that I can patch classes. Is there a way to patch functions? Something
like this:
def my_code_that_uses_random():
return random.choice([0, 1, 2, 3])
with patch.function(random.choice, return_value=3) as mock_random:
choice = my_code_that_uses_random()
assert choice == 3
That code doesn't work, what do I need instead?
No comments:
Post a Comment