Testing Fixed Output of Custom Random Number Generator

  • Share this:

Code introduction


This test function tests a custom random number generator function. It uses the unittest's mock module to simulate the behavior of the random module, ensuring that the random number generator function returns a fixed value.


Technology Stack : unittest, mock, random

Code Type : Test function

Code Difficulty : Intermediate


                
                    
import unittest
from unittest.mock import patch

def random_number_generator():
    return 42

def test_random_number_generator():
    with patch('random.random') as mocked_random:
        mocked_random.return_value = 0.5
        assert random_number_generator() == 42