Randomly Executing Test Cases with Nose Library

  • Share this:

Code introduction


This function randomly selects one test case from the provided list of test cases and runs it using the nose library.


Technology Stack : nose

Code Type : Test function generator

Code Difficulty : Intermediate


                
                    
import nose
import random

def random_test_case_generator(test_cases):
    """
    This function takes a list of test cases and randomly selects one to run.
    It uses the nose library to execute the selected test case.
    
    Parameters:
    test_cases (list): A list of test case functions to be executed.
    
    Returns:
    None
    """
    selected_test_case = random.choice(test_cases)
    nose.run(selected_test_case)

# Example usage
if __name__ == "__main__":
    @nose.tools.raises(ZeroDivisionError)
    def test_divide_by_zero():
        return 1 / 0

    @nose.tools.raises(ValueError)
    def test_invalid_input():
        return int("abc")

    test_cases = [test_divide_by_zero, test_invalid_input]
    random_test_case_generator(test_cases)                
              
Tags: