Testing Random Module Functionality with Nose Library

  • Share this:

Code introduction


This function uses tools from the nose library to test the functionality of the random module. It accepts two arguments, if both arguments are None, it tests whether two randomly generated numbers are equal; if the first argument is not None, it tests whether the generated random number is greater than the first argument; if the second argument is not None, it tests whether the generated random number is less than the second argument; if both arguments are not None, it tests whether the generated random number is equal to the values of both arguments.


Technology Stack : nose, random

Code Type : Test function

Code Difficulty : Intermediate


                
                    
import nose.tools as nt
import random

def test_random_module_function(arg1, arg2):
    if arg1 is None and arg2 is None:
        nt.assert_equal(random.random(), random.random())
    elif arg1 is not None and arg2 is None:
        nt.assert_true(random.random() > arg1)
    elif arg1 is None and arg2 is not None:
        nt.assert_true(random.random() < arg2)
    else:
        nt.assert_equal(random.random(), arg1, arg2)                
              
Tags: